服务器
[ServiceContract(Namespace="http://www.artech.com/", CallbackContract = typeof(ICallback))]
服务接口加入CallbackContract
//添加回调接口,用户客户端回调的实现,客户端代理会自动生成这个接口。
public interface ICallback
{
[OperationContract(IsOneWay=true)]
void DisplayResult(double x, double y, double result);
}
[OperationContract(IsOneWay=true)]
void AddTest(double x, double y);
public void AddTest(double x, double y)
{
double result = x + y;
//回调客户端接口的实现方法。
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
callback.DisplayResult(x, y, result);
}
客户端
实现接口
class CalculateCallback : IBLLServiceCallback
{
public void DisplayResult(double x, double y, double result)
{
Console.WriteLine("x + y = {2} when x = {0} and y = {1}", x, y, result);
}
}
调用
InstanceContext instanceContext = new InstanceContext(new CalculateCallback());
demoClient.AddTest(1,2);