我的WCF Helper

学WCF一段时间了,就把用到的这个Server端发布的封装Helper类放上来,
供大家参考下子。不断完善中...

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif/**//* ///WCFHelper
ExpandedBlockEnd.gif
*/
 --------------------------------------------------------
None.gif
using System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.ServiceModel;
None.gif
None.gif
namespace WCFHelper
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public static class ServerHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
dot.gif#region
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//* Different types of buindings
InBlock.gif
InBlock.gif            System.ServiceModel.BasicHttpBinding
InBlock.gif            System.ServiceModel.CustomBinding
InBlock.gif
InBlock.gif            System.ServiceModel.MsmqBindingBase
InBlock.gif            System.ServiceModel.MsmqIntegrationBinding
InBlock.gif            System.ServiceModel.NetMsmqBinding
InBlock.gif
InBlock.gif            System.ServiceModel.NetNamedPipeBinding
InBlock.gif            System.ServiceModel.NetPeerTcpBinding
InBlock.gif            System.ServiceModel.NetTcpBinding
InBlock.gif
InBlock.gif            System.ServiceModel.WSDualHttpBinding
InBlock.gif            System.ServiceModel.WSHttpBindingBase
InBlock.gif            System.ServiceModel.WSFederationBinding
InBlock.gif            System.ServiceModel.WSHttpBinding
ExpandedSubBlockEnd.gif         
*/

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Deploys a BasicHttpBinding endpoint
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Uri">Uri of the endpiont such as "http://localhost:500/HelloService"</param>
InBlock.gif        
/// <param name="ContractInterface">The contract interface type</param>
InBlock.gif        
/// <param name="ContractClass">The contract class type</param>
ExpandedSubBlockEnd.gif        
/// <returns>The service host object</returns>

InBlock.gif        public static ServiceHost DeployHttpEndpoint(string Uri, Type ContractInterface, Type ContractClass)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.ServiceModel.ServiceHost HService;
InBlock.gif            Uri baseURI 
= new Uri(Uri);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                HService 
= new ServiceHost(ContractClass, baseURI);
InBlock.gif                HService.AddServiceEndpoint(ContractInterface,
InBlock.gif                                       
new BasicHttpBinding(), baseURI);
InBlock.gif                HService.Open();
InBlock.gif                
return HService;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                HService 
= null;
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Deploys a NetTcpBinding endpoint
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Uri">Uri of the endpiont such as "net.tcp://localhost:500/HelloService"</param>
InBlock.gif        
/// <param name="ContractInterface">The contract interface type</param>
InBlock.gif        
/// <param name="ContractClass">The contract class type</param>
ExpandedSubBlockEnd.gif        
/// <returns>The service host object</returns>

InBlock.gif        public static ServiceHost DeployTCPEndpoint(string Uri, Type ContractInterface, Type ContractClass)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.ServiceModel.ServiceHost HService;
InBlock.gif            Uri baseURI 
= new Uri(Uri);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                HService 
= new ServiceHost(ContractClass, baseURI);
InBlock.gif                HService.AddServiceEndpoint(ContractInterface,
InBlock.gif                                       
new NetTcpBinding(), baseURI);
InBlock.gif                HService.Open();
InBlock.gif                
return HService;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                HService 
= null;
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Creates a self-host service endpoint.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="service">
InBlock.gif        
/// Full type name of service interface.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param name="type">
InBlock.gif        
/// Full class name of service implementation.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param name="customBinding">
InBlock.gif        
/// Full class name binding type.
InBlock.gif        
/// </param>
InBlock.gif        
/// <param name="uri">
InBlock.gif        
/// Address of service.
ExpandedSubBlockEnd.gif        
/// </param>

InBlock.gif        public static void RegisterService(string service, string type, string customBinding, string uri)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ServiceHost serviceHost 
= new ServiceHost(Type.GetType(type));
InBlock.gif
InBlock.gif            Binding binding 
= (Binding) Activator.CreateInstance(Type.GetType(customBinding));
InBlock.gif            serviceHost.AddServiceEndpoint(Type.GetType(service), binding, 
new Uri(uri));
InBlock.gif            serviceHost.Open();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Creates a self-host service for this type.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="type">
InBlock.gif        
/// Full class name of service implementation.
ExpandedSubBlockEnd.gif        
/// </param>

InBlock.gif        public static void RegisterService(string type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ServiceHost serviceHost 
= new ServiceHost(Type.GetType(type));
InBlock.gif            serviceHost.Open();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/njbaige/archive/2006/05/18/403842.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值