Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

host.RunAsService() not found #1338

Closed
johnsrude opened this issue Feb 28, 2018 · 6 comments
Closed

host.RunAsService() not found #1338

johnsrude opened this issue Feb 28, 2018 · 6 comments

Comments

@johnsrude
Copy link

johnsrude commented Feb 28, 2018

I am setting up a webserver for internal use using ASP.Net Core 2.x so I want to run it as a service.

I am following the instructions Host an ASP.NET Core app in a Windows Service:

public static void Main(string[] args)
{
    var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
    var pathToContentRoot = Path.GetDirectoryName(pathToExe);

    var host = WebHost.CreateDefaultBuilder(args)
        .UseContentRoot(pathToContentRoot)
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .Build();

    host.RunAsService();  <------- NOT FOUND
}

The problem is that RunAsService is undefined. I have with the same code been able to run host.Run() though.

Here is the project file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <StartupObject></StartupObject>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.0.1" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
  </ItemGroup>
</Project>
@Tratcher
Copy link
Member

Do you have a Net Core app or a Net4x app? RunAsService is only available for Net4x.

@johnsrude
Copy link
Author

It's Net Core. Do I have to go through the whole reverse proxy thing to get this to work, then?

@Tratcher
Copy link
Member

@johnsrude reverse proxies aren't involved here, the components needed for windows service support haven't been ported to .NET Core yet. You can use ASP.NET Core in a console application targeting .NET Framework 4.x to get windows service support.

You could also try 2.1.0-preview1, .NET Core support has been added there. #1189

@Drawaes
Copy link

Drawaes commented Feb 28, 2018

Or you can use the excellent package by @dasMulli that provides windows services in .net core ;)

@dergroncki
Copy link

Change the target framework to <TargetFramework>net461</TargetFramework> in the csproj-file of your application.

You need to install the NuGet package Microsoft.AspNetCore.Hosting.WindowsServices.

Add the following using statement to program.cs: using Microsoft.AspNetCore.Hosting.WindowsServices;

After this the methode "RunAsService()" will be available:

image

@muratg
Copy link

muratg commented Apr 20, 2018

@johnsrude If you'd like to use this on .NET Core, you'll have to move to 2.1.0-preview2.

We decided not to bring this back to 2.0.x (#1229)

@muratg muratg closed this as completed Apr 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants