WCF雙向通訊小Demo
自己使用總結,方便理解。
1、定義wcf服務介面類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel;
namespace WcfCallbackService { /// <summary> /// 定義回撥契約CallbackContract屬性指定 /// </summary> [ServiceContract(CallbackContract = typeof(ICallback))] public interface ICalculator { [OperationContract(IsOneWay = true)] void Add(double x, double y);
[OperationContract(IsOneWay = false)] double Minus(double x, double y); } }
2、定義回撥協定介面
using System; using System.Collections.Generic; using System.Text; using System.Linq; using System.ServiceModel;
namespace WcfCallbackService { /// <summary> /// 回撥協定介面 /// </summary> public interface ICallback { [OperationContract(IsOneWay =true)] void DisplayResult(double x, double y, double result); } }
3、服務具體實現類CalculatorService
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel;
namespace WcfCallbackService { [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] public class CalculatorService: ICalculator { public void Add(double x, double y) { double result = x + y; ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>(); callback.DisplayResult(x, y, result); Console.WriteLine("客戶端已經正常顯示結果為:" + result.ToString()); }
public double Minus(double x, double y) { double result = x - y; return result; }
/// <summary> /// 輸出執行過程概要日誌 /// </summary> public event Action<string> OnLogOutPut;
/// <summary> /// 概要日誌 /// </summary> /// <param name="summaryInfo"></param> protected void LogOutPut(string summaryInfo) { if (OnLogOutPut != null) { OnLogOutPut(summaryInfo); } }
} }
4、服務啟動入口類
using System; using System.Collections.Generic; using System.ServiceModel; using System.Text; using WcfCallbackService;
namespace WcfCallbackServiceWrapper { class Program { static void Main(string[] args) { StartServer(); } /// <summary> /// 啟動伺服器 /// </summary> public static void StartServer() { Console.WriteLine("開啟服務");
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService)); NetTcpBinding binding = new NetTcpBinding { Security = { Mode = SecurityMode.None, Transport = { ClientCredentialType = TcpClientCredentialType.None }, Message = { ClientCredentialType = MessageCredentialType.None } }, MaxBufferPoolSize = 2147483647, MaxReceivedMessageSize = 2147483647, ReceiveTimeout = new TimeSpan(0, 0, 10, 0), SendTimeout = new TimeSpan(0, 0, 10, 0) }; serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, "net.tcp://127.0.0.1:2017"); serviceHost.Open(); Console.WriteLine("開啟服務成功"); Console.ReadKey(); } } }
5、客戶端回撥協定介面實現類CalculateCallback
using System; using System.Collections.Generic; using System.Text; using WcfCallbackService;
namespace WcfCallbackClient { /// <summary> /// 回撥協定介面的實現類 /// </summary> public class CalculateCallback: ICallback { public void DisplayResult(double x, double y, double result) { Console.WriteLine("x + y = {2} when x = {0} and y = {1}", x, y, result); } } }
6、客戶端繫結相關屬性類
using System; using System.Collections.Generic; using System.Linq; using System.Net.NetworkInformation; using System.ServiceModel; using System.Text;
namespace WcfCallbackClient { /// <summary> /// 繫結相關屬性類 /// </summary> public static class TcpBindingInfo { private static string Ip = "127.0.0.1:2017";
/// <summary> /// 繫結主客戶端地址 /// </summary> private static EndpointAddress _ipaddress;
/// <summary> /// 繫結 /// </summary> private static NetTcpBinding _tcpBinding;
public static EndpointAddress Endpointaddress { set { _ipaddress = value; } get { _ipaddress = new EndpointAddress($"net.tcp://{Ip}"); return _ipaddress; } }
public static NetTcpBinding TcpBinding { set { _tcpBinding = value; } get { _tcpBinding = new NetTcpBinding { Security = { Mode = SecurityMode.None, Transport = { ClientCredentialType = TcpClientCredentialType.None }, Message = { ClientCredentialType = MessageCredentialType.None } }, MaxBufferPoolSize = 2147483647, MaxReceivedMessageSize = 2147483647, ReceiveTimeout = new TimeSpan(0, 0, 10, 0), SendTimeout = new TimeSpan(0, 0, 10, 0) }; return _tcpBinding; } } } }
7、客戶端呼叫服務
using System; using System.Collections.Generic; using System.ServiceModel; using System.Text; using WcfCallbackService;
namespace WcfCallbackClient { public class Program { static void Main(string[] args) { AddCallback(); Minus(20, 1); Console.Read();
}
/// <summary> /// 加法回撥 /// </summary> public static void AddCallback() { InstanceContext instanceContext = new InstanceContext(new CalculateCallback()); DuplexChannelFactory<ICalculator> channelFactory = new DuplexChannelFactory<ICalculator>(instanceContext, TcpBindingInfo.TcpBinding); ICalculator proxy = channelFactory.CreateChannel(TcpBindingInfo.Endpointaddress); if (proxy != null) { proxy.Add(2, 3); } }
/// <summary> /// 呼叫減法服務 /// </summary> /// <param name="x"></param> /// <param name="y"></param> public static void Minus(double x, double y) { InstanceContext instanceContext = new InstanceContext(new CalculateCallback()); DuplexChannelFactory<ICalculator> channelFactory = new DuplexChannelFactory<ICalculator>(instanceContext, TcpBindingInfo.TcpBinding); ICalculator proxy = channelFactory.CreateChannel(TcpBindingInfo.Endpointaddress); if (proxy != null) { double value = proxy.Minus(x, y); } Console.WriteLine($"x-y={x - y}"); } } }