2012-05-29 5 views
7

मुझे लगता है कि कोशिश पकड़ के अंदर हर बार कोई त्रुटि हुई है फेंक दिया जाएगा अपने कस्टम अपवाद बनाया:कस्टम अपवाद को वापस करने के लिए WebFaultException को कैसे संभालें?

[Serializable] 
public class CustomException : Exception 
{ 
    public CustomException() { } 

    public CustomException(string message) 
     : base(message) { } 

    public CustomException(string message, Exception innerException) 
     : base(message, innerException) { } 
} 

मैं दो सेवाओं, आराम और साबुन की है। एसओएपी सेवाओं के लिए, मुझे अपना कस्टम अपवाद फेंकने में कोई समस्या नहीं है। लेकिन आरईएसटी में, मुझे कई कठिनाइयों का सामना करना पड़ा।

public static WebFaultException RestGetFault(ServiceFaultTypes fault) 
    { 
     ServiceFault serviceFault = new ServiceFault(); 
     serviceFault.Code = (int)fault; 
     serviceFault.Description = ConfigAndResourceComponent.GetResourceString(fault.ToString()); 
     FaultCode faultCode = new FaultCode(fault.ToString()); 
     FaultReasonText faultReasonText = new FaultReasonText(serviceFault.Description); 
     FaultReason faultReason = new FaultReason(faultReasonText); 
     WebFaultException<ServiceFault> webfaultException = new WebFaultException<ServiceFault>(serviceFault, HttpStatusCode.InternalServerError); 

     throw webfaultException; 
    } 

ServiceFault एक वर्ग जहां यह जो मैं सभी जानकारी की आवश्यकता डाल करने के लिए उपयोग करने के कुछ गुण है:

यहाँ एक WebFaultException फेंकने के लिए तरीका है।

मैं इस विधि का उपयोग बाकी सेवा के अंदर एक अपवाद फेंकने के लिए:

public static CustomException GetFault(ServiceFaultTypes fault) 
    { 
     string message = fault.ToString(); 
     CustomException cusExcp = new CustomException(message, new Exception(message)); 
     throw cusExcp; 
    } 

एक नमूना बाकी सेवा (विधि में प्रवेश करें):

[WebInvoke(UriTemplate = "Login", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
    public Session Login(ClientCredentials client, LogCredentials loginfo) 
    { 
     try 
     { 
      // Login process 
      return copied; 
     } 
     catch (LogicClass.CustomException ex) 
     { 
      LogicClass.RestGetFault(LogicClass.EnumComponent.GetServiceFaultTypes(ex.Message)); 
      throw ex; 
     } 
    } 

MVC हिस्सा:

नियंत्रक:

[HttpPost] 
    public ActionResult Login(LoginCredentials loginfo) 
    { 
     try 
     { 
      string param = "{\"client\":" + JSonHelper.Serialize<ClientAuthentication>(new ClientAuthentication() { SessionID = Singleton.ClientSessionID }) 
          + ", \"loginfo\":" + JSonHelper.Serialize<LoginCredentials>(loginfo) + "}"; 

      string jsonresult = ServiceCaller.Invoke(Utility.ConstructRestURL("Authenticate/Login"), param, "POST", "application/json"); 
      UserSessionDTO response = JSonHelper.Deserialize<UserSessionDTO>(jsonresult); 

     } 
     catch (Exception ex) 
     { 
      return Json(new 
      { 
       status = ex.Message, 
       url = string.Empty 
      }); 
     } 

     return Json(new 
     { 
      status = "AUTHENTICATED", 
      url = string.IsNullOrWhiteSpace(loginfo.r) ? Url.Action("Index", "Home") : loginfo.r 
     }); 
    } 

मैं ServiceCaller.Invoke का उपयोग REST API का कॉल और प्रतिक्रिया प्राप्त करने हेतु: ServiceCaller.cs

public class ServiceCaller 
{ 
    public static string Invoke(string url, string parameters, string method, string contentType) 
    { 
     string results = string.Empty; 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url)); 
     request.Method = method; 
     request.ContentType = contentType; 

     if (!string.IsNullOrEmpty(parameters)) 
     { 
      byte[] byteArray = Encoding.UTF8.GetBytes(parameters); 
      request.ContentLength = byteArray.Length; 
      Stream dataStream = request.GetRequestStream(); 
      dataStream.Write(byteArray, 0, byteArray.Length); 
      dataStream.Close(); 
     } 

     try 
     { 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      if (HttpStatusCode.OK == response.StatusCode) 
      { 
       Stream responseStream = response.GetResponseStream(); 
       int length = (int)response.ContentLength; 

       const int bufSizeMax = 65536; 
       const int bufSizeMin = 8192; 
       int bufSize = bufSizeMin; 

       if (length > bufSize) bufSize = length > bufSizeMax ? bufSizeMax : length; 

       byte[] buf = new byte[bufSize]; 
       StringBuilder sb = new StringBuilder(bufSize); 

       while ((length = responseStream.Read(buf, 0, buf.Length)) != 0) 
        sb.Append(Encoding.UTF8.GetString(buf, 0, length)); 

       results = sb.ToString(); 
      } 
      else 
      { 
       results = "Failed Response : " + response.StatusCode; 
      } 
     } 
     catch (Exception exception) 
     { 
      throw exception; 
     } 

     return results; 
    } 
} 

