WCF TCP协议传输

1 篇文章 0 订阅

1、服务端:

using com.jbossjf.common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;

//作者微:jbossjf
namespace com.jbossjf.core
{
    public class WcfService
    {
        private static ServiceHost basefileHost = null;
        public int StartWcfService()
        {
            try
            {
                if (basefileHost == null)
                {

                    //使用代码绑定 
                    Uri tcpa = new Uri("net.tcp://" + CommonHelp.ip + ":8866/BasefileService");
                    basefileHost = new ServiceHost(typeof(com.jbossjf.core.BasefileService), tcpa);
                    ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
                    NetTcpBinding tcpb = new NetTcpBinding()
                         {
                             ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
                             {
                                 MaxStringContentLength = 2147483647
                             },
                             Security = new NetTcpSecurity()
                             {
                                 Mode = SecurityMode.None
                             },
                             ReceiveTimeout = new TimeSpan(0,1,30)
                             ,
                             SendTimeout = new TimeSpan(0,1,30)
                             ,
                             MaxBufferSize = 65536
                             ,
                             MaxBufferPoolSize = 524288
                             ,
                             MaxReceivedMessageSize = 65536
                             
                             //OpenTimeout = new TimeSpan(0,0,5)
                             //,
                             //CloseTimeout = new TimeSpan(0,1,0)
                         };
                    basefileHost.Description.Behaviors.Add(mBehave);
                 
                    basefileHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
                    basefileHost.AddServiceEndpoint(typeof(com.jbossjf.core.IBasefileService), tcpb, tcpa);

                   // NetTcpBinding binding = new NetTcpBinding()
                
                    basefileHost.Open();

                    return 1;
                }
            }
            catch(Exception ex){
                CommonHelp.WriteLog(ex.Message);
            }

            return 0;

        }

        public int CloseWcfService()
        {
            try
            {
                if (basefileHost != null)
                {
                    basefileHost.Close();
                    return 1;
                }
            }
            catch { }
            return 0;
        }

    //     using (ServiceReferenceDemo.Service1Client sc = new ServiceReferenceDemo.Service1Client())
    //{
    //    sc.Open();
    //    MessageBox.Show(sc.GetData(10));
    //    sc.Close();
    //}
    }
}

2、客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.jbossjf.core.WCF
{
   public class WcfHelp
    {
       public static   com.jbossjf.core.BasefileServiceReference.BasefileServiceClient _BasefileServiceReference = null;
       public com.jbossjf.core.BasefileServiceReference.BasefileServiceClient GetBasefileServiceClient()
       {
           try
           {

               if (_BasefileServiceReference == null)
               {
                   _BasefileServiceReference = new core.BasefileServiceReference.BasefileServiceClient();
                  // if (!_BasefileServiceReference.State.ToString().Equals("Created"))
                   _BasefileServiceReference.Open();
               }
               if (_BasefileServiceReference.State.ToString().Equals("Faulted"))
               {

                 
                   try
                   {
                       _BasefileServiceReference = null;
                       _BasefileServiceReference = new core.BasefileServiceReference.BasefileServiceClient();
                       _BasefileServiceReference.Open();
                   }
                   catch { }
                   //_BasefileServiceReference = new core.BasefileServiceReference.BasefileServiceClient();
               }
           }
           catch { }

           return _BasefileServiceReference;
       }


    }
}

DataTable dt = _WcfHelp.GetBasefileServiceClient().FindFermentationALL("jbossjf");

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值