我正在尝试实现WCF回调。当我在服务端调用回调方法时,它会异步执行。有没有办法等待回调方法在服务方法进入下一个语句之前完成执行,而不从回调中向服务返回值?
//Server Side
public interface ICallback
{
[OperationContract]
void Oncallback1(string str);
[OperationContract]
int Oncallback2(string str);
}
[ServiceContract(CallbackContract = typeof(ICallback))]
public interface IServiceContract
{
[OperationContract]
void Method1();
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class MycallbackService : IServiceContract
{
public void Method1()
{
ICallback objIcallback = OperationContext.Current.GetCallbackChannel<ICallback>();
Thread.Sleep(5000);
objIcallback.Oncallback1(obj1);
Console.WriteLine("calling callback2");
Console.WriteLine(objIcallback.Oncallback2("2"));
Console.WriteLine("Service Execution over!");
}
}
//Client Side
[CallbackBehavior]
public class callbackClient : ICallback
{
public void Oncallback1(CallbackService.EventInfo obj)
{
Thread.Sleep(5000);
Console.WriteLine("Callback 1");
}
public int Oncallback2(string str)
{
Thread.Sleep(5000);
Console.WriteLine("Callback 4");
return 2;
}
}
这里声明“Console.WriteLine(”call callback2“);”在callback1方法完成之前执行。然而它一直等到callback2结束。
答案 0 :(得分:2)
我认为这是因为回调方法的返回类型不同:
void Oncallback1(string str);
int Oncallback2(string str);
Oncallback1
不返回任何内容,因此默认情况下它是IsOneWay = true
即发即弃操作。另一方面,Oncallback2
默认为IsOneWay = false