我们在之前的博客已经完成过实例,大家也看到了如何使用WCF服务:
添加服务引用——>输入服务地址——>实例化服务——>调用服务方法
?
那么今天为什么要再次说“程序中WCF服务整合”这个话题?
用过WebService的人都知道,随着服务的增多,管理WebService是一个非常繁重的工作。好了,今天我们的目标来了——让WCF服务变得整齐、有序、易管理!
?
首先,我们建立一个工厂类,这个工厂用来实例化我们的服务。这样做的好处是,所有的服务都是由工厂实例化的。
然后我们要建立一个公共的接口,这个接口继承所有的服务接口。这样做的好处是,所有的服务接口都可以用这个接口来代替。
?
好了,有了这两点,我们就可以利用多态+工厂来统一管理我们的服务了。
?
说了这么多理论,还是以我们程序猿的语言说更明了一些!
?
1、建立接口类IServices
?
namespace Modules.Interface
{
[ServiceContract]
public interface IServices :
IUserSevice
{
}
}
?
2、建立服务工厂ServiceFactory
namespace Modules.Factory
{
public class ServiceFactory
{
private static readonly SortedList
_serviceBusiness = new SortedList
(); //获取接口 public static IServices GetServiceBusiness(string endpointName) { IServices iServices; iServices = CreateWCFInterface(endpointName); if (_serviceBusiness.ContainsKey(endpointName)) { iServices = _serviceBusiness[endpointName]; } else { if (true) { iServices = CreateWCFInterface(endpointName); } _serviceBusiness.Add(endpointName, iServices); } return iServices; } //获取WCF服务方法,使用代理工厂 private static IServices CreateWCFInterface(string endpointName) { return ServiceProxyFactory.Create
(endpointName); } //获取用户服务 public static IUserSevice GetUserService(string endpointName) { return GetServiceBusiness(endpointName); } } }
3、建立代理工厂ServiceProxyFactory
namespace Modules.Factory.ServiceProxy
{
public static class ServiceProxyFactory {
public static T Create
(string endpointName) {
if (string.IsNullOrEmpty(endpointName)) {
throw new ArgumentNullException(endpointName);
}
return (T)(new ServiceRealProxy
(endpointName).GetTransparentProxy()); } } }
4、建立代理类RealProxy
namespace Modules.Factory.ServiceProxy
{
public class ServiceRealProxy
: RealProxy
{
private readonly string _endpointName;
public ServiceRealProxy(string endpointName): base(typeof(T))
{
if (string.IsNullOrEmpty(endpointName))
{
throw new ArgumentNullException(endpointName);
}
this._endpointName = endpointName;
}
//重写Invoke
public override IMessage Invoke(IMessage msg)
{
T channel = ChannelFactoryCreator.Create
(this._endpointName).CreateChannel(); IMethodCallMessage methodCall = (IMethodCallMessage)msg; IMethodReturnMessage methodReturn = null; object[] copiedArgs = Array.CreateInstance(typeof(object), methodCall.Args.Length) as object[]; methodCall.Args.CopyTo(copiedArgs, 0); try { object returnValue = methodCall.MethodBase.Invoke(channel, copiedArgs); methodReturn = new ReturnMessage(returnValue, copiedArgs, copiedArgs.Length, methodCall.LogicalCallContext, methodCall); ((ICommunicationObject)channel).Close(); } catch (TargetInvocationException tiEx) { string title; if (tiEx.InnerException is NotSupportedException) { title =string.Concat( WCFExpetion, NotSupportedException, WCF服务契约异常); throw new Exception( string.Format({0}: {1}, title, tiEx.InnerException.Message), tiEx.InnerException); } else if (tiEx.InnerException is FaultException) { title = string.Concat(WCFExpetion, FaultException, 方法内部出现没有处理的异常); throw new Exception(string.Format({0}: {1}, title, tiEx.InnerException.Message), tiEx.InnerException); } else if (tiEx.InnerException is CommunicationException) { title = s