asp.net - 从 .NET Framework 应用程序启动 AspNetCore 托管
问题描述
所以我有一个 .NET 4.6.1 应用程序,它使用 Topshelf 作为 Windows 服务运行,我还有一个 ASP.NET Core Web 应用程序(MVC 和 API),它当前作为服务运行,并在自己的进程中使用Microsoft.AspNetCore.Hosting
和Microsoft.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 核心项目的其他功能,使用控制台和自托管。
解决方案
也许尝试像这样在 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" />
推荐阅读
- async-await - Webdriverio 等到可见
- com - 如何创建进程外 RTD 服务器的多个实例?
- android - 警告:尝试生成可用性为空的警卫:已过时 = 21 错误
- sql - 预订表查询
- joomla - Joomla configuration.php 在权限为 0644 时不可写,但在 0664 时可写
- javascript - Angular - 找不到名称 CookieService
- sql - 从表 A 中选择所有行,并在帐号上连接表 B,但排除 tableB.foo == bar 的行。我可以在一次查询中获取此数据集吗?
- python-3.x - python tkinter,滚动条缺少旋钮(拇指)
- php - 如何在最后一个数组值之后加入字符串?
- javascript - 在不破坏我的代码的情况下删除一个函数