Core: The issue of using AUTOFAC in. NET Core 3.0 Preview 5

Created on 5 Jun 2019  ·  12Comments  ·  Source: dotnet/core

Bing-translator: The issue of using AUTOFAC in. NET Core 3.0 Preview 5 cannot be returned IServiceProvider

Original

Title: .net core 3.0 预览版5中使用autofac的问题

.net core 3.0 预览版5中使用autofac的问题 无法返回IServiceProvider

needs-more-info

Most helpful comment

You do not, this is what the web example looks like in 3.0:

https://github.com/dotnet/core/issues/2828#issuecomment-500195988

Program.cs

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace WebApplication277
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            // Wire up autofac
            .UseServiceProviderFactory(new AutofacServiceProviderFactory());
}

}

**Startup.cs**

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WebApplication277
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {

        }

        public void ConfigureContainer(ContainerBuilder containerBuilder)
        {
            // wire up using autofac specific APIs here
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
}

All 12 comments

@lichen19951127 please use English, we do not scale to respond to reports in other languages. Thanks!

Please provide a minimal repro demonstrating the problem.
It is not clear what the problem is in the description above. Thanks!

.net core 3.0 preview5 use autofac,ConfigureServices Unable to return IServiceProvider

In .NET Core 3.0 you can do this:

C# public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .UseServiceProviderFactory(new AutofacServiceProviderFactory())

.net core 3.0 preview5 use autofac, ConfigureServices Unable to return IServiceProvider in Startup class

Right, and the above code snippet is the replacement.

I don't know how to write in Startup class

Do you have a web example?

You do not, this is what the web example looks like in 3.0:

https://github.com/dotnet/core/issues/2828#issuecomment-500195988

Program.cs

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace WebApplication277
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            // Wire up autofac
            .UseServiceProviderFactory(new AutofacServiceProviderFactory());
}

}

**Startup.cs**

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WebApplication277
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {

        }

        public void ConfigureContainer(ContainerBuilder containerBuilder)
        {
            // wire up using autofac specific APIs here
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
}

Thank you,I succeeded

Great!

How can I not see your comment, don't understand what you mean?


莫谈他人高薪,且看闲时谁在拼!!!

------------------ 原始邮件 ------------------
发件人: "Nenad Vićentić"notifications@github.com;
发送时间: 2019年9月5日(星期四) 晚上7:09
收件人: "dotnet/core"core@noreply.github.com;
抄送: "李晨"594281739@qq.com; "Mention"mention@noreply.github.com;
主题: Re: [dotnet/core] The issue of using AUTOFAC in. NET Core 3.0 Preview5 (#2828)

@davidfowl What is correct way to use builder.Populate(services);method in this scenario? I could save private field IServiceCollection _servicesat the last line of ConfigureServicesmethod, but it feels as completely wrong approach.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub , or mute the thread .

Was this page helpful?
0 / 5 - 0 ratings