How to dispose WCF services correctly? - wcf

My WCF service is IDisposable because it uses a ReadWriterLockSlim. When I dispose both of them in the following method:
public void Dispose()
{
lockSlim.Dispose();
}
Some of the public methods are still running (or accepting new connections, I don't know), and it fires exceptions because of the attempts of using disposed objects (the lockSlim in this case). It would be nicer if, when stopping a Windows Service hosted WCF Service, the Dispose method was called after stopping all running methods and closing it for new connections. But it seems that it must be done manually. There are any shortcuts or examples?

I think there is no need to dispose LockSlim object. On service stop, call ServiceHost.Close() . all the threads will interrupr its work, all the links will be broken and Garbage colector will take care about the memory. ReadWriterLockSlim is not critical resource. Please let me know if it was helpfull

Related

ASP.NET, WCF and per-operation static variables - how to use them safely?

I have a WCF service and I have the following (simplified) class:
public class PerOperationSingleton : IDisposable
{
private static bool _hasInstance = false;
public PerOperationSingleton()
{
if(_hasInstance)
throw new InvalidOperationException("Cannot have multiple instances during a single WCF operation");
_hasInstance = true;
}
public void Dispose()
{
_hasInstance = false;
}
}
I guess, it's pretty self explanatory piece of code. I don't need a singleton for entire WCF service but only during a single operation call. If one instance of the PerOperationSingleton is disposed, it should be safe to create a new instance during the same WCF operation.
The problem is that I don't know how to make the _hasInstance variable to be effective only for one WCF operation. I know about [ThreadStatic], but I've heard that ASP.NET and WCF do not guarantee that an operation will be executed on a single thread - it might be transferred to another thread.
I definitely don't want my _hasInstance = true to move to thread pool and get incorrectly detected if some other operation picks that thread from the pool.
If WCF operation moves to another thread, I would like the _hasInstance variable to keep the "true" value if it was set.
And I don't want to change some global settings for my WCF service to avoid affecting the performance or get into some problems which will be hard to debug and solve later (I don't feel proficient enough in advanced ASP.NET and WCF topics).
I cannot store _hasInstance in session either because my client requested to disable .NET sessions for various reasons.
I would like the class PerOperationSingleton actually to be environment agnostic. It shouldn't really know anything about WCF or ASP.NET.
How do I make _hasInstance variable static during entire call of my WCF operation and don't affect other WCF operations?
I would consider using OperationContext to make you data "static" during the operation call.
Here is a similar discussion Where to store data for current WCF call? Is ThreadStatic safe?

WCF: keeping a client alive and time to close it

I have a wrapper around a WCF service APIs
Class APIWrapper
{
private WCFClient _client;
//constructor opens a client connection
public APIWrapper()
{
_client = new WCFClient();
_client.open();
}
public API1()
{
_client.doSomething1();
}
public API2()
{
_client.doSomething2();
}
}
I want to ask:
Q1 will timeout exception occur? if this wrapper class instance exists for too long? (does the WCF connection by default keepalive? without setting that attribute in config)
for example, after a wrapper class is constructed, its API1 or API2 is called after 10mins, which is longer than the timeout value of this WCF connection.
Q2 Do I need explicitly close the connection, if so, should I do it in the destructor of the wrapper class like below?
~APIWrapper
{
if(_client !=null)
try{
_client.close(); }
catch(Exception e){
_client.Abort(); }
}
I am not sure why you wanna do that, but if the WCF is hosted in a IIS7, the WCF will start with or without connections, there is no point keeping a connection alive.
In my experience, those kind of services works best when are stateless (unless you have a really good reason). I strongly suggest opening and closing the connection each time. If you are doing this for performance, there are another ways to avoid closing and opening each time.
Q1: According to MSDN the openTimeout is 1 minute
http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.binding.opentimeout.aspx
Q2: you do not need to close the connection explicitly, but it's a good practice and I strongly recommend to do it. Non closing the connections could lead in a overhead on the WCF.
As I know, WCF do no keep the connection alive. After a predefined time passes (inactivityTimeout="00:10:00"), the connection will throw an exception when you try to call _client.doSomething1() or any other method on the service.
WCF inactivity timeout
To keep connection alive you should call a simple method at predefined intervals, lets say every 1 minute.
However, I agree with Jordi about that you should use wcf services stateless until it is realy necessary.

WCF, Unity: Teardown disposable instances

