0

I'm trying to configure an ASP.NET Core (2.1) application in the same IIS server with two different url/port, each url/port should use different DataBase connection e.g.:

  1. http://localhost:55001 (use QA DB connection)
  2. http://localhost:55002 (use Production DB connection)

This scenario is for using an intranet web application. A group of users will use the Production app. Another group of users will use the QA or Staging app. This two urls/db-connections must be enable in the same application at same time in order to have one main app (for policies, maintaining and machine resources consumption purposes) and two different databases.

What I've tried is to configure two appsettings.json files: the first (appsettings.Release.json) sets the production database connection string and a applicationUrl url/port. The second one (appsettings.QA.json) defines a different database connection string and different applicationUrl.

appsettings.Release.json:

{
  "ConnectionStrings": {
    "default": {
      "ConnectionString": "<production-connection-string>",
      "ProviderName": "Oracle.ManagedDataAccess.Client"
    }
  },
"applicationUrl": "https://localhost:55001",
...
}

appsettings.QA.json:

{
  "ConnectionStrings": {
    "default": {
      "ConnectionString": "<qa-connection-string>",
      "ProviderName": "Oracle.ManagedDataAccess.Client"
    }
  },
"applicationUrl": "https://localhost:55002",
...
}

Now here I'm a little bit lost, the next configuration settings I need to achieve are confusing and not clear for me. I don't even know if what I'm trying to achieve is possible or acceptable. If I define an ASPNETCORE_ENVIRONMENT variable value for each appsettings[environment/scenario].json then I cannot use two values for this variable at the same time right?.

So I'm thinking that set and use the ASPNETCORE_ENVIRONMENT may not be the answear to my problem. Also I have to properly configure the startup and (maybe) program classes to achieve what I'm looking for.

So well at this point, any advice, guidance, code snippet or solution you guys may have will be very appreciated.

1 Answer 1

0

I think your question basically boils down to how to set different values for ASPNETCORE_ENVIRONMENT per app instance. Pretty much your best bet there for an IIS deployment is adding an <environmentVariables> section in <aspNetCore> in your web.config:

<aspNetCore ...>
    <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
    </environmentVariables>
</aspNetCore>

To have that handled automatically for you when publishing, you'll need to modify the .pubxml for your publishing profile and add something like:

<PropertyGroup>
    <EnvironmentName>Staging</EnvironmentName>
</PropertyGroup>

That will then add the relevant <environmentVariables> setup to the generated web.config automatically when publishing.

For more information, see: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/aspnet-core-module?view=aspnetcore-2.2#configuration-with-webconfig

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Chris, I was able to configure the app with your mentioned solution, I can see that it uses the connection string depending on the environment. The only problem left, is that I cannot configure the custom url for each designed environments, I'm guessing I will be unable to do it because I'm hosting the app in IIS and that url config resides in IIS, or I haven't found the right documentation to acheive it.
Yeah. That part is unavoidable. The site bindings are in IIS.
I thought this would help me. The question was about different appsettings.json files. But I was surprised to find an answer about web.config. Have no idea how that happened. Wonder which web.config this even was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.