在dnxcore50上为控制台应用程序创建自定义托管服务 [英] Create Custom Hosting Service for console app on dnxcore50
问题描述
我创建了一个控制台应用程序,可以按计划的时间发送电子邮件.我正在创建Windows服务来执行此操作.但是Windows服务在.netcore中不可用.我可以知道解决方案吗?
I have created a console app to send email on scheduled time. I am creating a windows service to do this. But windowsservices are not available in .netcore. Can I know the solution for this.
在继承ServiceBase时出现错误,因为dnxcore50中没有System.ServiceProcess.我无法使用net451或其他框架,因为我的应用程序在生产中使用.netCore,因此我只使用了此dnxcore50.
I am getting an error while inheriting ServiceBase, as there is no System.ServiceProcess in dnxcore50. I can't user net451 or other framework as my application uses .netCore in production so I have use only this dnxcore50.
下面是我主要方法中的代码
Below is the code in my main method
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(contentRootPath)
.Build();
if(Debugger.IsAttached || args.Contains(-debug")))
{
host.Run();
}
其他
{
host.RunAsCustomService();
}
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(contentRootPath)
.Build();
if (Debugger.IsAttached || args.Contains("--debug"))
{
host.Run();
}
else
{
host.RunAsCustomService();
}
}
下面是我的project.json文件
Below is my project.json file
{
版本":"1.0.0- *",
"dependencies":{
"Microsoft.AspNetCore.Http.Abstractions":"1.0.0&",
"NETStandard.Library":"1.6.0&",
"Newtonsoft.Json":"9.0.1",
"Microsoft.Extensions.DependencyInjection":"1.0.0&",
"Microsoft.Extensions.Logging.Console":"1.0.0&",
"Microsoft.Extensions.Logging.Debug":"1.0.0&",
"Microsoft.Extensions.PlatformAbstractions":"1.0.0&",
"Bz.Business.Manager":"1.0.0- *",
"Bz.Business.Models":"1.0.0- *",
"Bz.Core.Repository":"1.0.0- *",
"Serilog":"2.1.0&",
"Serilog.Extensions.Logging":"1.1.0&",
"Serilog.Sinks.File":"2.1.0&",
"Serilog.Sinks.Literate":"2.1.0-dev-00031",
"Serilog.Sinks.RollingFile":"2.1.0&",
"Microsoft.Extensions.Configuration":"1.0.0&",
"Microsoft.Extensions.Configuration.FileExtensions":"1.0.0&",
"Microsoft.Extensions.Configuration.Json":"1.0.0&",
"Microsoft.Extensions.Configuration.EnvironmentVariables":"1.0.0&",
"Microsoft.AspNetCore.Hosting":"1.0.0&",
"Microsoft.AspNetCore.Server.Kestrel":"1.0.0&",
"Microsoft.AspNetCore.Hosting.Abstractions":"1.0.0&",
"Microsoft.Framework.Runtime.Interfaces":"1.0.0-beta4"
},
框架":{
"netstandard1.6":{
进口":"dnxcore50"
}
}
}
{
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNetCore.Http.Abstractions": "1.0.0",
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1",
"Microsoft.Extensions.DependencyInjection": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.PlatformAbstractions": "1.0.0",
"Bz.Business.Manager": "1.0.0-*",
"Bz.Business.Models": "1.0.0-*",
"Bz.Core.Repository": "1.0.0-*",
"Serilog": "2.1.0",
"Serilog.Extensions.Logging": "1.1.0",
"Serilog.Sinks.File": "2.1.0",
"Serilog.Sinks.Literate": "2.1.0-dev-00031",
"Serilog.Sinks.RollingFile": "2.1.0",
"Microsoft.Extensions.Configuration": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.AspNetCore.Hosting": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.Hosting.Abstractions": "1.0.0",
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-beta4"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
下面是CustomWebHostService.cs
Below is the CustomWebHostService.cs
下面是错误
推荐答案
Hi Ramya gollapudi,
谢谢您在这里发布.
根据您的代码.我认为您的问题与ASP.NET论坛有关.
According to your code. I think your issue is related to ASP.NET forum.
您能告诉我们您的项目类型是什么吗?这是一个ASP.NET Web服务?如果是,我建议您重新发布
Could you tell us what type of your project? it is a ASP.NET webservices? if yes, I would suggest you repost
此处.
根据我的搜索.我找到了 类似的解决方案.
Based on my search. I find a similar solution.
您在这里有两个选择-使用Microsoft的WebHostService类,继承WebHostService或编写您自己的.
You've got a couple of options here - use Microsoft's WebHostService class, inherit WebHostService or write your own.
我希望这些信息对您有所帮助.
I hope the information is helpful to you.
最好的问候,
Hart
这篇关于在dnxcore50上为控制台应用程序创建自定义托管服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!