Recover and use Windows Azure connection strings?

I configured the connection strings in the Azure management portal Configure-> Connection Strings (related resources):

enter image description here

What are these connection strings for?

I tried to remove conn. lines from the web.config file, so it should read here, but it is not.

Is there another way?

Basically, I want these connection strings to overlap the connection strings in web.config for use in a production environment.

I added the following to the Application_Start method:

 var sb = new StringBuilder(); var appConfig = ConfigurationManager.OpenMachineConfiguration(); foreach (ConnectionStringSettings conStr in appConfig.ConnectionStrings.ConnectionStrings) sb.AppendFormat("Name: {0}, ConnectionString: {1}\n", conStr.Name, conStr.ConnectionString); throw new Exception(sb.ToString()); 

Here is the result:

Name: LocalSqlServer, ConnectionString: data source =. \ SQLEXPRESS; Integrated Security = SSPI; AttachDBFilename = | DataDirectory | aspnetdb.mdf; User Instance = true

I tried the above using ConfigurationManager.ConnectionStrings , but the connection string to the server (Azure) was not there.

+6
source share
2 answers

Connection strings in Portal allow you to override the connection strings defined in web.config .

When you develop locally, you are probably using a database located at localhost \ SQLExpress or something similar. If you deploy without configuring web.config, this means that your website running on Windows Azure will still point to localhost \ SQLExpress, which you do not need.

Connection strings Portal allows you to override existing connection strings that are already defined in web.config. If your web.config does not contain a connection string with the same name as the one configured on the portal, it will not be added and will be available at runtime. Perhaps this is the problem you are facing.

To fix this, simply add the connection string to the web.config file with the same name as the one you already added to the portal.

Update: As I explained in the comment, Windows Azure websites do not physically modify the web.config ( source ) file, it does this at runtime. Therefore, to check which AppSettings and ConnectionStrings are really available at run time, try the following:

Controller:

 public ActionResult Index() { ViewBag.ConnectionStrings = ConfigurationManager.ConnectionStrings.Cast<ConnectionStringSettings>(); ViewBag.AppSettings = ConfigurationManager.AppSettings; return View(); } 

View:

 <h3>ConnectionStrings:</h3> <ul> @foreach (var setting in ViewBag.ConnectionStrings) { <li>@setting.Name: @setting.ConnectionString</li> } </ul> <h3>AppSettings:</h3> <ul> @foreach (var s in ViewBag.AppSettings) { <li>@setting: @System.Configuration.ConfigurationManager.AppSettings[s]</li> } </ul> 
+11
source

Two days later, I finally managed to get it to work. I am adding my solution here, hoping this can help someone in the future.

Environment

  • Azure API (but theoretically, it should also work for other types of projects)
  • SQL DB (hosted on Azure). Obviously, any db
  • EF 6.0 - First Database Approach
  • The connection string is stored in Azure under โ€œApplication Settings โ†’ Connection Strings.โ€ Image shown below

Snapshot from Azure -> Api App -> Application Settings -> Connection Strings

What I wanted to do I wanted to put the connection string on Azure and, as @Sandrino Di Mattia mentioned, dynamically retrieve it from there.

Steps that worked for me

  • Create connectionStrings in web.config

`

 <connectionStrings> <add name="dbConnectionStringForAzure" connectionString="Local Connection String" providerName="System.Data.EntityClient"/> </connectionStrings> 

`

Note that the provider name is System.Data.EntityClient, not System.Data.SqlClient.

Additional bit: In addition, after publishing the project, you can check that the connection string is in the web.config file. Go to projectorapiurl.scm.azurewebsites.net.

Go to Menu -> Debug Console -> PowerShell -> Edit Web.config file. (There are other ways to get web.config files. Use the one you prefer.)

Note. Here you can find another line of generated Azure autorun. It is safe to remove as we are not using it.

  1. Go to Azure -> your project and application settings. Add the data as shown below:

    Name = dbConnectionStringForAzure

    Value = Provider=System.Data.SqlClient; metadata=res://*/csdlModel.csdl|res://*/ssdlModel.ssdl|res://*/mslModel.msl; Provider Connection String ='Data Source=server.database.windows.net,1433;initial catalog=database;User ID=username;Password=password;MultipleActiveResultSets=True;App=EntityFramework;' Provider=System.Data.SqlClient; metadata=res://*/csdlModel.csdl|res://*/ssdlModel.ssdl|res://*/mslModel.msl; Provider Connection String ='Data Source=server.database.windows.net,1433;initial catalog=database;User ID=username;Password=password;MultipleActiveResultSets=True;App=EntityFramework;'

  2. Before the third drop-down menu, select CUSTOM. It is important that Azure adds System.Data.SqlClient (or any other provider, depending on which one is selected) in the provider name of our connection string, which we do not need.

  3. Save

At this point, Azure should use this connection string at runtime. Want to check it out!? similar to what @Sandrino Di Mattia suggested above or in this post SO @Shaun Luttin Get the Azure SQL Database Connection String that is linked to the Windows Azure website with C # .NET .

Alternatively, put the code below in any razor template:

  <p>Value of dbConnectionStringForAzure : @System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionStringForAzure"].ConnectionString </p> 

On the other hand, I set the connection string name to my DbContext constructor.

  public MyEntities() : base("name=dbConnectionStringForAzure") { } 

Now that I called my API, it dynamically used the connection stored in Azure.

Thanks to dozens of messages and additional snapshots of coffee!

+2
source

All Articles