2

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?

1
  • 1
    In my opinion you shouldn't worry about cloning a class in this scenario. This is JSON and this is SOA, not OOA (.NET Remoting, Java RMI). Furthermore, you have no WSDL. What you're doing now, manually is what in the classic ages would automatically happen behind the scenes when you added an ASP.NET Classic web service client stub. The JSON that gets transmitted only takes the shape of the original class, which is part of System.Web. You are simply trying to conveniently parse that shape into a more accessible form - an object. Good luck! Commented Mar 11, 2015 at 0:21

1 Answer 1

1

No, the way you have is just fine. Adding Web API core does not make sense for your client app as its only for a server/service application.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.