Search code examples
c#wpfurl

How to make url parameter server agnostic in source code?


I have a desktop WPF app with .NET which has 3 versions.

MyAppDev

MyAppPreProduction

MyAppProduction

First 2 versions are used by developers to test changes. Last one is production version which is used to distribute users.

MyAppDev connects to development server via url=devServerUrl in constants.cs in source code for developers to test.

MyAppPreProduction connects to preproduction server via url=preprodServerUrl in constants.cs in source code for developers to test before production.

MyAppProduction connects to production server via url=prodServerUrl in constants.cs in source code.

The problem is developers sometimes forget to change this url parameter and production url stays in development app. (MyAppDev) So developer assumes he is testing on development server but in fact he is connected to production server and changes valuable production data. This causes problems later for us because we have to correct production data later.

What I want to ask is, is it possible to define only one url constant in source code for all 3 app's repository? (So when developer forgets to change the url parameter, it will still connect to correct url.)

different


Solution

  • I may not really understand what you’re trying to do or what you mean by “versions” or “agnostic”, but the simplest way to do this would be to use the RELEASE and DEBUG symbols that come predefined with Visual Studio’s default project templates. So they’re probably already there. You can add a PREPROD build configuration and put this somewhere in your code (constants.cs):

    #if RELEASE
        url = "https://prod";
    #elif PREPROD
        url = "https://preprod";
    #else
        url = "https://dev";
    #endif
    

    Now devs can switch in the VS toolbar before compiling:

    [Switching build configs in Visual Studio

    When publishing via Visual Studio or your build server, configure it so the Release build config is used (this is the default).

    Now unless you’re shipping from \bin\Debug\, the production version will always have the production url.


    It sounds like you’re just hardcoding constant strings, which is what my code above shows. You may want to load such config values from a config file instead. You could use the same code above to switch between config.json and config.debug.json, or handle that some other way.