You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
5.5 KiB
155 lines
5.5 KiB
using System;
|
|
using System.ServiceModel;
|
|
using System.ServiceModel.Description;
|
|
using System.Text;
|
|
#if Credentials
|
|
using System.IdentityModel.Selectors;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.ServiceModel.Security;
|
|
#endif
|
|
|
|
namespace SSWMS.Common
|
|
{
|
|
public class WCFService
|
|
{
|
|
private string _Url;
|
|
private string _Status;
|
|
private ServiceHost ServiceHost;
|
|
private object S_Service;
|
|
private Type InterfaceType;
|
|
|
|
public string Url
|
|
{
|
|
get { return _Url; }
|
|
set { _Url = value; }
|
|
}
|
|
public string Status
|
|
{
|
|
get { return _Status; }
|
|
set { _Status = value; }
|
|
}
|
|
|
|
public WCFService(object service)
|
|
{
|
|
this.ServiceHost = null;
|
|
this.S_Service = service;
|
|
this.InterfaceType = service.GetType().GetInterfaces()[0];
|
|
string sServiceName = service.GetType().Name;
|
|
if (sServiceName == "S_ERPService")
|
|
{
|
|
this.Url = string.Format("{0}{1}", AppSettings.ERPServiceUrl, sServiceName);
|
|
}
|
|
else
|
|
{
|
|
this.Url = string.Format("{0}{1}", AppSettings.ServiceUrl, sServiceName);
|
|
}
|
|
this.Status = "停止";
|
|
}
|
|
|
|
public void StartService()
|
|
{
|
|
if (this.Status == "停止")
|
|
{
|
|
this.ServiceHost = new ServiceHost(this.S_Service, new Uri(this.Url));
|
|
#if Credentials
|
|
this.ServiceHost.AddServiceEndpoint(this.InterfaceType, WCFService.GetWSHttpBinding(), "");
|
|
this.ServiceHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
|
|
this.ServiceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomUsernamePasswordValidator();
|
|
this.ServiceHost.Credentials.ServiceCertificate.SetCertificate("CN=localhost", StoreLocation.LocalMachine, StoreName.My);
|
|
#else
|
|
this.ServiceHost.AddServiceEndpoint(this.InterfaceType, WCFService.GetBasicHttpBinding(), "");
|
|
#endif
|
|
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 == "启动")
|
|
{
|
|
if (this.ServiceHost != null)
|
|
{
|
|
this.ServiceHost.Close();
|
|
this.ServiceHost = null;
|
|
}
|
|
this.Status = "停止";
|
|
}
|
|
}
|
|
|
|
public static WSHttpBinding GetWSHttpBinding()
|
|
{
|
|
WSHttpBinding wshb = new WSHttpBinding
|
|
{
|
|
CloseTimeout = new TimeSpan(0, 0, 30),
|
|
OpenTimeout = new TimeSpan(0, 0, 30),
|
|
ReceiveTimeout = new TimeSpan(0, 0, 30),
|
|
SendTimeout = new TimeSpan(0, 0, 30),
|
|
MaxBufferPoolSize = int.MaxValue,
|
|
MaxReceivedMessageSize = int.MaxValue,
|
|
TextEncoding = Encoding.UTF8,
|
|
MessageEncoding = WSMessageEncoding.Text
|
|
};
|
|
wshb.ReaderQuotas.MaxArrayLength = int.MaxValue;
|
|
wshb.ReaderQuotas.MaxStringContentLength = int.MaxValue;
|
|
wshb.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
|
|
wshb.Security.Mode = SecurityMode.Message;
|
|
wshb.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
|
|
return wshb;
|
|
}
|
|
|
|
public static BasicHttpBinding GetBasicHttpBinding()
|
|
{
|
|
BasicHttpBinding bhb = new BasicHttpBinding
|
|
{
|
|
CloseTimeout = new TimeSpan(0, 0, 30),
|
|
OpenTimeout = new TimeSpan(0, 0, 30),
|
|
ReceiveTimeout = new TimeSpan(0, 0, 30),
|
|
SendTimeout = new TimeSpan(0, 0, 30),
|
|
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<T>(string sURL)
|
|
{
|
|
#if Credentials
|
|
ChannelFactory<T> cf = new ChannelFactory<T>(WCFService.GetWSHttpBinding(), new EndpointAddress(sURL));
|
|
cf.Credentials.UserName.UserName = "super";
|
|
cf.Credentials.UserName.Password = "siasun";
|
|
return cf.CreateChannel();
|
|
#else
|
|
return ChannelFactory<T>.CreateChannel(WCFService.GetBasicHttpBinding(), new EndpointAddress(sURL));
|
|
#endif
|
|
}
|
|
}
|
|
|
|
#if Credentials
|
|
public class CustomUsernamePasswordValidator : UserNamePasswordValidator
|
|
{
|
|
public override void Validate(string userName, string password)
|
|
{
|
|
if (userName != "super" || password != "siasun")
|
|
{
|
|
throw new Exception("用户名或密码错误。");
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
}
|