2

I have a Visual Studio solution comprising several projects, among them a normal Windows application and a Windows service. The Windows service currently hosts several WCF endpoints and does other background stuff.

I now want to integrate a small web front-end using Asp.Net Core.

Since there is already a Windows Service with lots of infrastructure (MSI installer, distribution/update channel for customers, and so on), I had hoped to host the Asp.Net Core application inside this service.

So much for the background.

I tried several mock setups using the project templates from VS 2017, but I cannot get this to work. Here are the important code bits:

  1. An Asp.Net Core project targeting .NET 4.5.2; based on VS 2017 "Web Application" project template. This project runs just fine on its own. I made sure to run it as a console application (not inside 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();
    }
    
  2. A separate Windows console application project targeting .NET 4.5.2, referencing the previous project. Here we simply call the main method of the Asp.Net Core project. (This is all just for prototyping, the final Windows service project would certainly be more sophisticated.)

    public static void Main(string[] args)
    {
        // AspNetCoreFullFramework is a reference to the other project
        AspNetCoreFullFramework.Program.Main(args);
    }
    

The server starts up fine, no errors, but for each request I get the following:

  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()

I tried the same targeting only .Net Core in both projects; also tried it targeting Framework 4.6. Didn't work either.

I looked at all the similar questions, but none of them cover the case in which an Asp.Net Core project is referenced in a separate Windows service and hosted there. For instance, I am aware of this excellent question (it suggests host.RunAsService()), but this solution requires a separate Windows service only for Asp.Net Core, which is exactly what I'm trying to avoid.

Now it may be that this is impossible. That would be an acceptable solution. I just hope that's not the case.

Andreas
  • 1,751
  • 2
  • 14
  • 25

0 Answers0