WCF InProcFactory error
- by Terence Lewis
I'm using IDesign's ServiceModelEx assembly to provide additional functionality over and above what's available in standard WCF.
In particular I'm making use of InProcFactory to host some WCF services within my process using Named Pipes. However, my process also declares a TCP endpoint in its configuration file, which I host and open when the process starts.
At some later point, when I try to host a second instance of this service using the InProcFactory through the named pipe (from a different service in the same process), for some reason it picks up the TCP endpoint in the configuration file and tries to re-host this endpoint, which throws an exception as the TCP port is already in use from the first hosting.
Here is the relevant code from InProcFactory.cs in ServiceModelEx:
static HostRecord GetHostRecord<S,I>() where I : class
where S : class,I
{
HostRecord hostRecord;
if(m_Hosts.ContainsKey(typeof(S)))
{
hostRecord = m_Hosts[typeof(S)];
}
else
{
ServiceHost<S> host;
if(m_Singletons.ContainsKey(typeof(S)))
{
S singleton = m_Singletons[typeof(S)] as S;
Debug.Assert(singleton != null);
host = new ServiceHost<S>(singleton,BaseAddress);
}
else
{
host = new ServiceHost<S>(BaseAddress);
}
string address = BaseAddress.ToString() + Guid.NewGuid().ToString();
hostRecord = new HostRecord(host,address);
m_Hosts.Add(typeof(S),hostRecord);
host.AddServiceEndpoint(typeof(I),Binding,address);
if(m_Throttles.ContainsKey(typeof(S)))
{
host.SetThrottle(m_Throttles[typeof(S)]);
}
// This line fails because it tries to open two endpoints, instead of just the named-pipe one
host.Open();
}
return hostRecord;
}