मैं ग्राहक पक्ष पर इस वापस जाने के लिए बाकी सेवा की उम्मीद कर रहा हूँ:

enter image description here

लेकिन में अंत में, यह हमेशा इस वापसी:

enter image description here

मुझे क्या करना चाहिए? कृपया मदद करे।

[FaultException: InvalidLogin] 
    System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +9441823 

आप "InvalidLogin" देखा:

संपादित

यहाँ नमूना प्रतिक्रिया जब साबुन सेवा बुला रहा है? यही वह है जो मैं आरईएसटी सर्ववसे से प्रतिक्रिया पर देखना चाहता हूं।
बाकी हिस्सों से नमूना प्रतिक्रिया:

[WebException: The remote server returned an error: (500) Internal Server Error.] 
    System.Net.HttpWebRequest.GetResponse() +6115971 

मैं एक WebFaultException फेंक लेकिन मैं एक WebException प्राप्त करते हैं।
यदि मैं आरईएसटी पर सटीक त्रुटि संदेश प्राप्त नहीं कर पाऊंगा, तो मैं एसओएपी के लिए जाऊंगा।
उत्तर के लिए धन्यवाद।

+0

कोई जवाब नहीं दिए गए चरणों का संक्षेप में? हम्म ... :( – fiberOptics

+0

क्या यह एक डब्ल्यूसीएफ सेवा है और यदि ऐसा है, तो क्या आपकी कॉन्फ़िगरेशन क्लाइंट में त्रुटियों को वापस करने के लिए सेट है? – Slick86

+0

मैं उसी प्रकार के उत्तर की तलाश में हूं ... मुझे लगता है कि मुझे पता लगाना नहीं है हैंडलर की तरह एक आरईएसटी कैसे बनाना है जो एक सार्थक अपवाद फेंकता है। मुझे मूल रूप से टेक्स्टस्टैटस का उपयोग करके पास या विफल होने के कारण मेरी जावास्क्रिप्ट से निपटना होगा। – farina

उत्तर

4

HttpWebRequest (या जावास्क्रिप्ट क्लाइंट) का उपयोग करते समय, आपके कस्टम अपवाद का उनके लिए कोई अर्थ नहीं है। बस एचटीपी त्रुटि कोड (जैसे 500 आंतरिक सर्वर त्रुटि) और प्रतिक्रिया की सामग्री में डेटा।

तो आपको अपने आप को अपवाद संभालना होगा। उदाहरण के लिए, यदि आप WebException पकड़ते हैं तो आप अपने सर्वर कॉन्फ़िगरेशन के आधार पर एक्सएमएल या जेसन प्रारूप में सामग्री (त्रुटि संदेश) पढ़ सकते हैं।

catch (WebException ex) 
{ 
    var error = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd(); 
    //Parse your error string & do something 
} 
+0

मैंने यह किया है, लेकिन जो मैं देखना चाहता हूं वह मेरा स्वयं का त्रुटि संदेश है। क्या http त्रुटि कोड या अपवाद संदेश को ओवरराइड करने के लिए वैसे भी है? साबुन सेवाओं में यह ठीक काम करता है, लेकिन आरईएसटी में नहीं। धन्यवाद। – fiberOptics

+0

@ user72213 अगर आप प्रतिक्रिया पढ़ते हैं तो आपको अपना कस्टम संदेश देखना चाहिए। (संभवतः, कुछ एक्सएमएल में फॉर्म) इस संदेश के साथ एक कस्टम अपवाद फेंकना कुछ ऐसा है जो आपको क्लाइंट साइड पर संभालना चाहिए। अगर आपने अपने साबुन-आधारित क्लाइंट को शुद्ध HttpWebClient के साथ कार्यान्वित किया है तो आपको भी वही समस्या होगी। –

0

मुझे कुछ मिनट पहले इसी तरह की समस्या थी। शायद यह मदद करता है।

public static void ExecuteServiceMethod(this IMyRESTService svc, Action svcMethod) 
{ 
    // try to get first last error here 
    string lastError = svc.CommHandler.CH_TryGetLastError(); 
    if (!String.IsNullOrEmpty(lastError)) 
     throw new WebFaultException<string>(lastError, System.Net.HttpStatusCode.InternalServerError); 

    try 
    { 
     // execute service method 
     svcMethod(); 
    } 
    catch (CommHandlerException ex) 
    { 
     // we use for now only 'InternalServerError' 
     if (ex.InnerException != null) 
      throw new WebFaultException<string>(ex.InnerException.Message, System.Net.HttpStatusCode.InternalServerError); 
     else 
      throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError); 
    } 
    catch (Exception ex) 
    { 
     throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError); 
    } 
} 
:

