标签:
创建服务契约
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ICallBack))]
public interface IUser
{
[OperationContract]
string GetName(string name);
}
回调
public interface ICallBack
{
[OperationContract(IsOneWay=true)]
void PrintName(string name);
}
服务中具体操作的实现
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class User:IUser
{
ICallBack callback = null;
public User()
{
callback = OperationContext.Current.GetCallbackChannel<ICallBack>();
}
public string GetName(string name)
{
callback.PrintName(name);
return name;
}
}
客服端实现回调接口
class Hand : Client.ServiceReference1.IUserCallback
{
public void PrintName(string name)
{
Console.WriteLine("服务器调用客服端的名字是{0}", name);
}
}
客服端调用服务端
static void Main(string[] args)
{
InstanceContext instanceContext = new InstanceContext(new Hand());
// Create a client
ServiceReference1.UserClient client = new ServiceReference1.UserClient(instanceContext);
string name= client.GetName("Aniy");
Console.WriteLine("服务器直接返回的名字是:" + name);
Console.ReadKey();
}
执行效果
demo路径WCF双工.rar
标签:
原文地址:http://www.cnblogs.com/aniy129/p/4196260.html