I found numerous questions about this issue : here, here, here, and here, But could not solve my problem.
Background:
We have an existing unmanaged application that launches a managed winforms application. There is a shared managed assembly which exposes comvisible objects for the unmanaged app and that assembly launches the managed app. So the ServiceHost runs on the winforms app and the client on that shared assembly. The setup is similar to this implementation . The application runs on a approx. 100 pcs, win xp or win 7.
The problem:
once in a few days we get : System.ServiceModel.AddressAlreadyInUseException when trying to launch the winforms application. we ran netstat but could not find anything listening on that port. The only 'solution' is rebooting the pc.
I could not reproduce this problem on my dev machine. but it happens on production environment once in a while.
Code:
Code is being typed hopefully without typos, can not copy paste :
Service config (found in winform appconfig):
<system.ServiceModel>
.
.
<services>
<service name="MyService">
<endpoint address="net.tcp://localhost:4444" binding="netTcpBinding"
contract="IMyService"/>
</service>
</services>
.
.
<\system.ServiceModel>
Service
StartService() is called from mainform_Load
private static void StartService()
{
try
{
_serviceHost = new ServiceHost(typeof(MyService));
_serviceHost.Open();
}
catch(Exception ex)
{
LogError(ex);
ExitApp();
}
}
private static void ExitApp()
{
try
{
_serviceHost.Close();
}
catch(CommunicationException cex)
{
LogError(cex);
_serviceHost.Abort();
}
finally
{
Application.Exit();
}
}
Client
private static void CallMyService()
{
var channelFactory = new ChannelFactory<IMyService>("net.tcp://localhost:4444");
IMyService myChannel= channelFactory.CreateChannel();
bool error = true;
try
{
myChannel.PerformOperation();
((IClientChannel)myChannel).Close();
error = false;
}
finally
{
if (error)
{
((IClientChannel)myChannel).Abort();
}
}
}
I hope i am clear enough, thanks.