I have a silverlight client that can call a method on my WCF web service to get values from a database. In the case of long query, I want to give the user the option to cancel it.
Client side, when the user press “Cancel”, I call “.Abort()” on my WCF instance. However, my WCF service will still continue its operation as it do not know that the client is no longer connected.
My question is: is it possible for a WCF service to know that the client has killed the connection?
I have tried suscribing to the Channel.Closed/faulted events in my contract method, but they never fire…
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
internal class MyContract: IMyContract
{
public object QueryDatabase(...)
{
System.ServiceModel.OperationContext.Current.Channel.Faulted += new System.EventHandler(Channel_Faulted);
System.ServiceModel.OperationContext.Current.Channel.Closed += new System.EventHandler(Channel_Faulted);
// Perform sql query
}
void Channel_Faulted(object sender, System.EventArgs e)
{
// Cancel SQL query here
}
}
Anyone know how to do this?
Thanks!
The Faulted and Closed events can indeed be used in the way you are using them in your code sample, but only if the binding you are using supports reliable sessions, and you have reliable sessions enabled. Check if the binding you are using supports it, and if so enable it.
If your binding does not support this, then you might consider switching to one that does.
You can find a list of what is supported for the different bindings here.