I am testing out the async
functionality in WCF and noticed following strange behavior:
Having the following two methods in IService.cs
[OperationContract]
string LongRunningMethod(int timeout, string message);
[OperationContract]
Task<string> LongRunningMethodAsync(int timeout, string message);
and Service.cs
public string LongRunningMethod(int timeout, string message)
{
Thread.Sleep(timeout);
return message;
}
public async Task<string> LongRunningMethodAsync(int timeout, string message)
{
await Task.Run(() => Thread.Sleep(timeout));
return message;
}
I get the exception
An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll
Additional information: Cannot have two operations in the same contract with the same name, methods LongRunningMethodAsync and LongRunningMethod in type WcfService.IService violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.
as soon as the WCF attempt to open the service:
public Server()
{
service = new Service("A fixed ctor test value that the service should return.");
svh = new ServiceHost(service);
}
public void Open(string ipAdress, string port)
{
svh.AddServiceEndpoint(
typeof(IService),
new NetTcpBinding(),
"net.tcp://"+ ipAdress + ":" + port);
svh.Open(); // exception is thrown here
}
If I rename LongRunningMethodAsync
into LongRunningMethodAsyncNew
I do not get the exception. On the client-side having the two functions LongRunningMethodAsync
and LongRunningMethodAsync
that call the respective server versions does not cause any issues:
public class Client : IMyEvents
{
ChannelFactory<IService> scf;
IService s;
public void OpenConnection(string ipAddress, string port)
{
var binding = new NetTcpBinding();
scf = new DuplexChannelFactory<IService>(
new InstanceContext(this),
binding,
"net.tcp://" + ipAddress + ":" + port);
s = scf.CreateChannel();
}
public string LongRunningMethod(int timeout, string message)
{
return s.LongRunningMethod(timeout, message);
}
public async Task<string> LongRunningMethodAsync(int timeout, string message)
{
return await s.LongRunningMethodAsyncNew(timeout, message);
}
}
What is going on here?