Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF async implementation with 'normal' interface

Every article I have read about doing an async implementation for a WCF service requires modifying the interface for the WCF service (e.g. the return type of the operation goes from T to Task). However, I don't want to modify the operation signature as I have client code that doesn't know anything about the Task<> type. I just want the implementation to be synchronous.

I could implement the existing synchronous entry points by having a 'wrapper' method that makes a call to an internal async implementation method and then does an await, but that would block the thread calling into the wrapper, and that defeats the purpose of having an async implementation (which is to free up those threads so that they can serve other requests).

So, I need WCF to know that the implementation is async, but also know that the interface is not.

like image 894
Terry Coatta Avatar asked Feb 13 '23 18:02

Terry Coatta


1 Answers

The "Asyncness" of the server does not affect the "Asyncness" of the client. For example on a project I am working on right now I have the following on the server

[ServiceContract(Namespace = "http://example.com/Example/v1", ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public interface IClientsUploader
{
    [OperationContract()]
    Task<Results> UploadClientsAsync(Database database, Client[] clients);
}

but on the client side I have the contract

[System.ServiceModel.ServiceContractAttribute(Namespace="http://example.com/Example/v1", ConfigurationName="Example.IClientsUploader")]
public interface IClientsUploader
{
    [System.ServiceModel.OperationContractAttribute(Action="http://example.com/Example/v1/IClientsUploader/UploadClients", ReplyAction="http://example.com/Example/v1/IClientUploader/UploadClientsResponse")]
    Example.DataStructures.Results UploadClients(Example.DataStructures.Database database, System.Collections.Generic.IEnumerable<Example.DataStructures.Client> clients);
}

And it works perfectly fine. WCF notices the Task XxxxAsync pattern and turns it in to a endpoint without the Task<T> return type and Async suffix. However when a operation comes in the operation can be processed using things like await inside it.

like image 95
Scott Chamberlain Avatar answered Feb 20 '23 01:02

Scott Chamberlain