WCF双向通讯netTCP

 

一、服务端配置

<?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

 五、整解决方案源码

点击下载

posted on 2017-07-25 23:05  木色小罗  阅读(340)  评论(1编辑  收藏  举报

导航

诗与远方