Hosting Web API in .Net Core Worker Service - cannot reference IWebHostEnvironment

ToreS

I'm creating a .NET Core Worker Service, and want to expose ASP.Net Core Web APIs for the service. I'm using .NET Core 3.0. Initially, my plan was to replace IHostBuilder with IWebHostBuilder and add a Startup class just like a regular Asp.Net Core web app (this is probably an oversimplification, though).

My plan was to simply try to replace

public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)                
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                });
        }

with

public static IWebHostBuilder CreateHostBuilder(string[] args)
        {
            return WebHost.CreateDefaultBuilder(args)
                .ConfigureServices(services =>
                {
                    services.AddHostedService<Worker>();
                }).UseStartup<Startup>();            
        }

which may not work at all, but at least it's a starting point... What's blocking me from trying this approach is that I cannot implement my Startup class because IWebHostEnvironment is unavailable.

Here's my

<PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UserSecretsId>dotnet-WorkerServices-0E977A2C-F0C8-49E7-B00A-5EB01B99FBEB</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.Server.Abstractions" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" />
  </ItemGroup>

As far as I know, IWebHostEnvironment should be in the Microsoft.Extensions.Hosting.Abstractions nuget package, but referencing it does not seem to work.

Does anyone have any ideas? -Thanks!

Henk Holterman

It is much easier to add a service to an API project. That's just one extra line.

If you can't or don't want to start over, change your project manually. Forget about IWebHostBuilder, it is now obsolete or deprecated or something.

I could post some fragments here but much easier: create a temporary API project, copy over the Program and Startup classes (but keep your namespaces) and then, inside Startup:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHostedService<Worker>();  // the worker will run
    services.AddControllers();
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Publishing and hosting Web API developed in .net core through IIS

分類Dev

In Azure Service Fabric, what is the deference between a Stateless Web API and the ASP.NET Core Web API?

分類Dev

Cannot test a simple Asp.net core web api deployed to Docker

分類Dev

Hosting multiple product APIs in single ASP.NET Core WebAPI Service

分類Dev

what are the limitations of a .net web api service?

分類Dev

.NET Core Web APIキー

分類Dev

.NET core web api with queue processing

分類Dev

SignalR with web Api as client [.Net Core]

分類Dev

Migrated web service to .NET Core 2.0 and returning json

分類Dev

Cannot launch asp.net core web app in IIS Express

分類Dev

Cannot access ASP.NET Web API with IP address

分類Dev

Return "raw" json in ASP.NET Core 2.0 Web Api

分類Dev

Unable to get request header in asp net core web API

分類Dev

ASP.NET Core Web APIの例外処理

分類Dev

ASP.NET Core Web API認証

分類Dev

Accept x-www-form-urlencoded in Web API .Net Core

分類Dev

Angular Post json to .net core web api is null

分類Dev

.net Core 2.0 web api 400 error using Validateantiforgerytoken

分類Dev

Cutomize Swagger UI ASP.NET Core Web API

分類Dev

passing an array to a asp net core web api action method HttpGet

分類Dev

Implement Pagination in ASP.NET Core 2.1 Web API

分類Dev

Publishing .NET Core web api/angular application to IIS

分類Dev

ASP.NET Core Web API FacebookJWT認証

分類Dev

Web APIのAuthorizeAttribute(ASP.NET Core 2)

分類Dev

Return string from Web API .NET Core get operation

分類Dev

Unhandled Exception in ASP.Net Core Web API on Linux

分類Dev

Angular 2 & .NET Core Web API HttpPost の問題

分類Dev

C#.net Core web Api Get request#parameter

分類Dev

Cannot Register Custom Firebase FCM Service Worker With Webpack

Related 関連記事

  1. 1

    Publishing and hosting Web API developed in .net core through IIS

  2. 2

    In Azure Service Fabric, what is the deference between a Stateless Web API and the ASP.NET Core Web API?

  3. 3

    Cannot test a simple Asp.net core web api deployed to Docker

  4. 4

    Hosting multiple product APIs in single ASP.NET Core WebAPI Service

  5. 5

    what are the limitations of a .net web api service?

  6. 6

    .NET Core Web APIキー

  7. 7

    .NET core web api with queue processing

  8. 8

    SignalR with web Api as client [.Net Core]

  9. 9

    Migrated web service to .NET Core 2.0 and returning json

  10. 10

    Cannot launch asp.net core web app in IIS Express

  11. 11

    Cannot access ASP.NET Web API with IP address

  12. 12

    Return "raw" json in ASP.NET Core 2.0 Web Api

  13. 13

    Unable to get request header in asp net core web API

  14. 14

    ASP.NET Core Web APIの例外処理

  15. 15

    ASP.NET Core Web API認証

  16. 16

    Accept x-www-form-urlencoded in Web API .Net Core

  17. 17

    Angular Post json to .net core web api is null

  18. 18

    .net Core 2.0 web api 400 error using Validateantiforgerytoken

  19. 19

    Cutomize Swagger UI ASP.NET Core Web API

  20. 20

    passing an array to a asp net core web api action method HttpGet

  21. 21

    Implement Pagination in ASP.NET Core 2.1 Web API

  22. 22

    Publishing .NET Core web api/angular application to IIS

  23. 23

    ASP.NET Core Web API FacebookJWT認証

  24. 24

    Web APIのAuthorizeAttribute(ASP.NET Core 2)

  25. 25

    Return string from Web API .NET Core get operation

  26. 26

    Unhandled Exception in ASP.Net Core Web API on Linux

  27. 27

    Angular 2 & .NET Core Web API HttpPost の問題

  28. 28

    C#.net Core web Api Get request#parameter

  29. 29

    Cannot Register Custom Firebase FCM Service Worker With Webpack

ホットタグ

アーカイブ