Issue
I created a WCF singleton service with netNamedPipeBinding. When a channel exception occurs, it leaves a channel in a faulty state, and all subsequent operations throws exceptions. How can I prevent this? I want that a TimeoutException, or any of the other common exceptions, to make only one operation fail, and not to make the service unresponsive.
Solution
There is another way. You can instantiate the client proxy for each request instead of using a single instance for more than one request. This way if the channel enters a faulty state, it is discarded anyway.
This is a bit tricky because you shouldn't dispose a channel, besides it being IDisposable.
It won't work:
using(var channel = channelFactory.CreateChannel())
{
return channel.ServiceMethod(parameter);
}
Instead you should:
public static class Service<T>
{
public static ChannelFactory<T> _channelFactory = new ChannelFactory<T>("");
public static TResult Use<TResult>(Func<T, TResult> func)
{
TResult output;
var channel = (IClientChannel)_channelFactory.CreateChannel();
bool success = false;
try
{
output = func((T)proxy);
channel.Close();
success = true;
}
finally
{
if (!success)
{
proxy.Abort();
}
}
return output;
}
}
return Service<IService>.Use(channel =>
{
return channel.ServiceMethod(parameter);
});
Answered By - Jader Dias Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.