扩展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位数字,在调用操作后发送执行结果
代码
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());
}
}
{
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、将参数检查器放入要实现的操作行为
代码
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
}
{
#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);
}
public interface IRelSrvContract
{
[EntryIdValidation]
[OperationContract]
bool Rel(string strEntryID);
}
posted on 2010-01-12 12:32 Utopia Coming 阅读(1175) 评论(0) 编辑 收藏 举报