Archive for the ‘dotNet’ Category

Typemock ASP.NET Bundle

Thursday, May 21st, 2009

Hi everyone. After using Typemock on one of my projects, I can definitely say that it is a fantastic product for your unit testing. Have just heard about a promotion which Typemock is running currently to promote their new produt and giving away licences. See the details below:

Unit Testing ASP.NET? ASP.NET unit testing has never been this easy.

Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle – and for the launch will be giving out FREE licenses to bloggers and their readers.

The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both Typemock Isolator, a unit test tool and Ivonna, the Isolator add-on for ASP.NET unit testing, for a bargain price.

Typemock Isolator is a leading .NET unit testing tool (C# and VB.NET) for many ‘hard to test’ technologies such as SharePoint, ASP.NET, MVC, WCF, WPF, Silverlight and more. Note that for unit testing Silverlight there is an open source Isolator add-on called SilverUnit.

The first 60 bloggers who will blog this text in their blog and tell us about it, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you’ll get a license automatically (even if more than 60 submit) during the first week of this announcement.

Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends.

Go ahead, click the following link for more information on how to get your free license.

Getting all XML Key Value pairs in XSL

Friday, January 30th, 2009

May times when I try to design a XSL against an unknown data stream, I would like to know what all values are available. Here is a nifty XSL which I have been using for a while. ( I am sure there are many versions out there ).

<xsl:for-each select="@*">
    <br/> Name: <xsl:value-of select="name()" /> Value:<xsl:value-of select="." />
</xsl:for-each>

It gives a list of all elements with their values. Quite handy to check if you have got the element name wrong etc.

Configure ASP.NET Websites to output XHTML to W3C Validator

Wednesday, November 12th, 2008

In my previous blog entry, I showed how to configure ASP.NET to output XHTML to the browsers. However, when the W3C Validator checks the page, ASP.NET does not know the W3C user agent and hence sends out non compliant code. The trick is in the w3cvalidator.browser file available on idunno.org. Just drop the file in your App_Browsers folder and ASP.NET will send out XHTML compliant code to the Validator.

Making ASP.NET XHTML Compliant

Wednesday, November 12th, 2008

ASP.NET by default does not emit XHTML compliant code. Consider this scenario. You have carefully created an XHTML compliant design. Created your pages using ASP.NET and when you try to validate your page, it fails with the error :

Line x, Column y: there is no attribute “name”.

<form name="aspnetForm" method="post" action="Default.aspx" id="aspnetForm">

This leaves you a bit stunned, as ASP.NET adds the name tag to the form on compile and you don’t have any control over it. However, don’t worry. There is a solution.

Just add to following under system.web in your web.config file. 
<

 

xhtmlConformance mode=Strict/>

ASP.NET should generate compliant code now, and you should see the following in your rendered output.

<form method="post" action="default.aspx" id="aspnetForm">

As you see, it does not render the name tag anymore. Problem solved. Time to display the W3C Validated icons on your page with pride :)

Analyse your Source Code

Friday, June 6th, 2008

I recently gave Microsoft Source Analysis a try. It was released sometime ago, and act’s as an add-in in Visual Studio to check the source code with some coding rules. This is different from FxCop which checks assemblies. This checks your source code before it is compiled right in Visual Studio editor.I did like the tool. Quite handy, but will need a little bit getting used to.

Some companies may need to change some rules to match their coding standards. For people who don’t have a particularly though out coding standard in place, and are using something from the ASP / VB / C days, it may be worth changing over the standard based on the Source Analysis rules, as it has been in use for quite a while at some parts of Microsoft, and from an overall look, it does look quite a decent one.

There are a few irritations, but you can turn rules on and off. The most irritating part is that it throws up tons of errors in the Visual Studio generated code. Also, another irritation is that the default rules need a header on every page. However, it is not a bad thing. Either you can toggle the rule off, or write a header. Doh… Most of you like me will go, how do I write a header which Source Analysis will understand? Well, worry not, help is at hand.You can use the following header for your code files:

//———————————————————————–
// <copyright file=”Program.cs” company=”Your Company Name”>
// Copyright (c) Your Company Name. All rights reserved.
// </copyright>
// <author>Your Name</author>
//———————————————————————–

What about the Visual Studio auto-generated files? You don’t need to worry about Source formatting and coding practices in those files. So how do we exclude them from being checked? Well, there is a simple enough way.Just add the following header in all files you don’t want checked by Source Analysis.

// <auto-generated />


For more details about headers, you can check out the rules documentation on the Source Analysis Blog.


Lastly, now, you are sort of morally forced to document every method. I know it’s a bit of an effort for all lazy developers like me. So GhostDoc come here to rescue us from our plight. For those of you, who don’t know about Ghost Doc, shame on you. Just kidding ;) It is a cool VS Add-In by Roland Weigelt, which auto generates method descriptions by guessing it from the description. It is not perfect, but definitely a big leap from the Visual Studio way. Download it and I can assure you that you will seriously get addicted to it. Combine together Ghost Doc and Source Analysis, make coding much clearer, better documented and developer friendly.

