简体   繁体   中英

Using a custom ServiceHostFactory in a WCF Service Library project

I'm programming a WCF service that internally relies heavily on Inversion of Control. I would like to bootstrap / initialize my IoC container inside a custom ServiceHostFactory. I've read some examples of different hooks that are available, but none of them seem to work for me.

This approach is the one I'd prefer to use (custom IServiceBehavior, IInstanceProvider, ServiceHost and ServiceHostFactory) but the last step is to signal to the service to use my custom ServiceHostFactory inside my serivce's *.svc-file. I don't have one of those since the project type is a WCF Service Library - not a WCF Service Application. Is there a way I can tell WCF to use my ServiceHostFactory in the App.config file?

Ciddan, glad to see you found a resolution (albeit not perfect). I had the same question as you and finally settled with using IIS to host the service and wired up my Ioc custom classes (that reside in my WCF Service Library) using the serviceHostingEnvironment in my web.config. Something along these lines:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
    <serviceActivations>
        <add relativeAddress="./Service1.svc" service="WcfServiceLibrary1.Service1"
          factory="WcfServiceLibrary1.IocServiceHostFactory, WcfServiceLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </serviceActivations>
</serviceHostingEnvironment>

"./Service1.svc" is my fileless service location where as WcfServiceLibrary1.Service1 is the concrete imp. I did hit a few stumbling blocks but in the end managed to encapsulate the Ioc from within the WCF Service Library.

This blog post was very helpful and has a few great tips.

As I don't see an anwer, I'll post what I found, which is not very encouraging.

All references to ServiceHostFactory point to the svc file and the Factory attribute, which is not what you are looking for.

This thread on MSDN even tells that it is not possible to do what you are trying to achieve.

Sorry...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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