契约:
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode = SessionMode.Required, CallbackContract = typeof(ITransferCallback))] 如果要使用回调,必须加上这句话 public interface IMonitorService { [OperationContract(IsOneWay = true)] void MonitorOnlineService(string strJson); }
[ServiceContract(CallbackContract = typeof(IMonitorService))] public interface ITransferCallback { [OperationContract(IsOneWay = true)] void ReturnResult(string strJson); }
server1
接口实现
public class TransferCallback : ITransferCallback { public void ReturnResult(string strJson) { Console.WriteLine("start service"); Monitor.MonitorOnlineService(strJson); } private IMonitorService Monitor { get { return OperationContext.Current.GetCallbackChannel<IMonitorService>(); } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="NewBehavior0"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="WcfServer.TransferCallback"> <endpoint address="net.tcp://127.0.0.1:8000/TransferCallback" binding="netTcpBinding" bindingConfiguration="" contract="WcfServer.ITransferCallback" /> <host> <baseAddresses> <add baseAddress="net.tcp://127.0.0.1:8000/TransferCallback" /> </baseAddresses> </host> </service> </services> <bindings> <basicHttpBinding> <binding name="basicHttpBind" closeTimeout="00:10:00" openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true" messageEncoding="Text"> <readerQuotas maxStringContentLength="2097152" /> <security mode="None" /> </binding> </basicHttpBinding> <netTcpBinding> <binding name="TransferNetTcpBind" closeTimeout="00:10:00" openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"> <readerQuotas maxStringContentLength="2097152"/> <security mode="None"></security> </binding> <binding name="NetTcpBinding_ITransferCallback" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Transport"> <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" /> <message clientCredentialType="Windows" /> </security> </binding> </netTcpBinding> </bindings> <client> <endpoint address="net.tcp://127.0.0.1:9000/MonitorService" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ITransferCallback" contract="IMonitorService" name="NetTcpBinding_ITransferCallback"> </endpoint> </client> </system.serviceModel> </configuration>
启用服务
class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(DataTransferCallback))) { ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (smb == null) host.Description.Behaviors.Add(new ServiceMetadataBehavior()); //暴露出元数据,以便能够让SvcUtil.exe自动生成配置文件 host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); //开启服务 host.Open(); Console.WriteLine("Service listen begin to listen..."); Console.WriteLine("press any key to teriminate..."); Console.ReadKey(); host.Abort(); host.Close(); } } }
Wcfserver2
public class MonitorService : IMonitorService { #region /// <summary> /// 写入集成控制平台数据 /// </summary> /// <param name="strJson">JSON字符串</param> /// <param name="strOrgCode">直属库编号</param> /// <param name="strTableName">表明</param> /// <returns></returns> public void MonitorOnlineService(string strJson) {Callback.ReturnResult(result); } #endregion ITransferCallback Callback { get { return OperationContext.Current.GetCallbackChannel<ITransferCallback>(); } } }
调用服务 new ChannelFactory<IDataTransferCallback>("NetTcpBinding_ITransferCallback").CreateChannel().ReturnResult("Hello");
<system.serviceModel> <client> <endpoint address="net.tcp://127.0.0.1:8000/TransferCallback" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ITransferCallback" contract="ITransferCallback" name="NetTcpBinding_ITransferCallback"> </endpoint> </client> <services> <service name="MonitorService"> <host> <baseAddresses> <add baseAddress="net.tcp://127.0.0.1:9000/MonitorService" /> </baseAddresses> </host> <endpoint address="net.tcp://127.0.0.1:9000/MonitorService" binding="netTcpBinding" bindingConfiguration="TransferNetTcpBind" contract="IMonitorService"/> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/> </service> </services> <bindings> <netTcpBinding> <binding name="TransferNetTcpBind" closeTimeout="00:10:00" openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"> <readerQuotas maxStringContentLength="2097152"/> <security mode="None"></security> </binding> <binding name="NetTcpBinding_ITransferCallback" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Transport"> <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" /> <message clientCredentialType="Windows" /> </security> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="False"/> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
这样服务就可以循环交互了,直到一个服务停止为止