DavidSpot Blog

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


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.


knockout.js tricks - debugging and multiple bidings in same dom with custom ignore binding

When using Knockout.js one of he first difficulties found it's how can we debug or see the realtime bindings to our Knockout model. 

There are a few cool tools and tricks we can use to this debug and I will mention the top three I like the most.

  • The first one (and the one I se the most) it's a google chrome extension called Knockoutjs context debugger and the usage it's very simple. Just install the the extension, restart google chrome (if not prompt to do). Then you jus have to open developper tools (on windows just click keyboard key F12).

Then, within the Elements Tab click in any html elements on the web page that it's within the model context you've provided when you call ko.applyBindings(mode, domElement) (by default, if you don't provide any domElement the bindings will be done to body tag) and you will see, os the right side of your developer tools a new tab called Knockout context where you'll have all the current knockout context that is bound to the element you choose. 

Check the example below from the knockout home page. 

This is very cool but at the time of this writing this tool doesn't show real time changes, what I mean by that is if you change a value in the select tag, the context will not change automatically, you need to select the dom again with the dom selector tool

 

  • Another way to debug knockout it's by simple add a pre tag with the bind <pre data-bind="text: ko.toJSON($data, null, 2)"></pre>, and in this way you'll have a real time debug experience, when you change a value from a html element you'll see it change in the knockout pre tag (just don't forget to remove it before going to production)

 

Another common difficulty it's when you want have multiple binding in the same dom witch could be great when you need to do modular development or injecting html code.

A nice and easy trick to do this it's to implement a custom binding witch will ignore a parent binding by using the knockout controlsDescendantBindings flag.

ko.bindingHandlers.stopBinding = {
    init: function() {
        return { controlsDescendantBindings: true };
    }
};

ko.virtualElements.allowedBindings.stopBinding = true;

This custom binding called stopBinding makes use of the controlsDescendantBindings to control binding context and you can even use this in Virtual Elements.

Then you can use like this:

<div>
    <h2 data-bind="text: Title"></h2>
    <!-- ko stopBinding: true -->
    <div id="profile">
        <div data-bind="text: Name"></div>
    </div>
    <!-- /ko -->
</div>

<script type="text/javascript">
	var model1 = {
		Title: ko.observable("Hello World");
	};

	var model2 = {
		Name: ko.observable("David");
	};

	ko.applyBindings(model1);
	ko.applyBindings(model2, document.getElementById("profile"));
</script>

Now you won't get the error "You cannot apply bindings multiple times to the same element" thanks to this little trick and you can have multiple bindings in the same html although in the majority of times you should use only one binding to avoid confusion.


NDepend and Prefix (from Stackify) - Two side's of the same coin to master your .NET Code and become a great developer.

As a .NET developer for some time I rely on IDE's to code better, faster and most important, to collaborative make great software with top quality and maintainable.

The Visual Studio it's the usual suspect for .NET world developers, where you can develop basically anything, but it's not the only one, nowadays you have more options since Microsoft shifted it's way's to the open source world.

Here are some of the main .NET tools available today:

This is all good but sometimes all this IDE's lack in one or other feature and the provide a "plugin system" where we can complete the missing features we need with third parties or even from the company that provides the IDE.

Some of this 2 great tools are NDepend and Prefix (from Stackify), this two tools are so great that I consider them as the two faces of the same coin.

NDepend it's great on giving you tips on the "health" of your code and help you track faster an issue that's happening with your code, I see it like a when you go to health clinic and perform a battery of tests to check how's your body doing. With NDepend It's the same but with code.

The NDepend it's a paid tool, this is the least attractive point, but it's the only one. I have to thank Patrick, who kindly offered me a pro license, to play with this amazing tool.

NDepend has a great integration with Visual Studio and provides nice tips about your code, here are some examples on top features that I used with BlogEngine software (witch is the framework that runs my blog).

  • Great Summary Dashboard and simple to get the important information you need to improve:

On the orange square it's the summary of all your solution, it provides quick and full access to the main info you want to check.

On the red square it's located the Queries and Rules Explorer that gives you metrics and analysis of your code (after clicking on the link Critical Rules Violated)

On the purple square you can see a generated query called CQLinq that enables you to query your code through the old and great LINQ queries (this is awesome and goes in line with the new possibilities brought by the new Roslyn compiler to inspect your code)

Another great feature I liked it's the dependency graph matrix witch could get you a hint, if you're refactoring the amount of impact that a change could do.

Here is a screenshot of the Graph view, I double clicked the green square with the number 345 to check for more info, it drill downed to a more detailed view and showed me a red square around a few namespaces, this means a possible Dependency Cycle issue witch could indicates a problem if you want to do refactoring and, for instance, do some componentization to make your code more reusable.

You can check more detailed information about NDepend features here.

Prefix (from Stackify) it's the tool to have to complete your debugging experience and get more information to help you. Going with the "medical" analogy, Prefix it's like those machines that football players use while running to check how the body behave during physical demand and "debug" possible problems in real time. Prefix is that, but for ASP.NET apps.

You can check everything that's happening in your application since, a web request, database queries, cache, etc, all in a single request. It's a great competition to the Glimpse tool that was integrated in the .NET Team.

This a screenshot from there demo site and you can see that, after I selected a request (GET people.QueueThem) the full sequence of the request, the http handler, the query to the database, the Redis cache info all until the end of the request.

This analytic tool is very powerful and provides you with useful data to track possible bugs (or unexpected behavior like a colleague of mine likes to say).

This is just a few tools, there are tons out there but this one's that I know and have great documentation and features witch for me, it's a "must have" to use any kind of tool.

Happy coding people :)