DavidSpot Blog

Developer, super curious, a dog lover, sports fan and techie guy


Remove method name with suffix "result" in a RESTful WCF response

When you are developing a REST service with WCF it's expected a clean json output in a response like this:

{
orderId:"1",
description:"blue t-shrt"
}

But in WCF you can get a result like this:

{
     GetProfileOutputResult:
     {
        orderId:"1",
        description:"blue t-shrt"
     }
}

To remove the unwatend "*Result" object you must decorate your method with de WebGet Attribute em set the BodyStyle to WebMessageBodyStyle.Bare.

You can check the little difference in the images below:

(With the "Result" suffix)

(Without the "Result" suffix)


Fixing generated WSDL files with weird names using WCF and svcutil.exe

A while back I was developing SOAP services using the WCF framework from Microsoft and when as was checking the generated WSDL, the proxy client, I checked that one the arguments of a method had a weird name:

 

This is the original name was this: SetMobileTokenContextBSGInputData. So why the extra suffix jvQP5IK6? Why was WCF adding this?

The thing is that WCF assumes that you can have multiple "classes" within different namespaces, so to avoid confusion, it added this kind of hash to the end of the class.

To avoid the behavior I just added the attribute XmlSerializerFormat to the interface signature and the "confusion" was solved and I had pretty names generated.

After this here is the pretty WSDL:

Hope this help someone :)


Handling and redirect to a view an exception in a .NET MVC app using OnException method

When you have an error in a .NET MVC application in Production you normally don't wan't the typical yellow screen with all the stacktrace described. You want to handle the error and present a nice and clean page.

To do that simply override the method OnException in the controller and redirect to your erro page.

A sample code could be as simple as this:

protected override void OnException(ExceptionContext filterContext)
{
       filterContext.Result = new ViewResult
    {
        ViewName = "~/Views/Shared/Error.aspx"
    };
    filterContext.ExceptionHandled = true;
}

Note that the ExceptionHandled property is set to true, if you don't do this, your exception will bubble up until the method Application_Error and will not be handled by your error view.