In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of NET Core 3.0 AutoFac built-in DI replacement, which is very detailed and has a certain reference value. Interested friends must read it!
The .NET Core 3.0 is different from previous versions, with some changes in the way AutoFac services are replaced, and there are some problems when trying to upgrade the project.
It turns out that in NET Core 2.1, AutoFac returned an IServiceProvider parameter to be injected into the ConfigureServices .NET Core service, which is basically what the mole does.
First we need a method to rewrite Autofac.Module, which will be used to register our Register [data access layer] and Services [logical layer].
Public class AutofacModuleRegister: Autofac.Module {/ / override the Autofac pipeline Load method, where the injection protected override void Load (ContainerBuilder builder) {/ / must be the builder.RegisterAssemblyTypes (GetAssemblyByName ("BlogService")) at the end of the Service. Where (a = > a.Name.EndsWith ("Service")) .AsImplementedInterfaces (); builder.RegisterAssemblyTypes (GetAssemblyByName ("BlogRepository")) .Where (a = > a.Name.EndsWith ("Repository")) .AsImplementedInterfaces () / / single registration / / builder.RegisterType () .Named (typeof (PersonService) .Name);} / get assembly name / assembly name public static Assembly GetAssemblyByName (String AssemblyName) {return Assembly.Load (AssemblyName);}}
Then, change the return value of the .NET Core's ConfigureServices method to IServiceProvider, which will be used to inject your service.
Public IServiceProvider ConfigureServices (IServiceCollection services) {services.AddMvc () .SetCompatibilityVersion (CompatibilityVersion.Version_2_1); return Blog.AutoFacModule.Solucation.AutoFac.Provider.RegisterAutofac.ForRegisterAutofac (services);}
In the above code, we call our custom method ForRegisterAutoFac, which replaces the policy we defined with AutoFac for the built-in DI.
Public static class RegisterAutofac {public static IServiceProvider ForRegisterAutofac (IServiceCollection services) {var builder = new ContainerBuilder (); builder.Populate (services); builder.RegisterModule (); var container = builder.Build (); return new AutofacServiceProvider (container);}}
In the API layer, we rely on injection Service so that our basic AutoFac for .NET Core2.1 is implemented.
[Route ("api/ [controller]")] [ApiController] public class ValuesController: ControllerBase {private IPersonService _ personService; public ValuesController (IPersonService personService) {_ personService = personService;} / / GET api/values [HttpGet] public ActionResult Get () {return Newtonsoft.Json.JsonConvert.SerializeObject (_ personService.people ());}}
For now, let's talk about the differences between the .NET Core3.0 and previous versions. I will change all the projects and dependencies to version 3.0, start now, and you will find something unexpected.
What? What are you talking about? Do you want to do it? Why didn't it work?
After reading the official documentation, I learned that.. NET Core 3.0 introduced features with strongly typed container configurations. It provides a ConfigureContainer method where you can use Autofac to register things without having to register things through ServiceCollection. So.... Okay! How the .NET Core3.0 will be configured.
First of all, we need to modify the service factory in Program.cs, which is built into ServiceProviderFactory, which we will specify as: AutofacServiceProviderFactory.
Public static IHostBuilder CreateHostBuilder (string [] args) = > Host.CreateDefaultBuilder (args) .ConfigureWebHostDefaults (webBuilder = > {webBuilder.UseStartup ();}) .UseServiceProviderFactory (new AutofacServiceProviderFactory ())
Now you need to add the method ConfigureContainer to Startup.cs and add the following code.
Public void ConfigureContainer (ContainerBuilder builder) {/ / add dependency injection builder.RegisterModule (new Blog.AutoFacModule.Solucation.AutoFac.Register.AutofacModuleRegister ()); var controllerBaseType = typeof (ControllerBase); / / use dependency injection builder.RegisterAssemblyTypes (typeof (Program) .Assembly) .Where (t = > controllerBaseType.IsAssignableFrom (t) & & t! = controllerBaseType) .PropertiesAutoinjection ();}
Then it's done, and there's no problem with starting it anymore.
The above is all the content of the article "sample Analysis of NET Core 3.0 AutoFac built-in DI replacement". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.