2
votes

I am creating a custom ServiceHost object and configuring it from code. My service is using InstanceContextMode.Single and ConcurrencyMode.Multiple and is hosted in a windows service.

As stated in a number of blogs/articles (here), sharing a StructureMap container across instances requires using a custom InstanceProvider, ServiceBehavior and ServiceHostFactory.

My initialization code looks like this. I do not use a config file.

var baseAddress = ConfigurationManager.AppSettings["BaseAddress"];
var port = Int32.Parse(ConfigurationManager.AppSettings["Port"]);

Host = new MyServiceHost(typeof(MediaFileServicePrivate), new Uri(string.Format(baseAddress, port)));

var binding = new NetTcpBinding();
Host.AddServiceEndpoint(typeof(IMediaFileServicePrivate), binding, string.Format(baseAddress, port));

How do I tell the service to use my custom service host factory? All the examples I can find configure it from the config file.

Is a ServiceHostFactory only used for IIS/WAS hosted scenarios? If so, how do I use SM for a self-hosted InstanceContextMode.Single service?

2

2 Answers

0
votes

Has this not been answered? essentially you tell it to use your servicefactory in the wcf markup

<%@ ServiceHost Language="C#" Debug="true" Service="WcfWithDI.Service1" CodeBehind="Service1.svc.cs" Factory="WcfWithDI.MyServiceFactory"%>

I have a sample project here all wired up with a custom provider (and structuremap) https://github.com/billCreativeD/WCF_With_DI

0
votes

Unfortunately it makes little sense to say "the service uses the factory". You would use the Ninject factory to create your service:

var factory = new NinjectServiceHostFactory();

var address = new Uri(_baseAddress, path);
ServiceHostBase host = factory.CreateServiceHost(typeName, new[] {address});

var binding = new NetTcpBinding();
host.AddServiceEndpoint(typeof(TheType), binding, string.Format(baseAddress, port));

The directive in the .svc file is used to tell .NET how to instantiate your service.