I want to use CancellationToken in one of async tasks in my WCF service. the implementation is as follows:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public partial class MyService: IMyService
{
private void Initialize()
{
_serviceHost = new ServiceHost(this, new Uri[] { new Uri("net.pipe://localhost") });
var binding = new NetNamedPipeBinding();
_serviceHost.AddServiceEndpoint(typeof(IMyService), binding, "MyService");
var behavior = _serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
behavior.InstanceContextMode = InstanceContextMode.Single;
_serviceHost.Open(); // I get error on this line
}
public async Task<List<object>> DoSomeWorkAsync(CancellationToken ct = default(CancellationToken))
{
//do some work
...
var list = await SomeAwaitableTask(ct);
return list;
}
}
There is no way to cross service boundaries and cancel on the server side.
You can do this using WCF client-side and task-based async patterns by registering an Abort action with a cancellation token Or use the extension method WithCancellation of Microsoft.VisualStudio.Threading.ThreadingTools.
How to cancel an async WCF call?<\/a>
What's the best way to cancel an asynchronous WCF request?<\/a>
How to use the CancellationToken property?<\/a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.