如何查看.NET IServiceProvider可以提供的所有服务? [英] How do I see all services that a .NET IServiceProvider can provide?

查看:47
本文介绍了如何查看.NET IServiceProvider可以提供的所有服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有关.NET的常见问题

This is a general question regarding .NET

我得到了IServiceProvider 接口,但是关于从中可以得到什么的信息很少.我如何找到它可能提供的所有服务的列表?

I am given an instance of the IServiceProvider interface, but I have little documentation as to what might be possible to get from it. How would I find out a list of all the services it might provide?

推荐答案

System.IServiceProvider has a single method, .GetService(Type), which returns a single service. It's essentially a dictionary mapping types to services, and it does not provide access to all keys, probably because it's intended for implementation over the wire.

要由类 implementing 决定接口,以公开允许其提供的服务发现的方法或属性-没有一般的方法可以单独使用接口查看所有提供的服务

It's up to the class implementing the interface to expose a method or property that allows discovery of the services it provides - there is no general way to see all provided services using the interface alone.

###解决方案:

  • 如果您可以控制服务提供商的来源,请创建一个允许您想要的子界面

  • If you have control over the service providers' source, make a child interface that allows what you want

  interface IBetterServiceProvider : System.IServiceProvider
     {
         IList<object> GetAllServices();
         IList<Type> GetAllServicedTypes();
     }

并使您的服务实现它.

如果您没有可以控制服务提供商的源,则可以强制转换为 IServiceProvider 实现类型,也可以使用反射来查找属性或告诉您您想要什么的方法.如果正在使用的提供程序中似乎存在一致的 .GetServices()方法,则可以使用动态分派

If you don't have control over the service providers' source, either cast to the IServiceProvider implementation type, or use reflection to look for properties or methods that tell you what you want. If there appears to be a consistent .GetServices() sort of method in the providers you're working with, then you can use dynamic dispatch 1, 2, 3 to access that method without casting.

也就是说,即使Microsoft自己的类实现也有点麻烦.要引用文档,

That said, even Microsoft's own implementations of the class are a bit of a rabbit hole. To quote the docs,

IServiceProvider 接口由多种类型实现,包括 System.Web.HttpContext System.ComponentModel.LicenseContext System.ComponentModel.MarshalByValueComponent System.ComponentModel.Design.ServiceContainer .

The IServiceProvider interface is implemented by a number of types, including System.Web.HttpContext, System.ComponentModel.LicenseContext, System.ComponentModel.MarshalByValueComponent, and System.ComponentModel.Design.ServiceContainer.

  • HttpContext 实现了该接口,但是 GetService(Type)方法被记录为仅供内部使用,并且它包含的唯一服务(在公共API中),至少)是 PageInstrumentation .在此实现中,无法查询所有服务.

    • HttpContext implements the interface, but the GetService(Type) method is documented as internal use only, and the only service it contains (in the public API, at least) is PageInstrumentation. There is no way to query for all services in this implementation.

      ServiceContainer 实际上并未实现该接口(尽管它确实具有有点吓人.它确实证实了怀疑-这是将类型映射到服务的出色字典.同样,此实现并没有提供自己的方式来获取其拥有的所有服务.这是我所期望的,因为它明确是服务的容器.

      ServiceContainer doesn't actually implement the interface (though it does have an internal field of that interface type.) Even though the ServiceContainer doesn't implement the interface, it does implement the method, and it's a bit scary. It does confirm suspicions - it's a glorified dictionary mapping types to services. Again, this implementation doesn't provide its own way of getting all services it holds. This is the one I expected to, since it's explicitly a container of services.

      LicenseContext.GetService(Type) 除非被覆盖,否则只会返回null.也许此类的某些子类提供了一种获取所有服务的方法,但这种方法没有.

      LicenseContext.GetService(Type) just returns null unless its overridden. Perhaps some of this class' subclasses provide a way to get all services, but this one doesn't.

      我已经完成了源代码和文档的挖掘工作.看起来有点混乱,但是上面的简短答案仍然存在:旧名称或新名称,伪实现或实际实现:无法单独从 IServiceProvider 接口获取所有服务,并且Microsoft的任何实现都无法做到这一点我发现您也可以做到这一点.

      I'm done digging through source and docs. It appears a bit messy, but the short answer above holds: old name or new, pseudoimplementation or actual implementation: there is no way to get all services from the IServiceProvider interface alone, and none of Microsoft's implementations that I found give you a way to do that either.

      这篇关于如何查看.NET IServiceProvider可以提供的所有服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