供大家参考下子。不断完善中...
/**//* ///WCFHelper
*/ --------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace WCFHelper
{
public static class ServerHelper
{
#region
/**//* Different types of buindings
System.ServiceModel.BasicHttpBinding
System.ServiceModel.CustomBinding
System.ServiceModel.MsmqBindingBase
System.ServiceModel.MsmqIntegrationBinding
System.ServiceModel.NetMsmqBinding
System.ServiceModel.NetNamedPipeBinding
System.ServiceModel.NetPeerTcpBinding
System.ServiceModel.NetTcpBinding
System.ServiceModel.WSDualHttpBinding
System.ServiceModel.WSHttpBindingBase
System.ServiceModel.WSFederationBinding
System.ServiceModel.WSHttpBinding
*/
#endregion
/**//// <summary>
/// Deploys a BasicHttpBinding endpoint
/// </summary>
/// <param name="Uri">Uri of the endpiont such as "http://localhost:500/HelloService"</param>
/// <param name="ContractInterface">The contract interface type</param>
/// <param name="ContractClass">The contract class type</param>
/// <returns>The service host object</returns>
public static ServiceHost DeployHttpEndpoint(string Uri, Type ContractInterface, Type ContractClass)
{
System.ServiceModel.ServiceHost HService;
Uri baseURI = new Uri(Uri);
try
{
HService = new ServiceHost(ContractClass, baseURI);
HService.AddServiceEndpoint(ContractInterface,
new BasicHttpBinding(), baseURI);
HService.Open();
return HService;
}
catch
{
HService = null;
throw;
}
}
/**//// <summary>
/// Deploys a NetTcpBinding endpoint
/// </summary>
/// <param name="Uri">Uri of the endpiont such as "net.tcp://localhost:500/HelloService"</param>
/// <param name="ContractInterface">The contract interface type</param>
/// <param name="ContractClass">The contract class type</param>
/// <returns>The service host object</returns>
public static ServiceHost DeployTCPEndpoint(string Uri, Type ContractInterface, Type ContractClass)
{
System.ServiceModel.ServiceHost HService;
Uri baseURI = new Uri(Uri);
try
{
HService = new ServiceHost(ContractClass, baseURI);
HService.AddServiceEndpoint(ContractInterface,
new NetTcpBinding(), baseURI);
HService.Open();
return HService;
}
catch
{
HService = null;
throw;
}
}
/**//// <summary>
/// Creates a self-host service endpoint.
/// </summary>
/// <param name="service">
/// Full type name of service interface.
/// </param>
/// <param name="type">
/// Full class name of service implementation.
/// </param>
/// <param name="customBinding">
/// Full class name binding type.
/// </param>
/// <param name="uri">
/// Address of service.
/// </param>
public static void RegisterService(string service, string type, string customBinding, string uri)
{
ServiceHost serviceHost = new ServiceHost(Type.GetType(type));
Binding binding = (Binding) Activator.CreateInstance(Type.GetType(customBinding));
serviceHost.AddServiceEndpoint(Type.GetType(service), binding, new Uri(uri));
serviceHost.Open();
}
/**//// <summary>
/// Creates a self-host service for this type.
/// </summary>
/// <param name="type">
/// Full class name of service implementation.
/// </param>
public static void RegisterService(string type)
{
ServiceHost serviceHost = new ServiceHost(Type.GetType(type));
serviceHost.Open();
}
}
}