Mixing debug and non debug mode during execution - race condition?
<p>I have an application that connects to different databases depending on the build configuration. In #DEBUG I access the test database and in #RELEASE I access the productive system. This usually works as expected but sometimes I appear to receive test data though being on #RELEASE or the other way.</p>

<p>The application uses a WCF connection to connect to a Web Service that handles the database access. If the program is built with #DEBUG the WCF connection uses different authentication data to distinguish between the modes.</p>

<pre><code>public static DBUpdaterClient CreateWebServiceInstance(ApplicationType appType)
{
    ServicePointManager.ServerCertificateValidationCallback += EasyCertCheck;

    switch (appType)
    {
        case ApplicationType.xxx:
            return new DBUpdaterClient
            {
                ClientCredentials =
                {
                    UserName =
                    {
#if DEBUG
                        UserName = "xxx_test",
                        Password = "xxx"
#else
                        UserName = "xxx",
                        Password = "xxx"
#endif
                    }
                }
            };
        ...
     }
     ...
}
</code></pre>

<p>The Web Service identifies the "test" part within the user name (or not) and establishes a database connection.</p>

<pre><code>public override void Validate(string userName, string password)
{
    switch (userName)
    {
        case "xxx":
            Helpers.CurrentUserCredentials = new WebServiceCredentials(ApplicationType.xxx, false);
        case "xxx_test":
            Helpers.CurrentUserCredentials = new WebServiceCredentials(ApplicationType.xxx, true);
                break;
        ...
    }
    ...
}
</code></pre>

<p>Depending on the WebServiceCredential object the connection data is retrieved from Web.config via the ConfigurationManager class.</p>

<pre><code>public DBSettings(WebServiceCredentials userCredentials)
{
    switch (userCredentials.ApplicationType)
    {
        case ApplicationType.XXX:
                XXXConnectionString = (userCredentials.IsTestMode)
                    ? ConfigurationManager.ConnectionStrings["XXXTest"].ConnectionString
                    : ConfigurationManager.ConnectionStrings["XXXProd"].ConnectionString;
        ...
    }
    ...
}
</code></pre>

<p>In most cases this works (let's say about 99%). But sometimes a connection to the wrong database is established and wrong data is retrieved or written (much worse).</p>

<p>The application itself does not use any tasks or background operations. But the application is used by about 10 users at the same time. The WCF connection is configured as <code>InstanceContextMode.PerCall</code>. I think the error must be on the client side somewhere but I have no steps to reproduce the problem. It just happens.</p>

<p>I am thankful for every little hint. </p>