| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:OperationInvoker_T.cs
- * 2.功能描述:服务操作者
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈晓野 2014/08/13 1.00 新建
- *******************************************************************************/
- using System;
- using System.ServiceModel;
- using Dongke.IBOSS.PRD.Basics.Library;
- namespace Dongke.IBOSS.PRD.WCF.Proxys.ServiceProxy
- {
- /// <summary>
- /// 服务操作者
- /// </summary>
- /// <typeparam name="TChannel"></typeparam>
- public class OperationInvoker<TChannel> : OperationInvoker
- {
- /// <summary>
- /// 终点名
- /// </summary>
- public string EndpointName
- {
- get;
- private set;
- }
- /// <summary>
- /// 服务操作者
- /// </summary>
- /// <param name="endpointName"></param>
- public OperationInvoker(string endpointName)
- {
- this.EndpointName = endpointName;
- }
- /// <summary>
- /// 执行
- /// </summary>
- /// <param name="serviceInvocation"></param>
- /// <param name="timeout">超时(分钟)</param>
- public void Invoke(Action<TChannel> serviceInvocation, double timeout = 0)
- {
- ChannelFactory<TChannel> channelFactory = ChannelFactories.GetFactory<TChannel>(this.EndpointName);
- TChannel channel = channelFactory.CreateChannel();
- IContextChannel icc = channel as IContextChannel;
- if (icc != null)
- {
- if (timeout > 0)
- {
- icc.OperationTimeout = TimeSpan.FromMinutes(timeout);
- }
- else
- {
- // 不能大于24天,否则异常。
- icc.OperationTimeout = TimeSpan.FromDays(20);
- }
- }
- Invoke(serviceInvocation, channel);
- }
- /// <summary>
- /// 执行
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="serviceInvocation"></param>
- /// <param name="timeout">超时(分钟)</param>
- /// <returns></returns>
- public TResult Invoke<TResult>(Func<TChannel, TResult> serviceInvocation, double timeout = 0)
- {
- ChannelFactory<TChannel> channelFactory = ChannelFactories.GetFactory<TChannel>(this.EndpointName);
- TChannel channel = channelFactory.CreateChannel();
- IContextChannel icc = channel as IContextChannel;
- if (icc != null)
- {
- if (timeout > 0)
- {
- icc.OperationTimeout = TimeSpan.FromMinutes(timeout);
- }
- else
- {
- // 不能大于24天,否则异常。
- icc.OperationTimeout = TimeSpan.FromDays(20);
- }
- }
- return Invoke(serviceInvocation, channel);
- }
- }
- }
|