MSBuild Conditional Compilation
Asked Answered
A

2

2

I have a web service class that the rest of the framework depends on to grab its data, but the web service class needs to have different method attributes depending on what environment it's in. For instance...

[SoapDocumentMethodAttribute("https://example",...)]
public string Test()
{
    //doSomething
}

See that "https://example"? That needs to change depending on the environment. AFAIK you can't make that string dynamic for runtime, it has to be compiled that way. So I'm trying to get it so that I have multiple CS files for this web service that have the different attribute URLs hardcoded in them, and MSBuild swaps them on precompile. So I'd have a base "Service.cs" for testing, "Service.cs.production" for the production environment, and so on.

  • Is this the best way to do this, or am I missing something where I can have one CS that handles the environment on its own?
  • To preserve having the same class name and IntelliSense not thinking things are ambiguous, I'm mucking up the file extensions ("Service.cs" versus "Service.cs.production"). Is that the only way to do it?
  • Considering all the above is OK, and I'm compiling against a "Production" configuration, can it compile Service.cs.production instead of Service.cs and everything goes hunky-dorey?

Thanks!

Afterpiece answered 17/11, 2008 at 16:54 Comment(0)
M
4

Could you use conditional comments?

#if TESTING
        [SoapDocumentMethodAttribute(something)]
#else
        [SoapDocumentMethodAttribute(someotherthing)]
#endif

For your test configuration you would define the constant:

<DefineConstants>TESTING</DefineConstants>
Matinee answered 17/11, 2008 at 17:6 Comment(0)
M
3

In conjunction with Defining constants, and using #if directives,

You can also write a custom build task -> Target BeforeBuild, then using the Engine.GlobalEngine.GetLoadedProject("projpath") in to Project object.

Now you can manipulate the properties on the Project object however you want for different environments.

Consider adding Platforms in the configuration, for different environments if you want to.

This may not be the answer you are looking for, but something to consider when you want to fork build based on project environments.

Milkmaid answered 17/11, 2008 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.