John Nicholas
John Nicholas

Reputation: 4836

Azure, working connection-string but says has not been initialized

Playing with azure, have a connection string. When the web app hits the first call that requires hitting the database i get this exception.

An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code

Additional information: The ConnectionString property has not been initialized.

the stack trace is

   at System.Data.SqlClient.SqlConnection.PermissionDemand()
   at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext`1.IsIdentityV1Schema(DbContext db)

Fortunatley its on my constructor:

public SisyphusContext(string connectionString)
            : base(connectionString)
        {
        }

and the connection string in there is valid. I took it, made a console app opened a connection selected a load of data. Interestingly the web app when it started successfully seeded the database presumably using this very connection string. (Well we know it cant of ...)

So my question: What is going on? How do i fix it? Everything i have found suggests the string is wrong, but i have shown this is not the case. I suspect its magicking a connection string from somewhere but i cannto find it (in configs or publish files)

Although that said the seeding uses UserManager and RoleManager, wheras this uses my DbContext. That said i pass my context using the constructor here to build it - so thats probably not useful.

update:

here are the connection strings as part of deployment: found in obj\Release\Package\PackageTmp of the web project.

  <connectionStrings>
    <add name="DefaultConnection"
         connectionString="$(ReplacableToken_DefaultConnection-Web.config Connection String_0)"
         providerName="System.Data.SqlClient" />
    <add name="Sisyphus.Core.Repository.SisyphusContext" connectionString="$(ReplacableToken_Sisyphus.Core.Repository.SisyphusContext-Web.config Connection String_0)"
         providerName="System.Data.SqlClient"/>
    <add name="Sisyphus.Core.Repository.SisyphusContext_DatabasePublish"
         connectionString="$(ReplacableToken_Sisyphus.Core.Repository.SisyphusContext_DatabasePublish-Web.config Connection String_0)" providerName="System.Data.SqlClient"/>
  </connectionStrings>

The thing is the connection string works. Just not in azure.

Upvotes: 2

Views: 2246

Answers (1)

trailmax
trailmax

Reputation: 35126

Is that Azure Web-Site or Web-Service?

In case of Web-Sites, Azure has connection strings in it's own configuration (Configuration page within portal). Check there if connection string is set.

Azure portal configuration page with Connection Strings

You have 2 options here: keep connection string set in Azure config: make sure it is a correct one. Or delete connection strings from Azure entirely and use web.config settings. And if you are using web.config, make sure you transform your connection string correctly when deploying.

If your connection strings are fine in Azure portal, I'm pretty sure they are messed up during deployment. WebDeploy has a tendency to do strange things with connection strings, you need to stop it. So when you deploy, on settings page uncheck "Use this connection string":

Web-deploy connection strings

And once you have deployed to Azure you can check the state of web.config either through Visual Studio 2013 Server Explorer:

VS2013 Server Explorer Web.config

Or through Kudu which you can access via https://<mywebsitename>.scm.azurewebsites.net/DebugConsole and navigate to web.config:

Kudu diagnostic panel

Upvotes: 4

Related Questions