Sunday, 22 March 2009

Integrating PartCover with Visual Studio 2008

One of essential tools in the software development is code coverage. Together with unit testing tools like NUnit it is easy to see how much of the code is covered by tests. Ideally Developer need to aim for 100% of code coverage when creating Unit Tests. This tutorial will use one of my software projects Windows Environment Variables Manager or EnvMan as an example.  On EnvMan website you can find another post that shows integration of PartCover with Visual Studio 2005. Technically there is no much difference between Visual Studio 2005 and 2008, this post covers version 2.2.0 of the PartCover and shows updated batch script as latest version of the PartCover required some changes to it.

PartCover.cmd

As first step we need to download and install latest version of the PartCover. It is available from SourceForge site. Once installed we need to create PartCover.cmd batch file.

REM === Start PartCover.cmd ===
"C:\Path\To\PartCover\partcover.exe"--target "C:\Path\To\NUnit\nunit-console.exe"--target-work-dir %1 --target-args %2 --output %1\partcover.xml --include %3

"C:\Path\To\PartCover\PartCover.Browser.exe" %1\partcover.xml
REM === End PartCover.cmd ===

Integrating PartCover with Visual Studio 2008

To integrate PartCover with Visual Studio select menu Tools\External Tools ...

In the External Tools Dialog box click Add and enter the following data in the fields.

Title:

Part Cover

Command:

C:\Path\To\PartCover\PartCover.cmd

Arguments:

$(ProjectDir)\bin\Debug $(ProjectDir)\bin\Debug\$(TargetName)$(TargetExt) [*Assembly Regular Expression*]*

Initial Directory:

$(ProjectDir)\bin\Debug

Tick Use output Window and Prompt for Arguments. Click OK

Running PartCover

To run PartCover select EnvManagerTest project and use Part Cover from Tools menu. Because we ticked "Prompt for Arguments" the following dialog box appears.

Replace "Assembly Regular Expression" with "EnvMan" so it looks like this [*EnvMan*]*. Please look at PartCover documentation for more details about regular expressions. After OK is clicked output window of Visual Studio will show a progress of the coverage test and Part Cover Code Browser window will appear. Unfortunately PartCover Code Browser does not open generated report file automatically. Use Open File menu to view generated partcover.xml report file.

Tuesday, 24 February 2009

Creating Service Reference: Failed to generate code for the service reference

I was working on Windows Service that was consuming another WCF Windows Service. I created a Service Reference in Visual Studio 2008 and noticed that client code was not automatically generating. The error I was getting was

Error 4 Custom tool error: Failed to generate code for the service reference 'MyWindowsService'. Please check other error and warning messages for details.

The Windows Service I was working on was already full blown project with lots of references, so to limit possibilities I created another empty windows service and added Service Reference to one I need. Client code was generated with no problems! I decided to use new Windows Service as a base and added all required functionality to it and selected “Update Service Reference” and again it failed to generate a code!

image

After some time looking around there was an easy solution to fix a problem. You need to edit Service Reference Configuration by selecting “Configure Service Reference…” from right click menu.

image

In the dialog box Un-tick “Reuse types in referenced assembles” and click OK. Try to Update Service Reference. This worked for me!

References

.NET C# Broken WCF service reference in VS 2008

Friday, 13 February 2009

ASP.NET page validation and redirection to a new page

I was working on the ASP.NET web site where users register using form and get redirected to a different page on pressing submit button. Easy way to redirect to a new page is to use PostBackUr property of the button.

<asp:Button runat="server" ID="BtnSubmit"  Text="Submit" PostBackUr="newpage.aspx" />

Unfortunately this does not work if we need to handle button OnClick event. Solution is to use Server.Transfer or Response.Redirect of the Page control.

protected void BtnClick(object sender, EventArgs e)
{
        if (Page.IsValid)
        {
                // Do something ...

                Response.Redirect("newpage.aspx");
        }
}

Page.isValid is true if all validators on the page and in web controls returned valid. A very good explanation about the difference between Server.Transfer and Response.Redirect can be found in the link below.

Server.Transfer Vs. Response.Redirect

Friday, 6 February 2009

Getting Started with AJAX development and AJAX Control Toolkit

Developers who use Visual Studio 2008 know that AJAX already built into ASP.NET for .Net 3.5 Framework. All they need is to get AJAX Control Toolkit.

AJAX Control Toolkit .Net 3.5

How Do I: Get Started with ASP.NET AJAX? is an excellent video that helps to get started.

AJAX for .NET 2.0

If you are developing pure .Net 2.0 ASP.NET website or still using Visual Studio 2005 you need to get AJAX 1.0 and  AJAX Control Toolkit for .Net 2.0.

ASP.NET AJAX 1.0

ASP.NET 2.0 AJAX Templates for Visual Studio 2008

AJAX Control Toolkit for .NET Framework 2.0, ASP.NET AJAX 1.0

AJAX Tutorials and Documentation

AJAX Website has extensive number of Tutorials and Control Toolkit has a sample website that will be very helpful to make learning curve shorter.

ASP.NET AJAX Videos

ASP.NET AJAX Documentation

AJAX Control Toolkit Tutorials (C#)

ASP.NET Learning Centre

Deployment of AJAX enabled websites

Once development is finished and project need to be deployed to testing or production servers, install ASP.NET AJAX 1.0 on the server if you are deploying ASP.NET 2.0 website. ASP.NET 3.5 website will require only .NET 3.5 runtime installed on the server. There is no need to install AJAX toolkit as long as toolkit DLL is distributed in bin folder.

Wednesday, 28 January 2009

ASP.NET Access DB write problem

Recently I was working on simple ASP.NET website that had to update Access Database. Everything was fine during development when I was using ASP.NET Development Server created by Visual Studio. Later when I connected with Internet browser to http://localhost/mywebsite/Default.aspx I started getting two kind of unhandled exceptions on Database insert and update.

Microsoft JET Database Engine error '80004005'

The Microsoft Jet database engine cannot open the file 'C:\DB\YourDatabase.mdb'. It is already opened exclusively by another user, or you need permission to view its data.

or this one

Microsoft JET Database Engine error '80004005'

Operation must use an updateable query.

After looking on the internet I found a couple of links that shed some light on this problem.

How Do I Fix ASP 80004005 errors? - Imar.Spaanjaars.Com

An unhandled exception may occur when you try to connect to an Access database from an ASP.NET worker process.

I tried to fix a problem by following instructions on these pages, unfortunately adding permission for IUSR_MyComputerName system user to access DB folder did not make a difference. The quick and easy (and NOT RECOMMENDED for production installation) solution is to add Everyone to access to DB folder and write to database. I want to repeat that this is not a good solution for security reasons, my preference is to try to change an account under which IIS service is running. I will post my solution later once I get it working.

References

How to configure file sharing in Windows XP