Сервис Fabri c As pNet Core 3.1 Autofa c WebHostBuilder - PullRequest
0 голосов
/ 30 января 2020

У меня есть сервисное приложение fabri c без состояний asp. net core 2.2. Я пытаюсь обновить это до asp. net core 3.1. Я использую Autofa c контейнер для инъекций зависимости. В соответствии с документацией autofa c регистрация DI перенесена из WebHostBuilder в Generi c HostBuilder https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html#asp - net -core-3-0-and-generi c -hosting . Но Service fabri c не поддерживает asp. net core Generi c Host https://github.com/microsoft/service-fabric-aspnetcore/issues/48.

Есть ли другой способ зарегистрировать Autofa c в WebHostBuilder?

1 Ответ

0 голосов
/ 26 февраля 2020

Я сделал это так:

namespace GameApi
{
    using System;
    using System.Linq;

    using Autofac;
    using Autofac.Extensions.DependencyInjection;
    using FluentValidation.AspNetCore;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;

    public sealed class Program
    {
        public static void Main(string[] args)
        {
            CreateHost(args).Run();
        }

        public static IHost CreateHost(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureContainer<ContainerBuilder>(SetupContainer)
                .ConfigureWebHostDefaults(SetupWebHost).Build();

        private static void SetupContainer(ContainerBuilder builder)
        {
            var types = AppDomain.CurrentDomain
                .GetAssemblies()
                .SelectMany(a => a.GetExportedTypes())
                .ToArray();

            builder.RegisterTypes(types)
                   .AsImplementedInterfaces()
                   .InstancePerDependency();
        }

        private static void SetupWebHost(IWebHostBuilder builder) => 
            builder.ConfigureServices(SetupServices)
                   .Configure(SetupApp);

        private static void SetupServices(IServiceCollection services)
        {
            services.AddControllers()
                    .AddFluentValidation();
        }

        private static void SetupApp(IApplicationBuilder app)
        {
            app.UseRouting();
        }
    }
}

Полная версия на моем GitHub: https://github.com/szubajak/game/blob/master/Src/GameApi/Program.cs

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...