2012-04-23 11 views
6
try 
{ 
    ///// here is the code that might throw erros. If I discover the user is unauthorized I throw a WebFaultException myself to alert the client 
}  
catch (WebFaultException ex) 
{ 
    throw ex; //but if I wrap all code in try-catch, I have to rethrow the exception o the status code reaches the client 
} 
catch (Exception ex) 
{ 
    throw new WebFaultException(ex.Message, HttpStatusCode.InternalServerError); 
} 

क्या मुझे सभी को कोशिश करने में लपेटना चाहिए, या आप क्या सलाह देते हैं? मैं बाकी-ful JSON सेवाओंक्या मुझे अपने सभी डब्ल्यूसीएफ सेवा कोड को कैच ब्लॉक में लपेटना चाहिए?

उत्तर

15

तुम कर सकते हो के साथ WCF का उपयोग करें, लेकिन यह शायद बेहतर हो IErrorHandler और add it as a behaviour अपने सेवा है, जो आपके बिना क्रिया अपवाद एक ही स्थान पर नियंत्रित किया जा करने की अनुमति देगा करने के लिए लागू करने के लिए हैं, तो आप करेंगे तो उपयोगकर्ताओं को ब्योरा वापस करने के लिए वहां एक गलती अपवाद बनाने में सक्षम हो।

/// <summary> 
/// Custom WCF Behaviour for Service Level Exception handling. 
/// </summary> 
public class ErrorHandlerBehavior : IServiceBehavior 
    { 
    #region Implementation of IServiceBehavior 


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


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


    public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
     { 
     IErrorHandler errorHandler = new ErrorHandler(); 

     foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers) 
      { 
      var channelDispatcher = channelDispatcherBase as ChannelDispatcher; 

      if (channelDispatcher != null) 
       { 
       channelDispatcher.ErrorHandlers.Add (errorHandler); 
       } 
      } 
     } 

    #endregion 
    } 

तब यदि आप स्वयं तुम सिर्फ व्यवहार प्रोग्राम के रूप में जोड़ सकते हैं होस्ट कर रहे हैं:

myServiceHost.Description.Behaviors.Add (new ErrorHandlerBehavior()); 

आप इसे जोड़ना चाहते हैं

ErrorHandler : IErrorHandler 
{ 
... just implement the handling of errors here, however you want to handle them 
} 

तो एक व्यवहार है जो इस का उपयोग करता है बनाने के लिए कॉन्फ़िगरेशन के माध्यम से आपको इनमें से एक की आवश्यकता है:

public class ErrorHandlerElement : BehaviorExtensionElement 
    { 
    public override Type BehaviorType 
     { 
     get { return typeof (ErrorHandlerBehavior); } 
     } 

    protected override object CreateBehavior() 
     { 
     return new ErrorHandlerBehavior(); 
     } 
    } 
} 

और फिर विन्यास:

<system.serviceModel> 
<extensions> 
    <behaviorExtensions> 
    <add name="ErrorLogging" type="ErrorHandlerBehavior, ErrorHandling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<whatever>" /> 
    </behaviorExtensions> 
</extensions> 
<bindings> 
    <basicHttpBinding> 
    <binding name="basicBinding"> 
    </binding> 
    </basicHttpBinding> 
</bindings> 
<services> 
    <service behaviorConfiguration="Service1Behavior" name="Service"> 
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="Service" /> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="Service1Behavior"> 
     <serviceMetadata httpGetUrl="" httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="false" /> 
     <ErrorLogging /> <--this adds the behaviour to the service behaviours --> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

+0

अरे सैम, आपका जवाब में एक कोड उदाहरण का कोई मौका? – EtherDragon

+0

@EtherDragon लिंक किए गए उदाहरण से पता चलता है कि व्यवहार को प्रोग्रामेटिक रूप से कैसे जोड़ा जाए। –

+0

@AbuHamzah, अद्यतन और जोड़े गए उदाहरण –