Debugging .NET Source Code

Thursday, January 17th, 2008

Shawn Burke has a nice article which explains how to enable Visual Studio to download the symbol files and enable debugging for the .NET Source Code in your project. This is definitely a big help, as we no longer have to treat internal .NET assemblies as a black box, but gives us a the power to find out why an error happened and what kind of data the runtime expects?


Have a look at this excellent article and start configuring your development environment to support it.

dotNet 3.5 Namespace Poster

Tuesday, November 20th, 2007

Just a quick update. Found this cool poster of .net 1.0 – 3.5 namespaces. Definitely a must to hang around any developer’s cubicle ;)  Download it straight from Microsoft.

.NET Code Performance

Wednesday, November 22nd, 2006

I recently had been busy performance tuning .NET code, and I was very surprised at some of the findings as I timed and profiled various code modules. I had done a lot of performance tuning with C++ before I migrated to .NET. Had not done any serious performance tuning till now with .NET. While I was profiling a SharePoint WebPart, these findings would be equally applicable to any .NET code.


I was surprised to see that accessing a property off an object had a surprising big hit. e.g.


MyObject is an object which has a property say, ID.


I have a loop of code which uses the MyObject and tried to compare the ID with something…


for (int i =0; i < 100; i ++)
{


if ( MyObject.ID == something)
do something…
}


I observed that there were a few milliseconds being spend whenever I tried to access MyObject.ID.


Alternatively the following code ran much faster.


int id = MyObject.ID


for (int i =0; i < 100; i ++)
{


if ( id == something)
do something…
}


I guess the overhead of accessing the object and getting the property value does have a hit and can take up time if say you have a recursive function or an iterative function. Will be careful of such cases in the future.


I guess I will spend a bit more time exploring the internals of .NET to profile and find out such cases which can make a difference in our code.


P.S. I know I have not been posting to this blog for a bit. I am trying to move my blog to a better host and with better control over it. Yahoo Hosting is not adequate for it, as I cannot do a lot of things I plan to do with my Wordpress installation. Hence will mostly be moving this site to a new server which does not have restrictions. So busy evaluating offers and packaged from hundreds of hosting companies to find one which is just right for a geek developer like me, who would like to have enough control when needed from time to time.

Code cannot find reference to assembly in GAC

Friday, September 29th, 2006

I came across this error today which made me quite curious on why it is happening. This occured to me in a SharePoint WebPart, but it is a very general .NET Framework error and as such can occur in any type of .NET application.


CS0011: Referenced class someClass’ has base class or interface
someNamespace.someBase’ defined in an assembly that is not referenced. You must add
a reference to assembly ’someNameSpace’.


To explain the scenario. You have an assembly in the GAC with some classes. You have some code which you are running which reference the GAC dll. For my case, I was inheriting my webpart from one of the baseclasses defined in the GAC assembly. If I put the assembly in the /bin directory as well as the GAC it worked beautifully, but if I deleted the assembly from my /bin directory, I got the above error. This sort of nullified the entire purpose of having a GAC.


What was happening was that the code was trying to reference the someNamespace.someBase Class but as there was no link in the code to provide it with a hint that the required assembly is in the GAC, it was complaining. As soon as the dll was in the /bin directory it was able to reference it and all was ok. To solve the problem, there are two ways to do it.


1. If you have a ASP.NET application, you can add a @Register Assembly derivative in your page.
2. You can add the reference in your web.config to hint the application to look for the assembly in the GAC by adding the following lines in your web.config file..
<system.web>
<compilation batch=”false” debug=”true”>
<assemblies>
<add assembly=”someAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=s0m3publ1ck3y” />
</assemblies>
</compilation>
</system.web></code>


The solution was found, the day was won. But it made me wonder if this is a design defect with .NET. If placed in the \bin directory, the application was very happy to reference the dll. If an assembly is in the GAC, why do we have to explicitely tell out application to look for the reference in the GAC. Shouldn’t it be doing that automatically in the first place.

Extracting dll’s out of the GAC

Tuesday, June 13th, 2006

I was recently asked how to reference the Microsoft.SharePoint and few of the other SharePoint dll’s in a project while developing on a non SharePoint machine. The solution is quite simple which we often seem to neglect.


This is applicable for any assembly which is installed in the GAC. If you use the Windows Explorer, you can see those assemblies in the Windows\Assembly\GAC folder, but cannot copy them out of there. To access the internal structure, use the command shell, by typing cmd in the Start->Run menu. Navigate to the GAC folder and you will see all installed dll’s as folders, which inturn will contain the corresponding folders for each version. Just copy the dll’s from there to some other location and you have successfully extracted the dll’s from the GAC.