Question

I am struggling to make the .Net backend of Mobile Services use the correct connectionString. When I publish the service I select the correct connection string for "MS_TableConnectionString". If I check the web.config on the server (via FTP) I see what I would expect:

web.config on server:

 <connectionStrings>

<add name="MS_TableConnectionString" connectionString="Data Source=tcp:[ServerAddress].database.windows.net,1433;Initial Catalog=[MyMobileService_db];Integrated Security=False;User ID=[correctUserName];Password=[CorrectPassword];Connect Timeout=30;Encrypt=True" providerName="System.Data.SqlClient" />

In my context it is configured to use a connection string called MS_TableConnectionString:

    private const string connectionStringName = "Name=MS_TableConnectionString";

    public MyMobileServiceContext() : base(connectionStringName)
    {
        Schema = "MyMobileService";
    } 

To see what connection string is actually being used I added this to an example controller:

Example Client Code:

public class ExampleController : ApiController
{

    MyMobileServiceContext context;

    public ApiServices ApiServices { get; set; }

    public ExampleController()
    {
        context = new MyMobileServiceContext();
    }


    public async Task<IHttpActionResult> PostExample(ExampleItem item)
    {
        ApiServices.Log.Warn("ConnectionString: " + context.Database.Connection.ConnectionString);
       ...

    }

And when I look at the Log Entry on Mobile Services I see a different UserName and Password:

[2014-04-15T12:26:33.1410580Z] Level=Warn, Kind=Trace, Category='PostExampleItem', Id=00000000-0000-0000-0000-000000000000, Message='ConnectionString: Data Source=[SameServerAddress].database.windows.net;Initial Catalog=[SameDatabaseName];User ID=[DifferentUserName];Password=[DifferentPassword];Asynchronous Processing=True;TrustServerCertificate=False;'

The different username and password are the same as I see in the original .PublishSettings file that I downloaded under the name of SQLServerDBConnectionString but I have no idea where this is stored on the server?

Because of the different username and password I see the following exception in the log:

[2014-04-15T13:18:11.2007511Z] Level=Error, Kind=Trace, Category='App.Request', Id=d7ec6d25-f3b7-4e88-9024-217be40ae77f, Exception=System.Data.Entity.Core.ProviderIncompatibleException: An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure. ---> System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException: Cannot open database "master" requested by the login. The login failed. Login failed for user '[DifferentUserName]'. This session has been assigned a tracing ID of '[GUID]'. Provide this tracing ID to customer support when you need assistance.

Any help would be much appreciated as at the moment I am having to hard code the whole connection string in the constructor of the Context to make it work.

Thanks

F

UPDATE: 15th April 2014 15:23

I deleted all my publisher profiles and created a copy of the original .PublishSettings file. From this I deleted all but one profile. I then deleted the SQLDBConnectionString attribute to confirm that it is not because I was sending this that was causing the problem. The result was no change, it is still using the DifferentUserName and Password so it must be reading it from the server somewhere.

Was it helpful?

Solution

We have a hole at the moment in that we pick up the connection string from the portal yet don't expose the ability to set or modify connection strings there.

The work-around is to set an application setting in the portal and then use that in your code using the ApiServices class, something like this (in your controller)

    string connectionString = this.Services.Settings["YourConnectionStringAsAppSetting"];

I know it is confusing... we'll make it easier to access and modify the connection strings.

Henrik

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top