मुझे लगता है कि कोशिश पकड़ के अंदर हर बार कोई त्रुटि हुई है फेंक दिया जाएगा अपने कस्टम अपवाद बनाया:कस्टम अपवाद को वापस करने के लिए 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;
}
}
मैं ग्राहक पक्ष पर इस वापस जाने के लिए बाकी सेवा की उम्मीद कर रहा हूँ:
लेकिन में अंत में, यह हमेशा इस वापसी:
मुझे क्या करना चाहिए? कृपया मदद करे।
[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
प्राप्त करते हैं।
यदि मैं आरईएसटी पर सटीक त्रुटि संदेश प्राप्त नहीं कर पाऊंगा, तो मैं एसओएपी के लिए जाऊंगा।
उत्तर के लिए धन्यवाद।
कोई जवाब नहीं दिए गए चरणों का संक्षेप में? हम्म ... :( – fiberOptics
क्या यह एक डब्ल्यूसीएफ सेवा है और यदि ऐसा है, तो क्या आपकी कॉन्फ़िगरेशन क्लाइंट में त्रुटियों को वापस करने के लिए सेट है? – Slick86
मैं उसी प्रकार के उत्तर की तलाश में हूं ... मुझे लगता है कि मुझे पता लगाना नहीं है हैंडलर की तरह एक आरईएसटी कैसे बनाना है जो एक सार्थक अपवाद फेंकता है। मुझे मूल रूप से टेक्स्टस्टैटस का उपयोग करके पास या विफल होने के कारण मेरी जावास्क्रिप्ट से निपटना होगा। – farina