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");