日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Autofac中的AsSelf()作用

發(fā)布時(shí)間:2023/12/29 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Autofac中的AsSelf()作用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

關(guān)于Autofac中的AsSelf()具體有什么用,有點(diǎn)好奇查了文檔了解了下

Services vs. Components

When you register?components, you have to tell Autofac which?services?that component exposes. By default, most registrations will just expose themselves as the type registered:

// This exposes the service "CallLogger" builder.RegisterType<CallLogger>();

Components can only be?resolved?by the services they expose. In this simple example it means:

// This will work because the component // exposes the type by default: scope.Resolve<CallLogger>();// This will NOT work because we didn't // tell the registration to also expose // the ILogger interface on CallLogger: scope.Resolve<ILogger>();

You can expose a component with any number of services you like:

builder.RegisterType<CallLogger>().As<ILogger>().As<ICallInterceptor>();

Once you expose a service, you can resolve the component based on that service. Note, however, that once you expose a component as a specific service, the default service (the component type) is overridden:

// These will both work because we exposed // the appropriate services in the registration: scope.Resolve<ILogger>(); scope.Resolve<ICallInterceptor>();// This WON'T WORK anymore because we specified // service overrides on the component: scope.Resolve<CallLogger>();

If you want to expose a component as a set of services as well as using the default service, use the?AsSelf?method:

builder.RegisterType<CallLogger>().AsSelf().As<ILogger>().As<ICallInterceptor>();

Now all of these will work:

// These will all work because we exposed // the appropriate services in the registration: scope.Resolve<ILogger>(); scope.Resolve<ICallInterceptor>(); scope.Resolve<CallLogger>();

一個(gè)是一個(gè)接口有多個(gè)實(shí)現(xiàn),AsSelf()的話就是設(shè)置為默認(rèn)的實(shí)現(xiàn)了。

參考文檔說明?

總結(jié)

以上是生活随笔為你收集整理的Autofac中的AsSelf()作用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。