WCF雙向通訊netTCP

來源:互聯網
上載者:User

標籤:ota   div   logging   actor   color   body   template   api   reader   

 

一、服務端配置

<?xml version="1.0" encoding="utf-8" ?><configuration>    <system.serviceModel>        <behaviors>            <serviceBehaviors>                <behavior name="MyBehavior">                    <serviceMetadata httpGetEnabled="true" />                    <serviceDebug includeExceptionDetailInFaults="false" />                </behavior>            </serviceBehaviors>          <endpointBehaviors>            <behavior name="webBehavior">              <webHttp />            </behavior>          </endpointBehaviors>        </behaviors>        <services>            <service name="PayCommunicationWcf.Server.AliPay" >                <endpoint address="" binding="netTcpBinding"  contract="PayCommunicationWcf.Interface.IPay">                    <identity>                     <!--   <dns value="localhost" />-->                    </identity>                </endpoint>              <!--  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"  />              -->  <host>                    <baseAddresses>                        <add baseAddress="net.tcp://localhost:57000/sessionservice" />                    </baseAddresses>                </host>            </service>        </services>    <bindings>            <webHttpBinding>        <binding name="ApiExportBinding" maxReceivedMessageSize="10485760"                 maxBufferPoolSize="10485760" maxBufferSize="10485760" closeTimeout="00:03:00"                 openTimeout="00:03:00" receiveTimeout="00:10:00" sendTimeout="00:03:00">          <readerQuotas maxDepth="32" maxStringContentLength="10485760"                        maxArrayLength="10485760" maxBytesPerRead="10485760" />          <security mode="None" />        </binding>      </webHttpBinding>    </bindings>    </system.serviceModel></configuration>
View Code

二、服務端服務契約和服務實現

 1、服務實現

using System;using PayCommunicationWcf.Interface;using PayCommunicationWcf.Model;using PurClient.Logging;namespace PayCommunicationWcf.Server{    public class PayCallBack : IPayCallBack    {        public void SendResultOfPay(PayResultResponse result)        {                       Logger.GetInstance().Info(string.Format("已經接收到服務端發來的支付結果訊息,結果碼:{0},結果資訊:{1}。",result.Resultcode, result.Msg));                  }    }}
View Code

2、服務契約

 服務契約

using System.ServiceModel;using System.ServiceModel.Web;using PayCommunicationWcf.Model;namespace PayCommunicationWcf.Interface{    [ServiceContract(CallbackContract = typeof(IPayCallBack))]    public interface IPay    {       /* [WebInvoke(UriTemplate = "PayRequestTest", BodyStyle = WebMessageBodyStyle.Bare,Method = "*",RequestFormat =WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]      */  [OperationContract]        PayResultResponse  PayRequestTest(PayRequestInfo payData);      /*  [WebInvoke(UriTemplate = "PayRequest?payData={payData}", BodyStyle = WebMessageBodyStyle.Bare, Method = "*", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]  */      [OperationContract]        string PayRequest(string payData);    }}
View Code

回調服務契約,在用戶端中實現

using System.ServiceModel;using PayCommunicationWcf.Model;namespace PayCommunicationWcf.Interface{    [ServiceContract]    public interface IPayCallBack    {        [OperationContract(IsOneWay = true)]        void SendResultOfPay(PayResultResponse result);    }}
View Code

3、服務註冊

using System.ServiceModel;namespace PayCommunicationWcf.Server{    public static class ServiceRegister    {        /// <summary>        /// 通過反射註冊服務        /// </summary>        public static void RegisterAllService()        {                       ServiceHost host = new ServiceHost(typeof(AliPay));                            if (host.State != CommunicationState.Opening)                    host.Open();                    }    }}
View Code

三 、用戶端配置

<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <client>      <endpoint address="net.tcp://182.150.28.182:57000/sessionservice" binding="netTcpBinding"                bindingConfiguration="" contract="PayCommunicationWcf.Interface.IPay"                name="sessionservice" />    </client>    <bindings>      <netTcpBinding>        <binding>          <security mode="None">          </security>        </binding>      </netTcpBinding>    </bindings>  </system.serviceModel></configuration>
View Code

四、用戶端實現

1、回調契約實現

using System;using PayCommunicationWcf.Interface;using PayCommunicationWcf.Model;using PurClient.Logging;namespace PayCommunicationWcf.Server{    public class PayCallBack : IPayCallBack    {        public void SendResultOfPay(PayResultResponse result)        {                       Logger.GetInstance().Info(string.Format("已經接收到服務端發來的支付結果訊息,結果碼:{0},結果資訊:{1}。",result.Resultcode, result.Msg));                  }    }}
View Code

2、用戶端建立

private static IPay Channel        { get; set; }        private static IPayCallBack Callback        { get; set; } private void FormMain_Load(object sender, EventArgs e)        {            var  logger = Logger.GetInstance(); ;            logger.DelShowUiLogEvent += Loger_DelShowUiLogEvent;            Callback = new PayCallBack();            Channel = new DuplexChannelFactory<IPay>(Callback, "sessionservice").CreateChannel();            //  ServiceRegister.RegisterAllService();            logger.Info("初始化完成。");        }
View Code

 五、整解決方案源碼

點擊下載

WCF雙向通訊netTCP

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.