2011-03-30 38 views
4

我有一個自定義行爲,我在其中實現「IParameterInspector」以便能夠使用BeforeCall和AfterCall。我正在使用這些方法在我的WCF服務執行調用之前進行一些驗證。使用IParameterInspector.BeforeCall(字符串操作名稱,對象[]輸入)中止呼叫

這裏是我的屬性:

[AttributeUsage(AttributeTargets.Class)] 
public class SendReceiveBehaviorAttribute : Attribute, IServiceBehavior 
{ 
    public SendReceiveBehaviorAttribute() 
    { 
    } 

    public void ApplyDispatchBehavior(ServiceDescription desc, ServiceHostBase host) 
    { 
     foreach (ChannelDispatcher cDispatcher in host.ChannelDispatchers) 
     { 
      foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints) 
      { 
       foreach (DispatchOperation op in eDispatcher.DispatchRuntime.Operations) 
       { 
        op.ParameterInspectors.Add(MyInspector); 
       } 
      } 
     } 
    } 

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) 
    { 
    } 

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
    } 
} 

我的檢查:

internal class MyInspector: IParameterInspector 
{ 
    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) 
    { 
    } 

    public object BeforeCall(string operationName, object[] inputs) 
    { 
     // Make some verifications and cancel if it fails. 

     return null; 
    } 
} 

編輯 什麼是放棄我的電話,如果我的驗證失敗的最好方法?

+0

Hi @Jean。順便說一句,其他問題是否排序?我看到你把它標記爲答案,我認爲它是固定的。 – Aliostad 2011-03-30 12:58:21

+0

這裏還有什麼問題? – Aliostad 2011-03-30 12:58:55

+0

是的,謝謝它的工作。我正在使用IOperationInvoker,它不適用於您的解決方案。所以我試過了一個IParameterInspector(上面的代碼),它工作得很好。請看我編輯的問題。 – 2011-03-30 13:03:27

回答

2

拋出異常是唯一的方法 - 據我所知 - 放棄。

+0

這實際上是我正在做的,我會稍微等一下,看看有沒有人有更正式的方式來中止呼叫。 – 2011-03-30 13:10:29