In a project I’m using IPC (Inter Process Communication) for sharing objects of a Windows service with a client application.
This always worked correctly when the client application started the first time and used the objects of the Windows service. But starting the client application (without restarting the service) some minutes later again, resulted in a RemotingException with this strange error message:
Failed to write to an IPC Port: The pipe is being closed.
It took me quite long to figure out what the problem was:
as it is important for my service to really have only one instance of the remote object for the whole lifetime of the service, I used RemotingServices.Marshal to register a previously instantiated object. But I did not know that objects registered with this method are only valid for a very short time period (one minute) by default.
To fix that you need to override the MarshalByRefObject.InitializeLifetimeService method within your remote server object like this:
public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromDays(365); lease.SponsorshipTimeout = TimeSpan.FromDays(365); lease.RenewOnCallTime = TimeSpan.FromDays(365); } return lease; }
And here the same for VB .NET developers:
Public Overrides Function InitializeLifetimeService() As Object Dim lease As ILease = CType(MyBase.InitializeLifetimeService(), ILease) If lease.CurrentState = LeaseState.Initial Then lease.InitialLeaseTime = TimeSpan.FromDays(365) lease.SponsorshipTimeout = TimeSpan.FromDays(365) lease.RenewOnCallTime = TimeSpan.FromDays(365) End If Return lease End Function
OK, 365 days are probably a little bit exaggerated, but this way you can be really sure your remote objects won’t expire very fast.
Did you come across this problem yourself already, too?
This post is also available in Deutsch.