WCF包含返回值方法的双工,无法添加引用
[ServiceContract(CallbackContract = typeof(ISomeCallbackContract))] public interface IService { [OperationContract(IsOneWay = true)] void DoSomething(); [OperationContract(IsOneWay = true)] Void DoSomething2(); } //用于回调的契约 public interface ISomeCallbackContract { [OperationContract(IsOneWay = true)] void SomeCallbackMethod(); [OperationContract(IsOneWay = true)] void SomeCallbackMethod3(); }
实现类:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)] public class MyService : IService { static List<ISomeCallbackContract> m_Callbacks = new List<ISomeCallbackContract>(); public void DoSomething() { //获取调用客户端的实例通道,使用这个引用可以调用客户端方法 ISomeCallbackContract callback = OperationContext.Current.GetCallbackChannel<ISomeCallbackContract>(); if (m_Callbacks.Contains(callback) == false) //确定某元素是否在m_Callbacks集合中。 { m_Callbacks.Add(callback); } Console.WriteLine("这句话是在服务端执行的"); //Thread.Sleep(5000); //模拟长时间运行 //调用客户端实现的SomeCallbackMethod方法 callback.SomeCallbackMethod(); } public Void DoSomething2() { Console.WriteLine("fgf"); } }
上面的双工WCF服务,包含两个方法,都没有返回值。结果没有任何问题。现在把DoSomething2方法修改成返回字符串类型,结果就悲剧了,客户端不能添加服务引用:
[ServiceContract(CallbackContract = typeof(ISomeCallbackContract))] public interface IService { [OperationContract(IsOneWay = true)] void DoSomething(); [OperationContract(IsOneWay = true)] string DoSomething2(); } //用于回调的契约 public interface ISomeCallbackContract { [OperationContract(IsOneWay = true)] void SomeCallbackMethod(); [OperationContract(IsOneWay = true)] void SomeCallbackMethod3(); }
实现类:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)] public class MyService : IService { static List<ISomeCallbackContract> m_Callbacks = new List<ISomeCallbackContract>(); public void DoSomething() { //获取调用客户端的实例通道,使用这个引用可以调用客户端方法 ISomeCallbackContract callback = OperationContext.Current.GetCallbackChannel<ISomeCallbackContract>(); if (m_Callbacks.Contains(callback) == false) //确定某元素是否在m_Callbacks集合中。 { m_Callbacks.Add(callback); } Console.WriteLine("这句话是在服务端执行的"); //Thread.Sleep(5000); //模拟长时间运行 //调用客户端实现的SomeCallbackMethod方法 callback.SomeCallbackMethod(); } public string DoSomething2() { Console.WriteLine("fgf"); return "dfdf"; } }
解决方案:100分
[OperationContract([color=red]IsOneWay[/red]] = true)]
string DoSomething2();
又要返回一个结果,又告诉wcf只单程发送,自相矛盾。
:
有返回值时,把IsOneWay去掉。