我有一个Visual Studio解决方案,包括几个项目,其中包括一个普通的Windows应用程序和一个Windows服务 . Windows服务当前托管多个WCF endpoints 并执行其他后台操作 .

我现在想要使用Asp.Net Core集成一个小型Web前端 .

由于已经有一个包含大量基础结构的Windows服务(MSI安装程序,客户的分发/更新通道等),我原本希望在此服务中托管Asp.Net Core应用程序 .

背景太多了 .

我使用VS 2017中的项目模板尝试了几个模拟设置,但我无法使用它 . 以下是重要的代码位:

  • 针对.NET 4.5.2的Asp.Net Core项目;基于VS 2017“Web应用程序”项目模板 . 这个项目本身运行得很好 . 我确保将其作为控制台应用程序运行(不在IIS Express中) .
public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot("hardcoded path to project root.")
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .Build();

    host.Run();
}
  • 一个针对.NET 4.5.2的单独Windows控制台应用程序项目,引用上一个项目 . 这里我们简单地调用Asp.Net Core项目的main方法 . (这只是用于原型设计,最终的Windows服务项目肯定会更复杂 . )
public static void Main(string[] args)
{
    // AspNetCoreFullFramework is a reference to the other project
    AspNetCoreFullFramework.Program.Main(args);
}

服务器启动正常,没有错误,但对于每个请求,我得到以下内容:

Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException

  An unhandled exception has occurred: One or more compilation failures occurred:
  /Views/_ViewImports.cshtml(5,28): error CS0234: The type or namespace name 'Identity' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)
  3xxxsx3m.yvs(34,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
  3xxxsx3m.yvs(35,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
  3xxxsx3m.yvs(36,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
  3xxxsx3m.yvs(39,36): error CS0234: The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
  3xxxsx3m.yvs(40,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
  3xxxsx3m.yvs(42,86): error CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
  3xxxsx3m.yvs(42,86): error CS0518: Predefined type 'System.Boolean' is not defined or imported
  3xxxsx3m.yvs(42,45): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
  3xxxsx3m.yvs(67,16): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

  at Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationResult.EnsureSuccessful()
     at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.CreateCacheEntry(String relativePath, String normalizedPath, Func`2 compile)
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.GetOrAdd(String relativePath, Func`2 compile)
     at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath)
     at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens, String relativePath, Boolean isMainPage)
     at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey)
     at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, String pageName, Boolean isMainPage)
     at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
     at Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
     at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor.FindView(ActionContext actionContext, ViewResult viewResult)
     at Microsoft.AspNetCore.Mvc.ViewResult.<ExecuteResultAsync>d__26.MoveNext()

我在两个项目中都尝试了同样的目标.Net Core;也试过它针对Framework 4.6 . 也没用 .

我查看了所有类似的问题,但没有一个涉及在单独的Windows服务中引用Asp.Net Core项目并在那里托管的情况 . 例如,我知道this excellent question(它建议 host.RunAsService() ),但是这个解决方案只需要为Asp.Net Core提供单独的Windows服务,这正是我想要避免的 .

现在可能这是不可能的 . 这是一个可以接受的解决方案 . 我希望事实并非如此 .