2

I'm trying to use a Windows Service to host a simple ASP.Net Core web app targeting .Net core 2.x, as detailed here by Microsoft.

This should be simple with the docs, but I'm getting nowhere because the Microsoft.AspNetCore.Hosting.WindowsServices package doesn't seem to work with any version of .Net Core (the NuGet page says it depends on .Net Standard). Simply opening a project will cause Visual Studio to get stuck at NuGet restore, without any error message whatsoever (I have to kill the VS process manually, as closing VS would cause it to stop responding). Running dotnet restore from command will cause cmd gets stuck at "Restoring packages for XXX".

The same thing happens with the sample code in ASP.Net official docs. I guess this must mean a machine- or platform-specific issue to me but I have tried with various (fresh) VMs and am out of ideas. The only thing that works so far is this answer here, i.e targeting .Net Framework and explicitly list all the package references rather than using Microsoft.AspNetCore.All.

I'm using VS2017 15.7.6. Any help on this issue would be greatly appreciated!

Update

The problem magically disappeared after I installed the Azure development workload in VS2017. I already had ASP.Net and .Net Core workloads before, so I really can't figure out which individual component did the trick, but it did solve the problem.

scharnyw
  • 2,347
  • 2
  • 18
  • 32

2 Answers2

3

Yes, this answer is correct. The windows-service package only runs on windows. You have to directly target each package you need, because the Microsoft.AspNetCore.App (.NET-Core >= 2.1) and Microsoft.AspNetCore.All (.NET-Core >= 1.1 <= 2.0) packages, contain package(s) that target .NET-Core instead of .NET-Standard.

You could target both frame-works using

<TargetFrameworks>net471;netcoreapp2.1</TargetFrameworks>

and have conditional includes in your project file + compiler directives in your c# code.

<!-- CSPROJ CONDITIONAL REFERENCE -->
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="2.1.1" Condition="'$(TargetFramework)' == 'net471'" />

// c#-code compiler directive
#if net471
using Microsoft.AspNetCore.Hosting.WindowsServices;
#endif

Keep in mind that using the full .NET-Framework will make you lose the performance improvements introduced with .NET-Core.

I am sure you could also have a batch script / windows task that starts your .NET-Core service each time your windows machine restarts.

alsami
  • 8,996
  • 3
  • 25
  • 36
  • Thanks a lot! It does seem strange though that MS’s own docs and sample code targeting this specific issue doesn’t work in this case. – scharnyw Aug 14 '18 at 01:02
0

I just had a similar problem. We have multiple Nuget package stores, and one of them was experiencing certificate issues.

I fixed it by doing the following:

  1. Right-click on project Dependencies in Solution Explorer, and click Manage Nuget packages.

  2. In the Package Manager, in the Installed tab, you should see the package, select the Microsoft.AspNetCore.Hosting.WindowsServices package. It will be showing a version 0 and some indication that it is failing to download.

  3. You may also see the Package source in the top-right pointing to one of the custom Nuget stores. Change the Package Source to ALL. That will cause the window to refresh, and then you may see the correct version and an Update button.

  4. Now click the Update button to update. My version is now 2.1.1.

Looking at the project file now, it changed the package reference to:

<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="2.1.1" />
Martin Lottering
  • 1,624
  • 19
  • 31