ASP.NET虚拟主机作为核心Windows服务 [英] Hosting ASP.NET Core as Windows service

查看:175
本文介绍了ASP.NET虚拟主机作为核心Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RC2得到它有一个为Windows服务中托管的应用程序的支持。我想测试它一个简单的Web API项目(使用.NET Framework 4.6.1)

As I get it in RC2 there's a support for hosting applications within Windows Services. I tried to test it on a simple web api project (using .NET Framework 4.6.1).

下面是我的Program.cs代码:

Here's my Program.cs code:

using System;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.WindowsServices;

namespace WebApplication4
{
  public class Program : ServiceBase
  {
    public static void Main(string[] args)
    {
      if (args.Contains("--windows-service"))
      {
        Run(new Program());
        return;
      }

      var program = new Program();
      program.OnStart(null);
      Console.ReadLine();
      program.OnStop();
    }

    protected override void OnStart(string[] args)
    {
      var host = new WebHostBuilder()
      .UseKestrel()
      .UseContentRoot(Directory.GetCurrentDirectory())      
      .UseStartup<Startup>()
      .Build();

      host.RunAsService();
    }

    protected override void OnStop() {}
  }
}

所有其他的东西基本都是从.NET核心模板(虽然我改变了框架net461和project.json增加了一些依赖)。

All the other stuff are basically from .NET Core template (though I changed framework to net461 and added some dependencies in project.json).

通过其发布后 DOTNET发布创建Windows服务资深大律师创建我能成功地开始我的服务,但我无法达到我的任何控制器(端口没有listeting)的。我认为我做错了什么。

After publishing it with dotnet publish and creating Windows Service with sc create I can succesfully start my service, but I can't reach any of my controllers (ports are not listeting). I assume I'm doing something wrong.

所以我想主要问题是如何使自身托管的Web API和运行它作为Windows服务。所有找到的解决办法不RC2更新后的工作。

So I guess the main question is how to make self hosted web api and run it as Windows Service. All found solutions don't work after RC2 update.

推荐答案

您已经有了几个选择这里 - 使用微软的WebHostService类,继承WebHostService或写你自己的。究其原因,后者是使用微软的实施,我们不能写一个类型参数继承WebHostService一个通用的扩展方法,因为这个类不含参数的构造函数,也可以访问该服务定位器。

You've got a couple of options here - use Microsoft's WebHostService class, inherit WebHostService or write your own. The reason for the latter being that using Microsoft's implementation, we cannot write a generic extension method with a type parameter inheriting WebHostService since this class does not contain a parameterless constructor nor can we access the service locator.

在这个例子中,我将创建一个使用Microsoft.DotNet.Web.targets控制台应用程序,输出一个.exe并经营作为一个MVC应用程序(浏览器,控制器等)。我认为创建控制台应用程序,在.xproj改变目标和修改project.json有适当的发布选项,并复制视图,控制器和标准的.NET核心的Web应用程序模板根目录 - 实在是微不足道。

In this example, I'm going to create a Console Application that uses Microsoft.DotNet.Web.targets, outputs an .exe and operates as a MVC app (Views, Controllers, etc). I assume that creating the Console Application, changing the targets in the .xproj and modifying the project.json to have proper publish options and copying the Views, Controllers and webroot from the standard .NET Core web app template - is trivial.

现在的重要组成部分:


  1. 获取的this 包,确保您在project.json文件框架net451(或更新版本)

  1. Get this package and make sure your framework in project.json file is net451 (or newer)

请确保在入口点内容根已正确设置应用程序.exe文件的发布目录,而且RunAsService()扩展方法被调用。例如:

Make sure the content root in the entry point is properly set to the publish directory of the application .exe file and that the RunAsService() extension method is called. E.g:

public static void Main(string[] args)
{
    var exePath= System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
    var directoryPath = Path.GetDirectoryName(exePath);

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(directoryPath)
        .UseStartup<Startup>()
        .Build();

    if (Debugger.IsAttached || args.Contains("--debug"))
    {
        host.Run();
    }
    else
    {
        host.RunAsService();
    }
}


.exe文件现在可以很容易地使用下面的命令安装

The .exe can now easily be installed using the following command

    sc create MyService binPath = "Full\Path\To\The\Console\file.exe"

在该服务已启动,Web应用程序是主持和成功找到并呈现其意见。

Once the service is started, the web application is hosted and successfully finds and renders its Views.

这种方法的一个主要好处是,这让我们覆盖OnStopping,OnStarting和OnStarted方法。

One major benefit of this approach is that this lets us override the OnStopping, OnStarting and OnStarted methods.

让我们假设以下是继承WebHostService

Let's assume that the following is our custom class that inherits WebHostService

internal class CustomWebHostService : WebHostService
{
    public CustomWebHostService(IWebHost host) : base(host)
    {
    }

    protected override void OnStarting(string[] args)
    {
        // Log
        base.OnStarting(args);
    }

    protected override void OnStarted()
    {
        // More log
        base.OnStarted();
    }

    protected override void OnStopping()
    {
        // Even more log
        base.OnStopping();
    }
}



完成上述步骤后,我们要记下我们的用我们的自定义类运行在主机的扩展方法:

Following the steps above, we have to write down our own extension method that runs the host using our custom class:

public static class CustomWebHostWindowsServiceExtensions
{
    public static void RunAsCustomService(this IWebHost host)
    {
        var webHostService = new CustomWebHostService(host);
        ServiceBase.Run(webHostService);
    }
}

这有待从以前的变化的唯一的行例如切入点到底else语句,它调用适当的扩展方法

The only line that remains to be changed from the previous example of the entry point is the else statement in the end, it has to call the proper extension method

host.RunAsCustomService();



最后,在安装使用相同的步骤与上述服务

Finally, installing the service using the same steps as above.

sc create MyService binPath = "Full\Path\To\The\Console\file.exe"

这篇关于ASP.NET虚拟主机作为核心Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