[ServiceContract(Namespace = "http://xx.com", CallbackContract = typeof(Ipub_c))]
public interface Ipub_s
{
[OperationContract(IsOneWay = true)]
void toserver(string cs);
}
public interface Ipub_c
{
[OperationContract(IsOneWay = true)]
void toclient(string cs);
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class Bpub : Ipub_s
{
public void toserver(string cs)
{
Ipub_c callback = OperationContext.Current.GetCallbackChannel<Ipub_c>();
for (int i = 0; i < 1000; i++)
{
System.Threading.Thread.Sleep(1000);
callback.toclient(cs + " | " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + " | " + cs);
}
}
}
ServiceHost host = new ServiceHost(typeof(Bpub));
host.AddServiceEndpoint(typeof(Ipub_s), new NetTcpBinding(), "net.tcp://localhost/dd");
host.Opened += (s, ek) =>
{
this.Invoke((Action)delegate
{
button1.Enabled = false;
button1.Text = "已启动";
Console.WriteLine("HelloService已经启动,按按回车键终止服务!");
});
};
host.Open();
InstanceContext instanceContext = new InstanceContext(new Bcpub());
DuplexChannelFactory<WcfSgS.Ipub_s> channel = new DuplexChannelFactory<Ipub_s>(instanceContext, new NetTcpBinding(), "net.tcp://localhost/dd");
Ipub_s proxy = channel.CreateChannel();
proxy.toserver("dddds");