मैं अपने शोकहारा WCF सेवा के निर्माता के भीतर फेंक दिया प्राधिकरण अपवाद को संभालने के लिए IErrorHandler को लागू किया है है। जब एक सामान्य अपवाद पकड़ा है अपने कस्टम प्रकार की उम्मीद के रूप में लौटा है, लेकिन ContentType हेडर गलत है।IErrorHandler गलत संदेश के मुख्य भाग लौटने जब HTTP स्थिति कोड 401 अनधिकृत
HTTP/1.1 500 Internal Server Error
Content-Type: application/xml;
...
{"ErrorMessage":"Error!"}
लेकिन जब त्रुटि हैंडलर एक 401 अनधिकृत HTTP स्थिति कोड संदेश के मुख्य भाग डिफ़ॉल्ट प्रकार लेकिन ContentType शीर्षक के ओवरराइड की गई है है के रूप में यह होना चाहिए वापस जाने के लिए कोशिश करता है।
HTTP/1.1 401 Unauthorized
Content-Type: application/json;
...
{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
जाहिर है कुछ यहाँ गलत है, लेकिन मुझे यकीन है कि क्या नहीं कर रहा हूँ।
मैं आईररहैंडलर को कैसे कार्यान्वित कर सकता हूं जैसे कि यह सही हेडर के साथ जेसन में अपना कस्टम प्रकार देता है?
BaseDataResponseContract वस्तु:
[Serializable]
[DataContract(Name = "BaseDataResponseContract")]
public class BaseDataResponseContract
{
[DataMember]
public string ErrorMessage { get; set; }
} // end
इस वस्तु मैं वापस जाने के लिए चाहते हैं। मेरे आवेदन में अन्य सभी ऑब्जेक्ट्स इस ऑब्जेक्ट से प्राप्त होते हैं। जब कोई अपवाद फेंक दिया जाता है तो हम वास्तव में http स्थिति कोड और त्रुटि संदेश की परवाह करते हैं।
IErrorHandler कार्यान्वयन (प्रवेश संक्षिप्तता के लिए नहीं दिखाया गया है):
namespace WebServices.BehaviorsAndInspectors
{
public class ErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return true;
} // end
public void ProvideFault(Exception ex, MessageVersion version, ref Message fault)
{
// Create a new instance of the object I would like to return with a default message
var baseDataResponseContract = new BaseDataResponseContract { ErrorMessage = "Error!" };
// Get the outgoing response portion of the current context
var response = WebOperationContext.Current.OutgoingResponse;
// Set the http status code
response.StatusCode = HttpStatusCode.InternalServerError;
// If the exception is a specific type change the default settings
if (ex.GetType() == typeof(UserNotFoundException))
{
baseDataResponseContract.ErrorMessage = "Invalid Username!";
response.StatusCode = HttpStatusCode.Unauthorized;
}
// Create the fault message that is returned (note the ref parameter)
fault = Message.CreateMessage(version, "", baseDataResponseContract, new DataContractJsonSerializer(typeof(BaseDataResponseContract)));
// Tell WCF to use JSON encoding rather than default XML
var webBodyFormatMessageProperty = new WebBodyFormatMessageProperty(WebContentFormat.Json);
fault.Properties.Add(WebBodyFormatMessageProperty.Name, webBodyFormatMessageProperty);
// Add ContentType header that specifies we are using json
var httpResponseMessageProperty = new HttpResponseMessageProperty();
httpResponseMessageProperty.Headers[HttpResponseHeader.ContentType] = "application/json";
fault.Properties.Add(HttpResponseMessageProperty.Name, httpResponseMessageProperty);
} // end
} // end class
} // end namespace
IServiceBehavior कार्यान्वयन:
namespace WebServices.BehaviorsAndInspectors
{
public class ErrorHandlerExtensionBehavior : BehaviorExtensionElement, IServiceBehavior
{
public override Type BehaviorType
{
get { return GetType(); }
}
protected override object CreateBehavior()
{
return this;
}
private IErrorHandler GetInstance()
{
return new ErrorHandler();
}
void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { } // end
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
var errorHandlerInstance = GetInstance();
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(errorHandlerInstance);
}
}
void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { } // end
} // end class
} // end namespace
Web.Config:
<system.serviceModel>
<services>
<service name="WebServices.MyService">
<endpoint binding="webHttpBinding" contract="WebServices.IMyService" />
</service>
</services>
<extensions>
<behaviorExtensions>
<!-- This extension if for the WCF Error Handling-->
<add name="ErrorHandlerBehavior" type="WebServices.BehaviorsAndInspectors.ErrorHandlerExtensionBehavior, WebServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<ErrorHandlerBehavior />
</behavior>
</serviceBehaviors>
</behaviors>
....
</system.serviceModel>
अंत में, मैं समान व्यवहार देख रहा हूँ WebFaultException का उपयोग करते समय। मेरा विचार यह है कि यह कुछ गहराई से दफन किया गया है। नेट शेन्नीगन्स। मैं इतना है कि मैं किसी अन्य अपवाद नहीं संभाला जा सकता है पकड़ कर सकते हैं IErrorHandler लागू करने के लिए चुनने कर रहा हूँ।
संदर्भ:
https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler(v=vs.100).aspx
अन्य उदाहरण:
IErrorHandler doesn't seem to be handling my errors in WCF .. any ideas?
How to make custom WCF error handler return JSON response with non-OK http code?
How do you set the Content-Type header for an HttpClient request?
हाँ ... लेकिन कैसे? मुझे WebOperationContext.current.OutgoingResponse के साथ एक ही प्रोजेम मिला है लेकिन मुझे इसमें कोई लिखें विधि नहीं है, तो कस्टम प्रतिक्रिया कैसे बनाएं? – DestyNova