Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to local SQL Server instance when running Service Fabric cluster in development environment

When opening a sql connection to a local database in a traditional console app I have no issues, however when I attempt to do the same thing within a stateless service running in Service Fabric I get a login error.

The error I receive is "Login failed for user 'WORKGROUP\\NICK$'."

And this is the code I'm using to connect

using (var con = new SqlConnection("Server=.;Trusted_Connection=True;Database=AddressBook"))
{
            try
            {
                con.Open();
            }
            catch (Exception e)
            {

            }
}

When I try to add that user to sql server it tells me that the user cannot be found.

like image 404
The Muffin Man Avatar asked Jul 18 '16 17:07

The Muffin Man


People also ask

How do I connect to a SQL Server managed instance?

Connect with SSMSOn the on-premises client computer, open SQL Server Management Studio. In the Connect to Server dialog box, enter the fully qualified host name for your managed instance in the Server name box. Select SQL Server Authentication, provide your username and password, and then select Connect.

How do you set up a service in fabric local cluster?

To set up a local cluster, press Start and type Service Fabric Local Cluster Manager and press Enter. From system tray right click to the Service Fabric icon and navigate to Setup Local Cluster → 1 Node. Wait few seconds until you see a notification Service Fabric Local Cluster Manager setup completed successfully.

Can't connect to Azure SQL managed instance?

If you are unable to connect to SQL Managed Instance from an Azure virtual machine within the same virtual network but a different subnet, check if you have a Network Security Group set on VM subnet that might be blocking access.

What type of services are available in Visual Studio while creating service fabric application?

The Service Fabric SDK includes an add-in for Visual Studio that provides templates and tools for creating, deploying, and debugging Service Fabric applications. This topic walks you through the process of creating your first application in Visual Studio.


2 Answers

A comment to the accepted answer mentions running your service as a different user account. Here's how to do that. In your ApplicationManifest.xml file, insert this:

<Principals>
  <Users>
    <User Name="AcctToUse" AccountType="DomainUser"
          AccountName="MyDomain\MyUsername" Password="MyPassword" />
  </Users>
</Principals>
<Policies>
  <DefaultRunAsPolicy UserRef="AcctToUse" />
</Policies>

Edit: I didn't make it clear, AcctToUse is just a string that you make up. It's a key that points to the User. The AccountName field on the other hand is the username.

like image 126
user2023861 Avatar answered Sep 20 '22 17:09

user2023861


Based on the comments above I learned that Service Fabric is running under the NETWORK SERVICE account.

The solution is to update the User Mapping and role membership for the databases that you want to access within the SF cluster.

In SSMS expand Security, Logins, right click NETWORK SERVICE and then choose properties. Under User Mapping place a checkbox next to each Database that you want to expose access to and down below public, db_datareader/writer.

enter image description here

like image 35
The Muffin Man Avatar answered Sep 19 '22 17:09

The Muffin Man