| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:OperationInvoker.cs
- * 2.功能描述:服务操作者
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈晓野 2014/08/13 1.00 新建
- *******************************************************************************/
- using System;
- using System.ServiceModel;
- using Dongke.IBOSS.PRD.Basics.BaseResources;
- using Dongke.IBOSS.PRD.Basics.Library;
- namespace Dongke.IBOSS.PRD.WCF.Proxys.ServiceProxy
- {
- /// <summary>
- /// 服务操作者
- /// </summary>
- public class OperationInvoker
- {
- /// <summary>
- /// 执行
- /// </summary>
- /// <typeparam name="TChannel"></typeparam>
- /// <param name="serviceInvocation"></param>
- /// <param name="channel"></param>
- public static void Invoke<TChannel>(Action<TChannel> serviceInvocation, TChannel channel)
- {
- ICommunicationObject communicationObject = (ICommunicationObject)channel;
- try
- {
- serviceInvocation(channel);
- communicationObject.Close();
- }
- catch (Exception ex)
- {
- HandleException(ex, communicationObject);
- }
- }
- /// <summary>
- /// 执行
- /// </summary>
- /// <typeparam name="TChannel"></typeparam>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="serviceInvocation"></param>
- /// <param name="channel"></param>
- /// <returns></returns>
- public static TResult Invoke<TChannel, TResult>(Func<TChannel, TResult> serviceInvocation, TChannel channel)
- {
- ICommunicationObject communicationObject = (ICommunicationObject)channel;
- TResult result = default(TResult);
- try
- {
- result = serviceInvocation(channel);
- communicationObject.Close();
- }
- catch (Exception ex)
- {
- HandleException(ex, communicationObject);
- }
- return result;
- }
- /// <summary>
- /// 异常处理
- /// </summary>
- /// <param name="ex"></param>
- /// <param name="channel"></param>
- public static void HandleException(Exception ex, ICommunicationObject channel)
- {
- channel.Abort();
- /*
- // 通信错误 诸如网络错误,地址错误,服务器没有启动等等
- // 状态异常 代理已经关闭,或者通道Fault,等问题
- // 服务异常 服务调用时抛出的异常,这个服务内部异常会序列化传递给客户端,被客户端捕获
- if (ex is CommunicationException)
- {
- // 通信错误,诸如网络错误,地址错误,服务器没有启动,验证没有通过等等
- }
- else if (ex is TimeoutException)
- {
- // 通信错误,超时
- }
- //else if (ex is FaultException<ExceptionDetail>)
- //{
- // // includeExceptionDetailInFaults="true"
- // //FaultException<ExceptionDetail> ded = (ex as FaultException<ExceptionDetail>);
- // // 服务端 应用异常
- //}
- else if (ex is FaultException)
- {
- // 服务端 应用异常
- }
- else if (ex is Exception)
- {
-
- }
- */
- throw ex;
- }
- }
- }
|