简体   繁体   中英

ASP net core MVC - Start Web application in a library

I need to start a web application, self hosted with Kestrel, from a console project. The web application is in a library. I have a solution with two projects, ie:

  • --APP.Console.exe (a dotnet core 3.1 console application)
  • --APP.WebSite (a dotnet core 3.1 class library)

The App.Console, calls an APP.WebSite singleton method to start the web app. The solution compiles, but when the website starts, I got over 50 errors like this scren shots: 在此处输入图片说明

If I modify the APP.WebSite into a console Application, it regulary starts with no errors. Where I'm wrong?

EDIT1: Start console app:

    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        Test.Start.StartApp();
        Console.ReadKey();
    }

StartApp static method:

    public static void  StartApp()
    {
        var s = Directory.GetCurrentDirectory();
        //activate web hosting here            
        var host = new WebHostBuilder()
              .UseKestrel(o =>
              {
                  o.AllowSynchronousIO = true;
                  o.ListenAnyIP(5000);
                  //o.ListenLocalhost(5000);
              })
              .UseWebRoot(s + "/Web/wwwroot")
              //.UseContentRoot(s+"/Web/wwwroot")
              .UseStartup<StartUpWeb>()
              .Build();

        host.Start();
    }

In Web/wwwroot there all client files

EDIT2: Solution folders:

在此处输入图片说明

I was able to achieve what you want using .NET Core 3.1.1, but there is a few things I had to do.

First your folder structure is wrong.

The correct folder structure is this:

在此处输入图片说明

Notice I moved the wwwroot folder to a folder called Web... ONLY the wwwroot

Second The correct way to "convert" a asp.net core website to a class library is here:

在此处输入图片说明

So basically create a asp.net core web application project then right click properties and change the output type.

Third starting from a Class Library project If you are starting from .NET Core Class Library Project then you can open the csproj file and change a few things :

This is how my csproj file looks like now:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <ApplicationIcon />
    <OutputType>Library</OutputType>
    <StartupObject />
    <EnableDefaultContentItems>true</EnableDefaultContentItems>  
  </PropertyGroup>

  <ItemGroup>
    <Content Include="Web\wwwroot\**">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

</Project>

So basically change the Sdk, the OutputType, EnableDefaultContentItems and add an ItemGroup to force copy all the content files for wwwroot.

Finally Your code should look this:

        public static void StartApp(string environment)//notice the new parameter
        {
            var s = Directory.GetCurrentDirectory();
            //activate web hosting here            
            var host = new WebHostBuilder()
                  .UseEnvironment(environment)//set the environment for debugging mostly
                  .UseKestrel(o =>
                  {
                      o.AllowSynchronousIO = true;
                      o.ListenAnyIP(5000);
                  //o.ListenLocalhost(5000);
                  })
                  .UseWebRoot(s + "\\Web\\wwwroot")//notice the correct path to client files
                  //.UseContentRoot(s+"/Web/wwwroot")
                  .UseStartup<StartUpWeb>()
                  .Build();

            host.Start();
        }

And your entry point in your Console App:

        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");

            Test.Start.StartApp("Development");//pass the environment for debugging

            System.Console.ReadKey();
        }

I made a few changes to your code.

Specify the environment to debug and load configuration files etc....

Also specify the WebRoot to find stuff like client files and views(even though they are compiled it still needs a webroot).

Here is the result hooraay!!

在此处输入图片说明

You got it! The very problem was this configuration line:

<Project Sdk="Microsoft.NET.Sdk.Web">

Thank you Jonathan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM