Can I change my code depending on the build type?

G

Guest

I've got a function that needs to work slightly differently on the
development server from the production server, but of course I don't want to
be changing code just before compiling a release version!?

I'm thinking I could use some #if #else #endif type statements to change
certain things, depending on the build type. I'm thinking of something like
this in my .cs file...

#if DEBUG
private string MyFunc()
{
// debug version
}
#else
private string MyFunc()
{
// release version
}
#end if

Would this work!? If not, how could I achieve a similar effect? (.net 2)

Cheers


Dan
 
G

Guest

Yes, that works.

I use it myself in an application that adds a lot of records to a
database. The release version puts data in a buffer and saves them when
the buffer is full or has lived a certain time, but the debug version
flushes the buffer for each record so that they show up in the database
immediately.
 
G

Guest

fantastic! cheers :D

Göran Andersson said:
Yes, that works.

I use it myself in an application that adds a lot of records to a
database. The release version puts data in a buffer and saves them when
the buffer is full or has lived a certain time, but the debug version
flushes the buffer for each record so that they show up in the database
immediately.
 
G

Guest

Hi Goran...

I can't get this to work - I wonder if you could shed any light on it...

Basically, I've just setup a simple web, with a config and an aspx page,
which has the following in Page_Load()...

#if DEBUG
Response.Write("debug!");
#else
Response.Write("release!");
#endif

However, when I run this page (thru IIS), it *always* writes "debug!" - even
if I set compilation debug="false" in web.config.

Is there something else I need to enable?! I'm not using Web Publish or
anything like that, just a straightforward Rebuild and then point IE at the
webserver.

Any suggestions!?
 
G

Guest

The setting in web.config is only used if the web server does the
compilation. If you build the solution in Visual Studio, it's the
setting in the Configuration Manager that is used.
 
G

Guest

Thanks.. think that's it.

Only problem is that for the Release profile, it won't let me select
"Release" configuration for one of projects in the solution - the only option
is debug.

Any idea why?!
 
S

Steven Cheng[MSFT]

Hi Dan,

As for the following behavior you mentioned:

============
it won't let me select
"Release" configuration for one of projects in the solution - the only
option
is debug.
============

this is due to the new dynamic compilation model of ASP.NET 2.0
application, and as for ASP.NET 2.0 web project, it is different from
original ASP.NET 1.X/VS 2003 web project, there is no prebuilt project
assembly, all the stuffs are dynamically get compiled. So the debug/release
is depend on the web.config file's <compilation debug=xxx> setting. The
following blog article has a detailed description on the new compilation
behavior:

#Debug and Release Builds in ASP.NET 2.0
http://odetocode.com/Blogs/scott/archive/2005/11/15/2464.aspx

Also, for ASP.NET 2.0 application, we can precompile the whole site, and
the ASP.NET dev team has provided a web deployment project that can help
control the precompilation more convenient which include specifying the
release/debug precompilation setting:


#Visual Studio 2005 Web Deployment Projects
http://msdn.microsoft.com/asp.net/reference/infrastructure/wdp/

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Hi Steven,

That's the third time I've read that blog post :eek:) Unfortunately, I'm still
no wiser.

Reading that, it looks like I can't get a release build of my code from
VS2005 unless I use the Publish command - is that right?

If that's the case, that's fine - but I'm getting "Ambiguous match" problems
when I do that. Is there no way to make a Release build without Publish or
WDP?

Thanks for your time (and patience!)


Dan
 
S

Steven Cheng[MSFT]

Thanks for your response Dan,

Actually the fixed "Debug" option in the VS 2005 SOLUTION for ASP.NET
project is just a dummy setting which won't make actual effect. That just
tell us that for ASP.NET 2.0 project, there is no concept of development
time prebuilt project assemblies. And for ASP.NET 2.0 pages and classes in
the App_Code, they will be dynamically compiled, and the debug/release
version just rely on the web.config's <compilation debug=...> setting.

Are you correctly put such case dependent code in page's codebehind for in
custom helper class's file? If in custom helper class, maybe you can
consider arrange them into a separate class library which will be more
convenient to control the debug/release output since class library project
is prebuilt at development time.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Steven,

Thanks for the patience!

Okay, so last first... yeah, I could do that. But the code I'm altering is
really integral to the application, so I don't *really* want to seperate it
out if I can help it.

What's more worrying though, is that whether I compilation debug= to true OR
false in my web.config, I always get the debug version, which would indicate
that it doesn't seem to be making any difference?

I don't actually need the alternate functions, I could just replace before
the final build, but it's the fact that I can't seem to switch it which is
making me persevere!

Any ideas?!
 
S

Steven Cheng[MSFT]

Thanks for your response Dan,

What's the actual behavior do you mean?

=======================
What's more worrying though, is that whether I compilation debug= to true
OR
false in my web.config, I always get the debug version, which would
indicate
that it doesn't seem to be making any difference?
=======================

Based on my local test, (in vs 2005 through file test server), the
conditional compiled block will just match the setting in the web.config's
<compilation debug=xxx> attribute. Also, I've tried set this attribute for
a particular sbu directory in the application directory and that worked(
the pages under that directory conforms to the sub web.config's compilation
setting).

Is there any particular setting or scenario in your application that could
cause the difference?

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top