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

不羁岁月 提交于 2019-12-19 16:35:42

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/38443001/connect-to-local-sql-server-instance-when-running-service-fabric-cluster-in-deve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!