Background - I have a .Net 4.5 WPF client application. The WPF client application uses the Web API 2.2 Client libraries to load data from a asp.net web api service. This all works.
My question is in regards to error handling.
The error handling from the asp.net web api service returns HttpError objects when there is an error. You can see them over the wire in fiddler for example like:
{
"Message": "An error has occurred.",
"ExceptionMessage": "Index was outside the bounds of the array.",
"ExceptionType": "System.IndexOutOfRangeException",
"StackTrace": " at WebApiTest.TestController.Post(Uri uri) in c:\\Temp\\WebApiTest\\WebApiTest\\TestController.cs:line 18\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClassf.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"
}
Errors are correctly returned to the client when there is an error, no issue here.
Question - My issue is in regards to deserializing these errors. The HttpError object is defined in the System.Web.Http assembly which is part of Web API Core. Since I am just using the Web API Client libraries which doesn't have a dependency on System.Web.Http this class is not available. Hence I am deserializing the error like so:
error = response.Content.ReadAsAsync<RestError>().Result;
Where RestError is a class I have defined locally in my application.
public class RestError
{
public string ExceptionMessage { get; set; }
public string ExceptionType { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
}
The above works but feels like wrong due to the duplication of the HttpError class.
Is there a better solution than defining my own class for HttpError or adding the full Web API Core to my WPF application?