-2
   #if DEBUG
                        mailMessage = new MailMessage("someEmail.com", "someEmail.com", subject, messsageBody);
   #else
                        mailMessage = new MailMessage(Membership.GetUser().Email, cppAccount.Email, subject, messsageBody);
   #endif

I have the above code but even when I publish on release it still uses the debug statement. This is an mvc4 c# project and this code is in one of my model classes.

phoog
  • 42,068
  • 6
  • 79
  • 117
user2117229
  • 135
  • 3
  • 8

2 Answers2

2

That is not the best way to do what you are trying. You should do this in web.config with an app setting for example.

<add key="MailMessageEnabled" value="true" />

And then in your cs file do something like -

bool isEnabled = bool.Parse(ConfigurationManager.AppSettings["MailMessageEnabled"]);
mailMessage = isEnabled ? new MailMessage(Membership.GetUser().Email, cppAccount.Email, subject, messsageBody) 
            : new MailMessage("someEmail.com", "someEmail.com", subject, messsageBody);

And you can have the value of the config to be true in default web config and false in release web config

MoXplod
  • 3,813
  • 6
  • 36
  • 46
  • So basically what your saying unlike vb.net the compilier directives don't work very well. Or is this just because I can modify the setting from outside the deployed dll? – user2117229 Jun 09 '14 at 16:43
  • 1
    Compiler directives work, but IMO its more flexibility to have the control from outside the DLL than having to recompile everytime you have to test, If you do it this way you can quickly test ON vs OFF by changing the value. – MoXplod Jun 09 '14 at 16:59
1

As MoXplod says, you should do that in the web.config file BUT I think it is better to use Transformation so you can have your web.config file for debug with the value that you require for development and in web.Release.config you can have the values that you need in PROD environment. Check http://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx for information about Transformation.

rafail
  • 11
  • 1
  • If I do it in the webconfig it would be done with a transform any way. Like have done before with live and dev apps. Thanks. – user2117229 Jun 09 '14 at 16:41