首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从现有的windows服务启动AspNetCore应用程序

如何从现有的windows服务启动AspNetCore应用程序
EN

Stack Overflow用户
提问于 2018-08-23 23:33:32
回答 2查看 400关注 0票数 1

我有一个现有的windows服务。我们的目标是有一个REST接口来与这个服务通信。我想这可能是一个好主意,只要创建一个ASP.NET核心Web应用程序(见截图),然后在我现有的服务中简单地启动整个程序。然后它们可以共享相同的IOC容器,等等。

然后我向sc create s1 binPath = "pathgoeshere"注册了该服务

当我启动服务(附加到进程以进行调试)时,我在输出窗口中得到一个错误:

代码语言:javascript
复制
Exception thrown: 'System.DllNotFoundException' in Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.dll
The thread 0x5250 has exited with code 0 (0x0).
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
'WindowsService1.exe' (CLR v4.0.30319: WindowsService1.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceProcess.resources\v4.0_4.0.0.0_de_b03f5f7f11d50a3a\System.ServiceProcess.resources.dll'. Module was built without symbols.
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
The thread 0x4fa0 has exited with code 0 (0x0).
Microsoft.AspNetCore.Server.Kestrel: Warning: Unable to bind to http://localhost:5000 on the IPv4 loopback interface: 'Die DLL "libuv": Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E) kann nicht geladen werden.'.
Exception thrown: 'System.DllNotFoundException' in Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.dll
The thread 0x58c4 has exited with code 0 (0x0).
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Microsoft.AspNetCore.Server.Kestrel: Warning: Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Die DLL "libuv": Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E) kann nicht geladen werden.'.
Exception thrown: 'System.IO.IOException' in Microsoft.AspNetCore.Server.Kestrel.Core.dll
Exception thrown: 'System.IO.IOException' in mscorlib.dll
Exception thrown: 'System.IO.IOException' in mscorlib.dll
Exception thrown: 'System.IO.IOException' in mscorlib.dll
Microsoft.AspNetCore.Server.Kestrel: Critical: Unable to start Kestrel.

我把整个例子放在GitHub上:

https://github.com/SuperSludge/aspnetcoreService

有人做过这样的事情吗?我是不是完全走错了路?是否更容易将整个windows服务重写为ASP.NET核心应用程序?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-23 23:43:33

如果你想在你的服务中有一个REST Api,你应该在服务项目中添加WebApi,而不是在不同的项目中。所以,在这种情况下,我不认为您可以使用Asp.Net核心。

使用Owin在服务中启动Rest Api,并在另一个线程中运行服务。

代码语言:javascript
复制
// declare baseUrl in confing file by ex
// Startup is your Startup class
using (WebApp.Start<Startup>(baseUrl))
{
     System.Console.WriteLine($"Listening on {baseUrl}");
     Thread.Sleep(Timeout.Infinite);
}

Imo,您应该有另一个服务来运行这个API,这样,您就可以使用Asp.Net.Core了。

Windows Service执行一些逻辑,而WebApi使用相同的Business Api来干扰相同的Db。

票数 1
EN

Stack Overflow用户

发布于 2018-08-24 06:34:01

代码语言:javascript
复制
Do you need ASP.NET core application or just expose REST endpoints ? I am thinking you are trying to use it for the benefits of the library. If latter is true,   
 I have done a similar thing where we have exposed some REST endpoints hosted in our legacy Windows service through "Microsoft.Owin.Hosting". This looks exactly like the ASP.NET WEPAPI

    1) In your windows service, you can start the WebApp like

           //WEBAPI URL
           private const string WEBAPI_BASEADDRESS = "https://+:{port number}/";
           // declare a Idisposable variable
           private IDisposable webAPI;
           //Use Microsoft Owin's library to start it up
           webAPI = WebApp.Start<Startup>(url: WEBAPI_BASEADDRESS);

    //above "Startup" is a class you would declare in your other class library (see below number 2)


    2) You can create a new C# class library with a class named "Startup"  and use the system.Web.Http "Httpconfiguration" class as below

    /// <summary>
    /// Web API startup method
    /// </summary>
    public class Startup
    {
        private const string TOKEN_URL = "/api/token";
        private const string BASE_URL = "/api/{controller}/{id}";
        // This code configures Web API. The Startup class is specified as a type
        // parameter in the WebApp.Start method.
        public void Configuration(IAppBuilder appBuilder)
        {

            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: BASE_URL,
                defaults: new { id = RouteParameter.Optional }
            );

            appBuilder.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
            {
                AllowInsecureHttp = true,
                TokenEndpointPath = new PathString(TOKEN_URL),
                Provider = new CustomAuthorizationServerProvider()
            });
            appBuilder.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

            //Swagger support
            SwaggerConfig.Register(config);
            //appBuilder.UseCors(CorsOptions.AllowAll);
            appBuilder.UseWebApi(config);
        }
    }

    3) Above code has a CustomAuthorizationProvider where you can declare your own Custom OAuth Authorization and do any kind of authentication you want as below

        public class CustomAuthorizationServerProvider : OAuthAuthorizationServerProvider

    and override the "`public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)`"

4) Then you can just spin up WebAPI controllers
    [Authorize]
    public class XXXController : ApiController
    {
}

Hope this helps . Let me know if something is not clear.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51989398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档