Вопрос по rest, asp.net-mvc, c# – Как обработать исключение WebFaultException для возврата CustomException?
Я сделал свое собственное исключение, которое будет выбрасываться внутри try-catch при каждом возникновении ошибки:
[Serializable]
public class CustomException : Exception
{
public CustomException() { }
public CustomException(string message)
: base(message) { }
public CustomException(string message, Exception innerException)
: base(message, innerException) { }
}
У меня есть две службы, REST и SOAP. Что касается сервисов SOAP, у меня нет проблем с созданием пользовательского исключения. Но в REST я столкнулся с множеством трудностей.
Вот метод для выброса WebFaultException:
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 - это класс, в котором есть некоторые свойства, которые я использую для размещения всей необходимой мне информации.
Я использую этот метод, чтобы вызвать исключение внутри службы REST:
public static CustomException GetFault(ServiceFaultTypes fault)
{
string message = fault.ToString();
CustomException cusExcp = new CustomException(message, new Exception(message));
throw cusExcp;
}
Пример службы REST (метод входа в систему):
[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;
}
}
Я ожидаю, что служба REST вернет это на стороне клиента:
Но, в конце концов, он всегда возвращает это:
Что я должен делать? Пожалуйста помоги.
EDIT
Вот пример ответа при вызове службы мыла:
[FaultException: InvalidLogin]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +9441823
Вы видели & quot; InvalidLogin & quot; ? Это то, что я хочу увидеть в ответе REST servivce.
Пример ответа от REST:
[WebException: The remote server returned an error: (500) Internal Server Error.]
System.Net.HttpWebRequest.GetResponse() +6115971
Я бросаюWebFaultException
но я получаюWebException
.
Если я не смогу получить точное сообщение об ошибке на REST, я пойду на SOAP.
Спасибо за ответы.
1) Добавьте в контракт / метод неисправности
2) Бросить исключение WebFaultException или WebFaultException
3) На стороне клиента поймайте webexception и затем прочитайте ответ об исключении
catch (WebException exception)
{
var resp = new StreamReader(exception.Response.GetResponseStream()).ReadToEnd();
}
С такой же проблемой столкнулся, как упомянуто в заявлении о проблеме, и смог решить ее с помощью ответа, упомянутого Л.Б. Итак, подытожив следующие шаги
У меня просто была похожая проблема несколько минут назад. Может быть, это поможет. Я пытался использовать расширение для всех моих вызовов службы, как показано ниже:
ЗдесьBAD кусок кода:
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);
}
}
ЗдесьFIXED кусок кода:
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);
}
}
Итак ... Возможно, вы заметили, что самый первыйthrow
, обрабатывается вcatch (Exception ex)
блок, который выбрасывает снова, заставляя его отображать всегда:'Internal Server Error& APOS; , Может быть, это помогает, потому что я вижу, что у вас есть глобальный
catch (Exception exception) { throw exception; }
что может быть причиной этого.
Когда используешьHttpWebRequest
(или клиент Javascript), ваше пользовательское исключение не имеет для них значения. Просто Http коды ошибок (вроде500 Internal server error) и данные в содержании ответа.
Таким образом, вы должны обработать исключение самостоятельно. Например, если вы ловитеWebException
Вы можете прочитать содержимое (сообщение об ошибке) в формате Xml или Json в зависимости от конфигурации вашего сервера.
catch (WebException ex)
{
var error = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
//Parse your error string & do something
}