接口:
[ServiceContract(CallbackContract = typeof(IViewCallback), SessionMode = SessionMode.Required)] public interface IViewService { [OperationContract] void ServiceTest(); }
类:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] public class ViewService : IViewService { //在ViewLibary中的ViewManager中注册 public event Action<ClientData> ServiceTestEvent;//用于客户端测试与服务端的WCF连接事件 private ClientData GetClientData() { OperationContext context = OperationContext.Current; MessageProperties properties = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; return new ClientData(endpoint.Address, endpoint.Port); } void IViewService.ServiceTest() { ClientData client = GetClientData(); ServiceTestEvent?.Invoke(client); } } public class ClientData { private string _IP; private int _Port; public string IP { get { return _IP; } set { _IP = value; } } public int Port { get { return _Port; } set { _Port = value; } } public ClientData(string ip, int port) { _IP = ip; _Port = port; } }
参数定义:
private ServiceHost _WcfHost = null; private ViewService _MainService = null; private int _MetaPort = 58942; private int _DataPort = 58946; public ServiceHost WcfHost { get { return _WcfHost; } } public ViewService MainService { get { return _MainService; } } public int MetaPort { get { return _MetaPort; } } public int DataPort { get { return _DataPort; } }
初始化服务端:
private void InitialHost() { #region TCP //Uri tcpa = new Uri(string.Format("net.tcp://127.0.0.1:{0}/AGVS/ViewService/", DataPort)); //_WpfHost = new ServiceHost(MainService, tcpa); //NetTcpBinding tcpb = new NetTcpBinding(); //ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior(); //_WpfHost.Description.Behaviors.Add(mBehave); //_WpfHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); //_WpfHost.AddServiceEndpoint(typeof(IViewService), tcpb, tcpa); #endregion #region HTTP Uri baseAddress = new Uri(string.Format("http://localhost:{0}/AGVS/ViewService/", MetaPort)); _WcfHost = new ServiceHost(MainService, baseAddress);//MainService用于处理客户端请求 string tcpAddress = string.Format("net.tcp://127.0.0.1:{0}/AGVS/ViewService/", DataPort); NetTcpBinding tcpBinding = new NetTcpBinding(); tcpBinding.Security.Mode = SecurityMode.None; ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior(); _WcfHost.Description.Behaviors.Add(mBehave); WcfHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); WcfHost.AddServiceEndpoint(typeof(IViewService), tcpBinding, tcpAddress); #endregion ServiceDebugBehavior debugBehavior = WcfHost.Description.Behaviors.Find<ServiceDebugBehavior>(); if (debugBehavior == null) { debugBehavior = new ServiceDebugBehavior(); debugBehavior.IncludeExceptionDetailInFaults = true; WcfHost.Description.Behaviors.Add(debugBehavior); } else { debugBehavior.IncludeExceptionDetailInFaults = true; } }
启动:
private void Main() { if (WcfHost.State == CommunicationState.Created) { try { WcfHost.Open(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
标签:return,string,中纯,WcfHost,private,WCF,new,public,Winform From: https://www.cnblogs.com/magicMaQaQ/p/18142796