using System; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; namespace SiaSun.LMS.Common { public class WCFService { private string _Url; private string _Status; public ServiceHost ServiceHost; public object S_Service; public Type InterfaceType; public string Url { get { return _Url; } set { _Url = value; } } public string Status { get { return _Status; } set { _Status = value; } } public WCFService(string sURL, object service) { this.ServiceHost = null; this.S_Service = service; this.InterfaceType = service.GetType().GetInterfaces()[0]; this._Url = sURL; this._Status = "停止"; } public void StartService() { if (this.Status.Equals("停止")) { this.ServiceHost = new ServiceHost(this.S_Service, new Uri(this.Url)); this.ServiceHost.AddServiceEndpoint(this.InterfaceType, WCFService.GetBasicHttpBinding(), ""); this.ServiceHost.Description.Behaviors.Add(new ServiceThrottlingBehavior() { MaxConcurrentCalls = 3000, MaxConcurrentSessions = 3000 }); this.ServiceHost.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true }); this.ServiceHost.Open(); this.Status = "启动"; } } public void StopService() { if (this.Status.Equals("启动")) { if (this.ServiceHost != null) { this.ServiceHost.Close(); this.ServiceHost = null; } this.Status = "停止"; } } public static BasicHttpBinding GetBasicHttpBinding() { BasicHttpBinding bhb = new BasicHttpBinding { CloseTimeout = new TimeSpan(0, 5, 0), OpenTimeout = new TimeSpan(0, 5, 0), ReceiveTimeout = new TimeSpan(0, 5, 0), SendTimeout = new TimeSpan(0, 5, 0), MaxBufferSize = int.MaxValue, MaxBufferPoolSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue, TextEncoding = Encoding.UTF8, MessageEncoding = WSMessageEncoding.Text }; bhb.ReaderQuotas.MaxArrayLength = int.MaxValue; bhb.ReaderQuotas.MaxStringContentLength = int.MaxValue; bhb.ReaderQuotas.MaxBytesPerRead = int.MaxValue; return bhb; } public static T CreateChannel(string sURL) { return ChannelFactory.CreateChannel(WCFService.GetBasicHttpBinding(), new EndpointAddress(sURL)); } } }