public static void ExecuteServiceMethod(this IMyRESTService svc, Action svcMethod) 
{ 
    try 
    { 
     // try to get first last error here 
     string lastError = svc.CommHandler.CH_TryGetLastError(); 
     if (!String.IsNullOrEmpty(lastError)) 
      throw new WebFaultException<string>(lastError, System.Net.HttpStatusCode.InternalServerError); 

     // execute service method 
     svcMethod(); 
    } 
    catch (CommHandlerException ex) 
    { 
     // we use for now only 'InternalServerError' 
     if (ex.InnerException != null) 
      throw new WebFaultException<string>(ex.InnerException.Message, System.Net.HttpStatusCode.InternalServerError); 
     else 
      throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError); 
    } 
    catch (Exception ex) 
    { 
     throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError); 
    } 
} 

यहाँ कोड की निर्धारित टुकड़ा है:

यहाँ खराब कोड का टुकड़ा है: मैं नीचे की तरह अपने सभी सेवा कॉल के लिए एक एक्सटेंशन का उपयोग करने की कोशिश कर रहा था

तो ... शायद आपने देखा कि पहले throw को catch (Exception ex) ब्लॉक में संभाला गया है जो इसे फिर से प्रदर्शित करने के लिए फिर से फेंकता है: 'आंतरिक सर्वर त्रुटि'। शायद यह मदद करता है, क्योंकि मुझे लगता है कि आपके पास वैश्विक

पकड़ (अपवाद अपवाद) {फेंक अपवाद; }

जो इसका कारण हो सकता है।

0

1) WebException में विधि/आपरेशन

2) WebFaultException या WebFaultException

3 फेंक) को faultcontract जोड़े क्लाइंट साइड पकड़ पर और फिर अपवाद प्रतिक्रिया

catch (WebException exception) 
{ 
var resp = new StreamReader(exception.Response.GetResponseStream()).ReadToEnd(); 
} 

एक ही मुद्दे का सामना करना पड़ा पढ़ जैसा कि समस्या बयान में उल्लिखित है और एलबी द्वारा उल्लिखित उत्तर के साथ हल करने में सक्षम है और कुछ अन्य पदों का पालन किया है। तो

संबंधित मुद्दे