Juan Suero

Hello,


Thank you ahead of time for your brain cyclesJ¡­

I¡¯m in one process space. But I want to host multiple servicehosts in that space.

Each servicehost will run a service instance I instantiate in code and provide to it.

Foreach(¡­

Servicehost(myServiceInstance);

Each instance of ¡°myServiceInstance¡± will be of the same type just a different instantiation.

At first glance you might think of endpoints but endpoints are really just different communication paths hosted by the servicehost into the same instance.

I can what I want with custom code already.

My problem is doing it all from configuration. The problem lies in the fact that when you set up a you config

<Service name=¡±namespace.type¡±

Is used to map the instance you fed into servicehost with its respective configuration information.

But since I¡¯m using many instance of the same type into different servicehosts in the same process space, how does

WCF differentiate between them

With custom code I can give them different base address or different endpoint addresses into different servicehosts.

Configuration seems limited in this regard.

Does anyone know any way around this

Thanks.



Re: Windows Communication Foundation (Indigo) WCF Hosting multiple instances of the same type via configuration

Brian McNamara - MSFT

See this thread

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1382902&SiteID=1

for workaround suggestions.






Re: Windows Communication Foundation (Indigo) WCF Hosting multiple instances of the same type via configuration

Juan Suero

thanks...

I came up with the solution about 10 minutes ago. Someone at your link also had the same approach.

If you override ServiceHost.ApplyConfiguration

You can supply your own configuration name to drive the

Configuration of your service runtime.

Make sure you call base.ApplyConfiguration(); AFTER you set this.Description.ConfigurationName

This changes the algorithm from using your object typename to index into WCF configuration to using whatever name you see fit.

MyServiceType MyService1 = new MyServiceType();

MyServiceType MyService2 = new MyServiceType ();

ServiceHost ServiceHost1 = new ServiceHost(MyService1);

ServiceHostOverride ServiceHost2 = new ServiceHostOverride(MyService2);

ServiceHost1.Open();

ServiceHost2.Open();

public class ServiceHostOverride : ServiceHost

{

public ServiceHostOverride(object singletonInstance, params Uri[] baseAddresses)

: base(singletonInstance, baseAddresses) { }

protected override void ApplyConfiguration()

{

this.Description.ConfigurationName = "Two";

base.ApplyConfiguration();

}

}

< xml version="1.0" encoding="utf-8" >

<configuration>

<system.serviceModel>

<behaviors>

<serviceBehaviors>

<behavior name="MyServiceBehavior">

<serviceMetadata httpGetEnabled="true" />

</behavior>

</serviceBehaviors>

</behaviors>

<services>

<service behaviorConfiguration="MyServiceBehavior" name="MyServiceLibrary.MyServiceType">

<endpoint address="ApplierOne" binding="basicHttpBinding" name="MyEndpoint"

contract="MyServiceLibrary.IMyService" />

</service>

<service behaviorConfiguration="MyServiceBehavior" name="Two">

<endpoint address="ApplierTwo" binding="basicHttpBinding" name="MyEndpoint"

contract="MyServiceLibrary.IMyService" />

</service>

</services>

</system.serviceModel>

</configuration>

[ServiceContract]

public interface IMyService

{

[OperationContract]

void MyOperation(string msg);

}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]

public class MyService : IMyService

{

public void MyOperation(string msg)

{

System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " " + msg);

}

}