扩展WCF操作行为

使用IParameterInspector, IOperationBehavior,Attribute(参数检查器、操作行为接口和标签)扩展WCF操作行为

开发环境: VS2008 SP1 WIN2008 SP2
WCF允许我们在端点Endpoint、消息message、操作Operation、参数Parameter中扩展WCF。本文简单介绍如何截获操作的参数来扩展WCF的操作。这种扩展需要在WCF的Contract契约上加标签来实现,不能算是真正的AOP,但是有些AOP的意思。
实现步骤:1、实现自己的参数检查器;2、将参数检查器放入要实现的操作行为;3、在操作Operation上加入标签attribute

1、实现自己的参数检查器,实现参数检查器,在调用操作前检查参数是否为17位数字,在调用操作后发送执行结果

ExpandedBlockStart.gif代码
    public class  EntryIdInspector : IParameterInspector
    {
        
public int
 intParamIndex
        {
            
get
;
            
set
;
        }
        
string EntryIdFormat = @"\d{17}"
;
        
        
public EntryIdInspector(): this(0
){ }
        
        
public EntryIdInspector(int
 intParamIndex)
        {
            
this.intParamIndex =
 intParamIndex;
        }

        
public object BeforeCall(string operationName, object
[] inputs)
        {
            
string strEntryId = inputs[this.intParamIndex] as string
;
            
if (!Regex.IsMatch(strEntryId, this
.EntryIdFormat, RegexOptions.None))
            {
                
                MessageQueue mq 
= new MessageQueue(@".\private$\msgqueue"
);
                mq.Send( 
"Parameter is not valid when call " + operationName + " at " +
 DateTime.Now.ToLongDateString());
                
throw new
 FaultException(
                    
"Invalid Entry ID format. Required format: ##################"
);
            }
            
return null
;

        }
        
public void AfterCall(string operationName, object[] outputs, object returnValue, object
 correlationState)
        {
            MessageQueue mq 
= new MessageQueue(@".\private$\msgqueue"
);
            
string strResult =
 returnValue.ToString();
            mq.Send(
"Client call " + operationName + ":" + strResult + " at " +
 DateTime.Now.ToLongDateString());
                
        }
    }

 

2、将参数检查器放入要实现的操作行为

ExpandedBlockStart.gif代码
    public class  EntryIdValidation : Attribute, IOperationBehavior
    {
        
#region IOperationBehavior Members


        
public void  AddBindingParameters(OperationDescription operationDescription,
            BindingParameterCollection bindingParameters)
        {
        }

        
public void
 ApplyClientBehavior(OperationDescription operationDescription,
            ClientOperation clientOperation)
        {
            EntryIdInspector EntryIdInspector 
= new
 EntryIdInspector();
            clientOperation.ParameterInspectors.Add(EntryIdInspector);
        }

        
public void
 ApplyDispatchBehavior(OperationDescription operationDescription,
            DispatchOperation dispatchOperation)
        {
            EntryIdInspector EntryIdInspector 
= new
 EntryIdInspector();
            dispatchOperation.ParameterInspectors.Add(EntryIdInspector);
        }

        
public void
 Validate(OperationDescription operationDescription)
        {
        }

        
#endregion

    }

 

3、在操作Operation上加入标签attribute,在操作契约中加上标签[EntryIdValidation]

    [ServiceContract]
    
public interface
 IRelSrvContract
    {
        [EntryIdValidation]
        [OperationContract]
        bool
 Rel( string strEntryID);
    }

转载于:https://www.cnblogs.com/utopia/archive/2010/01/12/1644787.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值