we've set up a WCF service which uses a Unity Container to resolve instances for managing Exchange 2010 Powershell commands. We defined an IExchangePowershell interface which has a concrete implementation that implements IDisposable. After some time we encountered the problem that we couldnt execute powershell commands anymore since the server said there are already too many powershell sessions open. It seems that we never really disposed of our powershell instances. The Dispose() method of the concrete Powershell would take care of closing the runspace and the session. Once I call this in the repository methods, we don't get the errors anymore.
((IDisposable)this.powershell).Dispose();
Now of course I dont want to explicitly call dispose in every repository method. I thought unity could take care of this. Our WCF Instance Provider does this:
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
container.Teardown(instance);
}
But that doesnt really dispose of the IExchangePowershell instances. Do you have any idea how I could automatically dispose of those instances?
That is actually well known problem in Unity. TearDown method does nothing. If you want to use TearDown you must create custom container extension.
I wrote an article about using object lifetimes managers in Unity and their impact on disposing. If you use default TransientLifetimeManager or PerResolveLifetimeManager the Unity will even don't track existence of your objects so it can't call Dispose. The only lifetime managers which calls Dispose on resolved instances are ContainerControlledLifetimeManager (aka singleton) and HierarchicalLifetimeManager. The Dispose is called when the lifetime manager is disposed.
The solution for you is either use casting and handle Dispose manually as you already did or switch to HiearchicalLifetimeManager and create new subcontainer for each incoming WCF request. Each subcontainer will only handle single request and it will dispose resolved entities with hiearchical lifetime.
There are other ways, for example this article builds a very complex code around Unity to support disposing and TearDown for all resolved objects.
The answer depends on how you register your type / instance with unity. Standard implementation of Teardown does nothing at all.
If you register type then Unity does not store reference to instance it creates - it's up to you to manage it's lifetime and dispose it. If you register instance, then the instance lifetime is managed by unity and is kept until you dispose of the container.
The link below helps to understand a bit better about lifetime management:
http://msdn.microsoft.com/en-us/library/ff648098.aspx
You need to ask yourself when you would like your objects to be disposed. If you know when to call ReleaseInstance, you might as well call IDispose instead of it.
(I'm sorry, I'm not familiar with WCF, so I'm not sure what instance provide is in this context)

how to create and manage wcf service clients?

At first I treated them as any dependency passing them in the ctor to the class consuming the service:
var serviceConsumer = new ServiceConsumer(new MailingServiceClient())
The problem was that once an exception was thrown from the service it entered a faulted state and would not reply to any requests, so re-initialization was due.
Further more, I became familiar with the fact that wcf services may not be disposed properly on several occasions, because the dispose method of the generated proxy is broken (calls close without checking the fault state) and encountered a couple of ways to overcome that:
wrapping every call to every service.
override the default IDisposable behavior of the scvutil.exe-generated class
Since I wouldn't like the consumer code to create the service client himself for testing reasons, my questions are:
how can I maintain a working un-faulted service?
how can I still use dependency injection for testability?
Found a nice solution.
a proxy generator that replaces the VS one, and generates a wrapper around the default proxy that deals with proxy faults, timeouts, and correct disposal.
http://wcfproxygenerator.codeplex.com/
seems to be working fine for me.

Not disposing of a WCF proxy?

I have a WCF service which is a singleton and which manages a collection of proxies to another WCF service which is session-based. The singleton creates a new proxy and passes it some work to do, and the session-based service makes calls back to the singleton service when certain activities complete (pretty much all of the OperationContract methods are one-way). The typical completion path is that the singleton receives an event from a hardware device when the process is complete, and it calls a method on the session-based service which returns the final status, and then disposes of its proxy. When an error situation is encountered and the session-based service can't continue with what it needs to do, I need to make a call back to the singleton to let it know to dispose of the proxy associated with that instance. However, to make the WCF method call back to the singleton, I need to create a proxy back to the singleton. What I'm seeing happen is that the singleton disposes of its proxy as expected, but every time the proxy to the singleton that the session-based service created times out when I try to dispose of it. Since the session ends with that method call and the instance will be disposed of, does it matter if the proxy it created doesn't get properly disposed?
Disposing of a WCF service is not the same as disposing of any other object that implements IDisposable. It is ok to dispose of the service when it is in a good state but when a fault occurs the dispose method will throw another exception.
Some insight is here and perhaps use the WCFProxyGenerator but I have not tried it
Not sure exactly the problem with the session based service and whether it is different from the WCF service.
My recommendation is not to use a singleton but use Dependency Injection to give the class that uses the WCF service a factory so when it wants the service it can create it. And when a fault occurs it can throw away the old and create a new one. Without seeing some code it is hard to see if this is possible.