AOP (アスペクト指向プログラミング) について聞いたことがありますか? これは、対象の型をラップし、ラップするメソッドの前後に追加の処理を実行する再利用可能なアスペクトとしてクロス カットの懸念を実装する方法です。
http://en.wikipedia.org/wiki/Decorator_pattern
WCF 環境内では、これは通常、サービス クラスに「ビヘイビア」を適用することによって行われます。この場合、IParameterInspector を実装する属性を使用して IOperationBehavior インターフェイスを提案し、サービス インスタンスが作成されて呼び出される前にパラメーターを確認します。wcf メッセージ パイプラインを拡張するためのオプションについて詳しく説明している便利な記事へのリンクを次に示します。
http://msdn.microsoft.com/en-us/magazine/cc163302.aspx
//Attribute class
public class LogOperationBehavior : Attribute, IOperationBehavior, IParameterInspector {
public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) {
return;
}
public void ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation) {
//clientOperation.ParameterInspectors.Add(new ClientParameterInspector());
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation) {
dispatchOperation.ParameterInspectors.Add(this);
}
public void Validate(OperationDescription operationDescription) {
return;
}
#region IParameterInspector Members
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) {
//perform logging after
}
public object BeforeCall(string operationName, object[] inputs) {
//perform logging before
return null;
}
#endregion
}
public class BusinessOperation : IBusinessOperation {
//Apply to your service via an attribute
[LogOperationBehavior]
public DivideResponse DivideTwoNumbers(DivideRequest dr) {
return new DivideResponse() {
Answer = dr.Numerator/ dr.Demoninator2,
};
}