ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

Support ASP.NET Core 3.0 #206

Closed shahabganji closed 5 years ago

shahabganji commented 5 years ago

I am planning to move to asp.net core 3.0, but I got an error message which sounds to be due to breaking changes in asp.net core 3.0.

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' to type 'Grace.DependencyInjection.IInjectionScope'.
   at Microsoft.Extensions.Hosting.Internal.ConfigureContainerAdapter`1.ConfigureContainer(HostBuilderContext hostContext, Object containerBuilder)
   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at dry_test.Program.Main(String[] args) in /../Program.cs:line 20

Startup.cs file:

// omitted code

 public void ConfigureContainer(IInjectionScope scope)
        {
            scope.SetupMvc();
            scope.Configure(c=> c.Export<SampleService>().As<ISample>());
        }

// omitted code

Program.cs file:

 public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.UseGrace();
                });

Any changes I need to make, or should wait until final version of 3.0?

ipjohnson commented 5 years ago

Hmmm that’s no good. I’ll see if I can address it this weekend.

ipjohnson commented 5 years ago

@shahabganji I had forgotten there are some changes I need to do for Asp.Net Core 3.0. So good news is I was able to put together a quick example to prove it works. The down side is it's going to take me a week or two to implement this and the other changes needed to fully support 3.0.

If you can wait a week or two for Grace 7.0-beta to be released I'd hold off. If you want to try out the extension I hacked together this afternoon I can post it.

shahabganji commented 5 years ago

Thanks @ipjohnson for the prompt reply,

There is no rush for this and your timing sounds great, I'll stick to the default IOC shipped with ASP.Net Core 3.0 since AFAIK Grace understands what has been registered with the default container so that there would be fewer changes I need to make after you release the beta/final version later on.

ipjohnson commented 5 years ago

I've released a Beta version of Grace and the extension library. The big change is that the UseGrace method was changed to apply to the IHostBuilder instead of the IWebHostBuilder. So your code will need to change to this.

 public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseGrace()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
shahabganji commented 5 years ago

@ipjohnson I tested the beta version. Everything looks to work properly on a test project, I guess this issue can be closed then. I'll come back if I hit any issues on a real project. Enclosed you can find which versions I Have used so that if there are any conflicts you can mention me.

Screen Shot 2019-03-31 at 14 27 38

Just one thing to mention, as far as I remember when using scope.SetupMvc( ); in the ConfigureContainer method it automatically registered IHttpContextAccessor am I right? because in the new version I have to register it manually.

ipjohnson commented 5 years ago

I believe it used to be registered automatically somewhere in asp.net core but they stopped doing that. So now you have to register it yourself