Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a custom ServiceHostFactory in a WCF Service Library project

Tags:

c#

wcf

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?

like image 556
Ciddan Avatar asked May 04 '10 07:05

Ciddan


2 Answers

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.

like image 76
njappboy Avatar answered Sep 28 '22 00:09

njappboy


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...

like image 27
Timores Avatar answered Sep 27 '22 22:09

Timores