简体   繁体   中英

SQL Login issue from .Net 5 Windows service but not from console installed via Wix installer

I have a.Net 5 Windows service (WindowsBackgroundService) that connects to a local SQL Express database instance. This database instance and application are meant to run on the local system, so I was trying to do Windows Authentication for SQL and not set up a login.

The service is also installed using the Wix 3.11 installer. The service installs just fine and completes (it is also listed in my services and add/remove programs) but the service will not start. In the Event Viewer logs, this is what it says:

Login failed for user 'NT AUTHORITY\SYSTEM'. Reason: Failed to open the explicitly specified database 'MyDatabase'. [CLIENT: ]

Now, I understand that error points to a login issue in general but what I am confused by is I am using Windows Authentication as I mentioned but even more confusing, when I run the executable in the installed folder, it runs fine and connects to my SQL database.

I guess this points to the permissions of the service as Wix installed it and those permissions are not good enough to satisfy Windows Auth for SQL?

So, I guess the question is, how do I configure Wix to install the service (hopefully without requiring the user to supply credentials during the installation) so that it can access SQL Express using Windows Auth? Here is how the service looks after it is installed:

服务登录

And here is the pertinent section of the Wix.wxs config:

          <ServiceInstall
            Id="ServiceInstaller"
            Type="ownProcess"
            Name="MyService"
            Vital="yes"
            DisplayName="My Test Service"
            Description="My Test Service"
            Start="auto"
            ErrorControl="normal"
            Account="LocalSystem"
            Interactive="no" >
            <util:ServiceConfig
              FirstFailureActionType="restart"
              SecondFailureActionType="restart"
              ThirdFailureActionType="restart"
              ResetPeriodInDays="1"
              RestartServiceDelayInSeconds="30" />
            <util:PermissionEx 
              User="Everyone" 
              ServicePauseContinue="yes" 
              ServiceQueryStatus="yes" 
              ServiceStart="yes" 
              ServiceStop="yes" 
              ServiceUserDefinedControl="yes" />
          </ServiceInstall>
            <ServiceControl Id="StartService" Start="install" Stop="both" Remove="uninstall" Name="MyService" Wait="yes" />

Thanks!!

After doing more and more digging (ie: different ways of searching the same issue) I found this SO post:

Add login to SQL

And it fixed my issue. The service now runs.

Problem solved!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM