Tuesday, November 16, 2021

[SOLVED] First try at a dotnetcore ASP.Net: WebHostBuilder error

Issue

Under linux, using the latest dotnetcore

[idf@localhost dot-net-core-centos-7-master]$ dotnet --version
1.0.4
[idf@localhost dot-net-core-centos-7-master]$ 

I am getting an erron on this simple application. What am I missing?

The .csproj file

[idf@localhost dot-net-core-centos-7-master]$ more dot-net-core-centos-7-master.csproj 
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk">
      <Version>1.0.0-alpha-20161104-2</Version>
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Sdk.Web">
      <Version>1.0.0-alpha-20161104-2</Version>
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NETCore.App">
      <Version>1.1.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AspNetCore.Mvc">
      <Version>1.1.0</Version>
    </PackageReference>
  </ItemGroup>
</Project>
[idf@localhost dot-net-core-centos-7-master]$

project.json:

[idf@localhost dot-net-core-centos-7-master]$ more project.json
{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0-rc2-3002702"
    },
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  },
}

The Program.cs

[idf@localhost dot-net-core-centos-7-master]$ more Program.cs 
using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;


namespace aspnetcoreapp
{
    public class Program
    {
        public static void Main(string[] args)
        {                
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup<Startup>()
                .UseUrls("http://*:80/")
                .Build();

            host.Run();
        }
    }
}

Startup.cs

[idf@localhost dot-net-core-centos-7-master]$ more Startup.cs 

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

namespace aspnetcoreapp
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.Run(context =>
            {
                return context.Response.WriteAsync("Hello from ASP.NET Core!");
            });
        }
    }
}

[idf@localhost dot-net-core-centos-7-master]$

When I try to run it I get a runtime error:

[idf@localhost dot-net-core-centos-7-master]$ dotnet run
/home/idf/dotnet/sdk/1.0.4/Sdks/Microsoft.NET.Sdk/build/Microsoft.NET.Sdk.DefaultItems.targets(101,5): warning : A PackageReference for 'Microsoft.NETCore.App' was included in your project. This package is implicitly referenced by the .NET SDK and you do not typically need to reference it from your project. For more information, see https://aka.ms/sdkimplicitrefs [/home/idf/dotnet_progs/dot-net-core-centos-7-master/dot-net-core-centos-7-master.csproj]
Program.cs(14,28): error CS0246: The type or namespace name 'WebHostBuilder' could not be found (are you missing a using directive or an assembly reference?) [/home/idf/dotnet_progs/dot-net-core-centos-7-master/dot-net-core-centos-7-master.csproj]
Program.cs(20,18): error CS7036: There is no argument given that corresponds to the required formal parameter 'handler' of 'RunExtensions.Run(IApplicationBuilder, RequestDelegate)' [/home/idf/dotnet_progs/dot-net-core-centos-7-master/dot-net-core-centos-7-master.csproj]

The build failed. Please fix the build errors and run again.
[idf@localhost dot-net-core-centos-7-master]$ ls

Solution

I think the problem lies in the fact that you are referencing Microsoft.NETCore.App in your .csproj and project.json file. Looking at the .csproj file for one of my .NET Core web app, I don't see any reference to Microsoft.NETCore.App. So my suggestion is for you to first backup the copy of the original somewhere and then remove the lines in your .csproj and project.json files that reference Microsoft.NETCore.App. I find it odd that you have both a .csproj file and project.json file.

Here is what I think your .csproj file should look like:

.csproj

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0" />
  </ItemGroup>
</Project>

Once you update your .csproj file and safely remove project.json, I would do dotnet clean to clean your build output and then dotnet build to see if everything is ok. If so, you should be able to dotnet run just fine.



Answered By - kimbaudi