I get the following exception (Cannot have two operations in the same contract with the same name, methods ExecuteAsync and Execute) when the following service is activated.
[ServiceContract]
public interface IMyService
{
[OperationContract]
byte[] Execute(MyRequest request);
[OperationContract]
Task<byte[]> ExecuteAsync(MyRequest request);
}
I guess this makes sense if you are using the svcutil.exe to create your service reference, because the task-based operations are created automatically for you. However, I don't want to add a service reference and instead just use the standard ChannelFactory to create the WCF Channel. Is there any other way this is possible without renaming the async method to something else? Or must I wrap the sync method on the client in a Task.Run?
The above is not valid because WCF itself makes two methods for each OperationContract in your case Execute()
one synchronous and second asynchronous, which can be called on client side by writing ServiceClientObj.ExexuteAsync(request)
, so you don't need to add async method explicitly in the IMyService
.The Framework is itself responsible for generating async
method of each Operation
Here's what I did. I have two separate contracts. One for the client and one for the server:
namespace ServiceLibrary.Server
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
byte[] Execute(MyRequest request);
}
}
namespace ServiceLibrary.Client
{
[ServiceContract]
public interface IMyService : Server.IMyService
{
[OperationContract]
Task<byte[]> ExecuteAsync(MyRequest request);
}
}
Because both ServiceContracts share the same name, the OperationContracts' Action and ReplyAction are the same for the async and sync methods. The client now has both the sync and async version and the server stays unmodified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With