Posts

Showing posts from August, 2010

DataAnnotations and ASP.NET MVC

In .NET 3.5 SP1, the ASP.NET team introduced a new DLL named System.ComponentModel.DataAnnotations, in conjunction with the ASP.NET Dynamic Data project. The purpose of this DLL is to provide UI-agnostic ways of annotating your data models with semantic attributes like [Required] and [Range]. Dynamic Data uses these attributes, when applied to your models, to automatically wire up to validators in WebForms. The UI-agnostic bit is important, and is why the functionality exists in the System.ComponentModel.DataAnnotations namespace, rather than somewhere under System.Web. For .NET 4.0, the .NET RIA Services team is also supporting DataAnnotations (which have been significantly enhanced since their initial introduction). This means that models you annotate can end up with automatic validation being performed in both client- and server-side code, supporting WebForms (via Dynamic Data) as well as Silverlight (via RIA Services). In our exploration of data support in ASP.NET MVC, we wrote a m...

ASP.NET MVC 2: Strongly Typed Html Helpers

I just started looking into MVC 2.0 and found a useful information that MVC came up with strongly typed Html helper methods which makes our life easier by avoding runtime and allowing you to find errors at build-time. Existing HTML Helper Methods ASP.NET MVC 1 shipped with a set of HTML helper methods that can be used within view templates to help with the generation of HTML UI. For example, to output a textbox you could write code (within your .aspx view template) using the Html.TextBox() helper method below: "HTML.TextBox("Product Name", Model.ProductName)" The first parameter to the helper method above supplies the name/id for the textbox, and the second parameter specifies the value it should have. The helper method above would then render HTML like below back to a browser: "input-> id="ProductName" value="XYZ" name="ProductName" " New Strongly-Typed HTML Helper Methods One of the common feature asks people had for ASP....

Improve code quality using Visual Studio Code Metrics

As the coding is important, we should also think of better coding... We need to think while writing a piece of code; Is the code scalable Is the code robust -> What happens if I do.... Is the code maintainable. What if I leave the company, somebody should understand and do.. Is the code secure enough Is the piece of code reusable Is the UI satisfies user experience Is the code handles exception and logs with proper useful and meaningful messages To achieve this we need to change our practice while coding. To tell there are lot of points and very easy to tell. To be frank I am not able to follow all these every time. But to achieve we need to be extra careful and extra cautious. Initial time to start with, we can use some code improvement tools which are freeware. Ex: FxCop, StyleCop etc... These are tools from Microsoft that checks and analyze the code and styling of the code and allow it to enforce a different set of rules. We can also customize the rules as per our own standards a...

How to Resolve WCF Service problem with Unhandled Exception

Background In one of my project recently I faced a problem where in a WCF service would encounter an exception and would stop working. The exception was related to the caching mechanism. We were trying to access some data from the web server cache using the ASP.Net cache API. There was a callback method used to repopulate the cached item when it expired after a certain interval. If the item was forcefully removed from the cache it would result in an exception when the next call was made to access the value of the cached key. Handle Exceptions to avoid AppDomain from shutting down During development testing the cached item never got removed forcefully and we did not encounter this problem. But during integration testing it surfaced. Last weekend I was reading some stuff on Threading in C# and came across a point made by the author, Joseph Albahari . This is what he had to say “ From .NET 2.0 onwards, an unhandled exception on any thread shuts down the whole application, meaning ignoring...

Trace Enabling for WCF Service

Background Recently while working on a WCF service of my own, I received an error which said “ The underlying connection was closed: The connection was closed unexpectedly ”. This error was occurring while trying to execute the service method from the client. My initial thoughts were that it could be a security issue. After doing some goggling I found that it could be due to serialization issues. Possible causes for this error Many of the articles and blogs suggested that this can occur if we are using Enum in our service and this enum is transferred to the client as part of data contract. We did have couple of enums defined in our service layer and they were getting transferred to the client. To resolve the serialization issue with Enum we need to decorate the Enum class with DataContract attribute. Also each enum member needs to be decorated with EnumMember attribute. We already had this in place for our service. It could also occur if there is mismatch in the way collections are han...