从.NET Framework应用程序启动AspNetCore托管

时间:2018-06-07 07:35:07

标签: asp.net .net asp.net-core .net-standard self-hosting

所以我有一个使用Topshelf作为Windows服务运行的.NET 4.6.1应用程序,我还有一个ASP.NET核心Web应用程序(MVC和API),它当前作为服务运行并在使用Microsoft.AspNetCore.HostingMicrosoft.AspNetCore.Hosting.WindowsServices创建自己的流程。我想要实现的是,我可以从.NET应用程序中启动AspNetCore Hosting,所以我只有一个进程。

然而,当我尝试使用此代码时:

bool isService = true;
if(Debugger.IsAttached || args.Contains("--console"))
{
    isService = false;
}

var pathToContentRoot = Directory.GetCurrentDirectory();
if(isService)
{
    var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
    pathToContentRoot = Path.GetDirectoryName(pathToExe);
}

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

host.Run();

我遇到了很多错误。起初我认为这是因为使用了不同的目标.net平台,但即使将两者调整为4.6.1并将asp.net核心数据更新为最新版本,它仍会输出这些错误(这就像1 / 30个原始错误长度):

    fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
          An unhandled exception has occurred while executing the request.
    Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred:
    imlmfapg.yxl(4,41): error CS0234: The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)
    imlmfapg.yxl(5,62): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
    imlmfapg.yxl(4,82): error CS0518: Predefined type 'System.Type' is not defined or imported
    imlmfapg.yxl(4,118): error CS0518: Predefined type 'System.String' is not defined or imported
    imlmfapg.yxl(4,135): error CS0518: Predefined type 'System.String' is not defined or imported
    imlmfapg.yxl(5,81): error CS0518: Predefined type 'System.String' is not defined or imported
    imlmfapg.yxl(5,109): error CS0518: Predefined type 'System.Type' is not defined or imported
    imlmfapg.yxl(5,11): error CS0518: Predefined type 'System.Void' is not defined or imported
    imlmfapg.yxl(9,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
    imlmfapg.yxl(10,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
    imlmfapg.yxl(11,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
    imlmfapg.yxl(12,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
    imlmfapg.yxl(15,36): error CS0234: The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
    imlmfapg.yxl(26,35): error CS0234: The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)
    imlmfapg.yxl(27,35): error CS0234: The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)

更新:

因此,对于每个尝试做我所做的事情的人来说,通过在控制台项目中托管Web项目(都针对.NET FW 4.6.1)。看起来这不起作用的原因是在编译时,当ASP尝试使用razor渲染页面时,它无法以正确的方式解析命名空间,因此找不到合适的DLL来使用。另一种方式很好。通过简单地运行aspnet核心项目的其他功能,使用控制台和自托管。

1 个答案:

答案 0 :(得分:0)

也许尝试在web.config中添加netstandard引用,如下所示:

<system.web> 
<compilation debug="true" targetFramework="4.7.1" > <assemblies> <add `assembly="netstandard, Version=2.0.0.0, Culture=neutral,` PublicKeyToken=cc7b13ffcd2ddd51"/> </assemblies> </compilation> <httpRuntime targetFramework="4.7.1" />