【问题标题】:Starting AspNetCore hosting from .NET Framework application从 .NET Framework 应用程序启动 AspNetCore 托管
【发布时间】:2018-11-17 00:37:25
【问题描述】:

所以我有一个 .NET 4.6.1 应用程序,它使用 Topshelf 作为 Windows 服务运行,我还有一个 ASP.NET Core 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 核心 nugets 更新到最新版本,它仍然会输出这些错误(这就像 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 核心项目的其他功能,使用控制台和自托管。

【问题讨论】:

  • 您的项目中似乎缺少一些参考资料。仔细阅读您的错误消息并添加参考。
  • 尝试了谷歌的不同方法来添加参考,似乎没有任何效果。不过可能做错了。如何在我的核心项目中添加对 netstandard2 的引用?
  • 这似乎和这个问题类似:stackoverflow.com/questions/50165910/…

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


【解决方案1】:

也许尝试像这样在 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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    相关资源
    最近更新 更多