简体   繁体   English

Asp.Net核心中是否有任何可扩展性点,可以在startup.cs以外的任何地方注册服务?

[英]Is there any extensibility point in Asp.Net core to register services anywhere other than startup.cs?

Basically in Asp.Net Core we register services inside startups.cs file. 基本上在Asp.Net Core我们在startups.cs文件中注册服务。 Now the question is , is there any extensibility point in Asp.Net Core giving us the chance to hook into some kind of registrar method (by implementing a specific interface for example) and don't mess up with startup.cs ? 现在的问题是, Asp.Net Core是否有任何可扩展性点,这使我们有机会加入某种注册方法(例如,通过实现特定的接口),并且不会搞砸startup.cs

Avoid making a mess of Startup 避免搞乱Startup

If you just want to avoid making a mess in Startup.cs just delegate the service registration logic to another method, perhaps in another static class. 如果只想避免在Startup.cs造成混乱,只需将服务注册逻辑委托给另一个方法(可能在另一个静态类中)即可。

ServiceRegistry.RegisterAllServices(services);

Hooking into ASP.Net Startup pipeline 涉足ASP.Net启动管道

If you really want to hack your way into startup logic of ASP.Net I'm afraid you're out if luck. 如果您真的想闯入ASP.Net的启动逻辑,恐怕运气不好。 The build up of the ServicesCollection takes place in the BuildCommonServices method. ServicesCollection的构建在BuildCommonServices方法中进行。 As you can see there is no easy way to hook into the logic here. 如您所见,这里没有简单的方法可以挂接到逻辑上。

In short what happens when an ASP.Net app start is the following: 简而言之,启动ASP.Net应用程序时会发生以下情况:

  1. In .Net core there is only one app model - Console Apps; .Net core ,只有一个应用程序模型-控制台应用程序; so Main is called. 所以称Main
  2. Some magic happens and we arrive at StartupLoader.LoadMethods . 发生了一些魔术,我们到达了StartupLoader.LoadMethods
  3. Three methods are located Configure , ConfigureContainer and ConfigureServices . 三种方法位于ConfigureConfigureContainerConfigureServices
  4. All three lookups are passed to FindMethod which always looks up in the startupType and the startupType only. 所有这三个查找都传递给FindMethod ,该方法始终仅在startupTypestartupType ( REF ) 参考

How to register services post startup 启动后如何注册服务

Registering additional services into the IOC Container that comes with ASP.Net Core is not trivial. 将其他服务注册到ASP.Net Core附带的IOC Container中并ASP.Net Core

The DI Framework itself is very basic and if you find yourself trying to do something that is not trivial you probably should upgrade yourself to a more mature and feature complete IOC Container . DI框架本身是非常基础的,如果您发现自己做的事情不那么琐碎,则可能应该将自己升级为更成熟且功能完善的IOC Container

You can find some links in the README file on the aspnet/DependencyInjection repository on GitHub. 您可以在GitHub上的aspnet / DependencyInjection存储库上的README文件中找到一些链接。

I have to note though, registering services after the container is created is considered a bad practice. 不过,我必须指出,在创建容器之后注册服务被认为是一种不好的做法。

Good answer here already, but just to add my 2c. 这里已经很好的答案了,但是只是添加我的2c。

I think what you are essentially looking for is something like "Profiles" or "Modules" and the IOC does an assembly scan. 我认为您本质上在寻找的是“配置文件”或“模块”之类的东西,并且IOC会进行程序集扫描。 You can do this already, but you would need to create your own interface/reflection code. 您已经可以执行此操作,但是您需要创建自己的界面/反射代码。

Another option is to use the ServiceCollection Extension pattern which seems to be the default way to add "services". 另一种选择是使用ServiceCollection Extension模式,这似乎是添加“服务”的默认方法。

For example take this code : 例如,使用以下代码:

public static class ServicesConfiguration
{
    public static void AddCustomServices(this IServiceCollection services)
    {
        services.AddTransient<IMyService, MyService>();
    }
}

Then your Configure Services method would look more like : 然后,您的Configure Services方法将更像:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddCustomServices();
}

In some ways I prefer this as you can still at a glance see what is being loaded in and follow the trail rather than having some mystery reflection going on. 从某些方面来说,我更喜欢这样做,因为您仍然可以一目了然地看到正在加载的内容并按照路线行驶,而不是进行一些神秘的思考。

Further reading : http://dotnetcoretutorials.com/2017/01/24/servicecollection-extension-pattern/ 进一步的阅读: http : //dotnetcoretutorials.com/2017/01/24/servicecollection-extension-pattern/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM