Thursday, September 28, 2006

Today I happend to me for the n-th time! When I have tried to "Get Latest Version" on my solution I got the following message:

---------------------------
Microsoft Visual Studio
---------------------------
Unexpected error encountered. It is recommended that you restart the application as soon as possible.

Error: Unspecified error

File: vsee\lib\path\cvapipath.cpp

Line number: 2459

Of course we have done nothing unusual today. Adding some projects, changing some files etc. All this stuff made using Visual Studio 2005.

As I've mentioned this is not the first time it has happened to me so I thought I knew what to do. I have tried all the hacks that worked previously from deleting the solution file, through deleting the projects that I have found to be causing the problem to getting the files from the computer where it works. Unfortunatelly this time the "standard" procedure has not worked. Strangely enought I was able to commit changes made to the project that caused the problem.

I have been unable to find anything helpful on the internet other than the fact that I'm not alone with this issue.

As the last effort I thought I delete a ".suo" file for my solution. It was a desperate move but it worked! Wow! So basically now if you have any problems with Visual Studio that appear from nowhere, try the .suo file first :-)

As a side note: I have noticed that the new way ASP.NET projects i.e.: no project file, just directory, is a nightmare for source control tools. I have tried Subversion some time ago and the AnkhSVN plugin which in the newest version is said to support the Web Projects but I was a pain setting it up and it does work after all. What is worse is that even Microsoft's own products have problems with Web Projects! Since I remember I have always had problems with SourceSafe and ASP.NET. Be it ASP.NET 1.1 and Visual Studio 2003 or ASP.NET 2.0 and Visual Studio 2005 - always there are problems!

kick it on DotNetKicks.com

Thursday, September 28, 2006 10:56:40 AM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [1]  | 
 Tuesday, September 26, 2006

Previously I have posted an article: What You Should Dispose When Using ADO.NET. There I describe the common pattern for working with ADO.NET objects. Lets go a step further. I think that it is a very bad practice to return a DataReader object from a data layer to some layer above. The reason for this is that in my opinion you should not return move IDisposable objects between layers. Why? Ask yourself a question: who should dispose of such an object? The client layer where the object is used? Or maybe the data layer where the object originates from?

Given the best practices of object oriented programming (OOP) and encapsulation, you should not have to know the inner workings of the objects that you use. In this case you should not have to know if the DataReader is disposed for you or not etc.

But you have to return some kind of data! So what to return? Some time ago I have created a following pattern for running my queries on the database which in a simple form looks more or less like this:

public IEnumerable<DbDataRecord> ExecuteSelect(string commandText)
{
   
using (DbConnection connection = CreateConnection())
   {
      
using (DbCommand cmd = CreateCommand(commandText, connection))
      {
         connection.Open();
         
using (DbDataReader reader = cmd.ExecuteReader())
         
{
            
foreach (DbDataRecord record in reader)
            {
               
yield return record;
            }
         }
         connection.Close();
      }
   }
}

This way the client for this method ends up with a IEnumerable object that looks and acts like a DataReader but is not IDisposable. In fact when you work with DataReader you actually work with the DbDataRecord objects which the DataReader encapsulates.

Notice also the use of base classes such as DbConnection and DbCommand and not concrete implementations such as SqlCommand. This way you can plug any provider you want to this pattern and in fact this is what I do in the helper methods CreateConnection and CreateCommand:

private DbProviderFactory factory;
private DbProviderFactory Factory
{
   
get
   
{
      
if (factory == null)
      {
         factory =
DbProviderFactories.GetFactory(providerInvariantName);
      }
      
return factory;
   }
}
private DbConnection CreateConnection()
{
   
DbConnection connection = Factory.CreateConnection();
   connection.ConnectionString = connectionString;
   
return connection;
}
private DbCommand CreateCommand(string commandText, DbConnection connection)
{
   
DbCommand command = Factory.CreateCommand();
   command.Connection = connection;
   command.CommandText = commandText;
   
return command;
}

The DbProviderFactories and DbProviderFactory are new feature of ADO.NET 2.0 which are useful for making things more generic not that you couldn't do connection.CreateCommand() before ;-).

kick it on DotNetKicks.com

Tuesday, September 26, 2006 10:26:01 AM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [5]  | 

Everyone that has been using ADO.NET to get the data from the database does more or less the same thing. First you create a Connection object, then a Command, then you execute the command to get the DataReader. With reader, you iterate through the collection of records using a while loop. A sample code looks as follows:

string connectionString = "connectionString";
using (SqlConnection connection = new SqlConnection(connectionString))
{
   string commandText = "commandText";
   using (SqlCommand command = new SqlCommand(commandText, connection))
   {
      using (SqlDataReader reader = command.ExecuteReader())
      {
         while (reader.Read())
         {
            //read the data
         }
      }
   }
}

I have noticed that most people use the using statement only for the connection objects because that is what you will find in most of the books and tutorials. There is however one very important thing to remember and that is a simple rule that you should dispose all disposable object as soon as possible. When you look at the ADO.NET objects you will see the connection object is not the only one that can (and should) be disposed. The command object and also the datareader object are also IDisposable so as far as the rule goes you should dispose them. As for the command object I have seen few examples where it is disposed, but I have seen only one where the datareader is disposed.

So just to remind you: dispose your disposable objects!

kick it on DotNetKicks.com

Tuesday, September 26, 2006 9:51:46 AM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [3]  | 
 Monday, September 25, 2006

Today I have discovered that NUnit is distributed without one, thing that is VERY important - at least for me. The thing that is missing is the Xml Documentation for NUnit assemblies!

Since I needed it to work more efficiently, I have investigated the problem. It seams that neither the .zip nor the .msi file have Xml Documentation files included so I have downloaded the sources to see if there is any documentation at all in the code. Fortunatelly there is. Unfortunatelly none of the projects is set to generate the Xml Documentation file so I have changed that and compilled the solution. I ended up with a bunch of xml files that I have copied to the NUnit installation folder and since then I have a help like this:

Instead of just this:

I think that this issue has been reported to the NUnit team, but I'm not sure: here is what I have found in this matter. In the meanwhile feel free to use the files I have generated: NUnitXmlDocumentation.zip (50.17 KB) - compiled for NUnit 2.0 2.2.8

Installation is very simple, just copy the xml files to the directory where you have NUnit assemblies installed. Typically it will be something like: c:\Program Files\NUnit-Net-2.0 2.2.8\bin\.

kick it on DotNetKicks.com

Monday, September 25, 2006 12:49:20 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 

I have written an article on Visual Studio Code Snippets - how to use them, and how to create your own. I have provided there a couple of snippets that I use on daily basis. You can also check Mads Kristensen's blog where he provides a snippet for "safe events". Today I have thought of yet another useful snippet...

There often happens that you need to have some kind of property that does some calculation yet you do not want it to be a method. Maybe it is because of the ease of use or some other reason? But as far as best practice tells you, the property is expected not to perform any kind of lenghty operation. So in order to be able to make a property that does some time consuming task the result of which can be then quickly retrieved any time, you do a what is called a Lazy Loading or Lazy Initialization - one of the most useful patterns.

Suppose that we have a tree structure of some operations. Each operation is either an atomic operation or a composite operation consisting of sub operations - a simple composite pattern. Each atomic operation has a property that shows the number of arguments the operation requires to execute. So to count a total number of arguments required by the tree of operations you would have to recursively sum the number of parameters from all sub operations. And that is the operation that can take some time to perform given a large tree of operations. So there is a a choice of making exposing this functionality as a method or doing the Lazy Initialization.

If you go the "lazy" way, you will end up with something like this:

If using 2.0 version of the .NET Framework, you can do better and use the Nullable type for the field. And that is exactly the code snippet I provide. With it you can create such a lazy initialized property very quickly. Just type: "propn" and what you get is:

Just fille the green places.

I have added the snippet to my collection of Snippets.zip (2.23 KB). Feel free to use it.

kick it on DotNetKicks.com

Monday, September 25, 2006 10:39:09 AM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
 Saturday, September 23, 2006

Today I have played a bit with Microsoft's new command line tool called Windows PowerShell. Other names I have heard over the years include Microsoft Shell. The code name for the project was Monad. As usual, Microsoft cannot make it's mind and so, expect a lot of confusion. As a simple example: what would you expect the abbreviation for Windows PowerShell would be? WPS or PS maybe? But what when you install the tool, it has an icon like this: . If you look closely you will see the "MSH"...

Putting the naming inconsistency aside, I have decided to give it a try. It took few seconds to start for the first time, and I was presented with a message asking me if I want to run a software from untrusted publisher, which happens to be non other than Micorsoft Corporation:

Since this is one of my favourite companies, I just had to agree :-). It wasn't enough to agree just once ("[R] Run Once") because I was asked the same thing for a couple of times, so I have decided to trust MS always. Few seconds later, I had a command prompt ready.

The first impression is that it is very slow. The auto-completion function is slow, moving between directories is slow. But by slow I do not mean few seconds. I just mean it is slower than the plain old CMD. Fortunately, the slowness does not impact the usability of the tool, which brings you the power of .NET to the command line.

Just for a taste of how it works I have tried using some familiar objects and methods I know from programming in Visual Studio. So first off I called the ordinary "dir" command in the root folder of the C: drive and the output was more or less what would be expected:

Nothing unusual here. So where is the power of the PowerShell? The next command will say it all: "dir | foreach { $_.GetType().ToString() }" and the output for the same directory as above:

The command basically does some kind of operation: "GetType().ToString()" on each object thanks to the "foreach" command. As you can see, everything is an object in the PowerShell and as such you can execute methods on it or use it's properties. That is what I call Power Shell! Where are the linux shells hiding now :-)

As a side note. I really think Microsoft should do something to make the standard black window for the shell somewhat more usable - whatever that means. One feature that I would like the most would be the autocompletion of command parameters. I have seen this on one of the unix-like systems so it certainly is possible! Nevertheless PowerShell will rock even without it!

kick it on DotNetKicks.com

Saturday, September 23, 2006 1:56:22 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [1]  | 
 Friday, September 22, 2006

Have You ever wondered how you can add a link somewhere on your page, that will enable your users to submit your article to digg.com? It is rather easy to do it the simple way, by copying the digg link from digg.com, and putting it somewhere on your site for example as a hyperlink on some image. The digg url usually looks like this:

http://digg.com/programming/Microsoft_s_Unit_Testing_Framework_is_useles

Using it with image you get the following:

digg iconDigg This!

Most of the sites on the Internet currently provide you with the above. Is it perfect? I thought not, so I have searched for the better solution. What I wanted is to get the same digg link that is used on the digg.com. And after a while I have found few pages that explain how to do it. It's really easy. Just use the code below:

<div class="diggbutton">
<script type="text/javascript">
digg_url = 'http://digg.com/programming/Microsoft_s_Unit_Testing_Framework_is_useles';
</script>
<script type="text/javascript" src="http://digg.com/api/diggthis.js"></script>
</div>

And what you get is:

kick it on DotNetKicks.com

Friday, September 22, 2006 1:18:18 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, September 21, 2006

After almost two months I have decided to change the theme of my blog. The reason for this was simple: the previous theme was layed out using tables and that made it very difficult to make changes. Additionally I should now rank higher on Google since the structure of the documents is much cleaner. Time will tell.

Just for a reminder of how the blog looked like before:

Thursday, September 21, 2006 8:57:26 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
 Thursday, September 14, 2006

Every once in a while when I'm doing some WebServices I get to the point where I need to use the same class on both the server and a client. There are voices that say that you should not do it and that is against the idea of web services. In my opinion though it is not so! What I want to get is to use the same Data Transfer Object on both ends of the line. The reason for this is that I already have it defined in one place and there is no reason to define it once again IF I can reuse the implementation. Other clients - those that do not have the access to my classes may still use the definition from the WSDL language to generate their own classes - nothing is lost here.

The really good article explaining how and what you can do to make your life easier can be found here. A nice read and good to know stuff - really.

Now I wanted to give it a try. Unfortunatelly I was unable to deploy the solution to the PrivateAssemblies folder of the devenv.exe. I got an error when I tried add a web reference. Installing assemblies in the GAC helped.

Next step was to try to debug this stuff. This is actually pretty easy once you know the trick which is to use 2 instances of Visual Studio. One VS with the sources of the SchemaImporterExtension code and the other one which you will use to add web references. In the first one go to Debug->Attach To Process... menu item. Select Devenv.exe from the list (there should be one if you have only 2 visual studios) and attach to it. Now in the second Visual Studio you can add a web reference and... if you have a breakpoint somewhere in the first VS it should break the execution there. In the example file from the above mentioned site put a breakpoint on the line where you have: "public override string ImportSchemaType" and it should work :-).

You can debug almost anything in such a way - by attaching to the projects. What is not so obvious is that you can also attach to the Visual Studio itself. All you need is a second instance :-). This was pointed to me by some of the readers of my blog, when there was an issue of debugging ASP.NET BuildProviders.

kick it on DotNetKicks.com

Thursday, September 14, 2006 12:14:38 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 

Today I have encoutered a problem with debugging my web projects in Visual Studio 2005. The message was:

"Unable to start debugging on the web server. The server committed a protocol violation. Section=ResponseStatusLine".

I haven't changed anything in my software configuration since the beginning of the procject so I thought that something may be wrong with my IIS. I have searched the Internet for the error message and I have found the following article describing the problem. Basically it was Skype that made a conflict. I have done as the author suggests and it worked! Many thanks to Martin for investigating the issue. Just remember to start the IIS after changing options in Skype because it is now running (at least such was my case).

The strange thing is that I have allwasy have Skype running so why it conflicted today and not before? Strange...

 

Thursday, September 14, 2006 8:48:32 AM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
 Monday, September 11, 2006

Today I have tried creating unit tests using the brand new stuff from Microsoft - their unit framework that comes with the Visual Studio Team Edition. Now that was a major disappointment. I wonder how Microsoft comes out with a crappy stuff like that all the time.

The problem I have encountered is that it is not possible to use inheritance in your test classes. Take for example a base abstract class in which you define tests for your objects hierarchy. One of the method is an abstract method which returns a concrete object and the other methods are test methods as in the example below:

public abstract class BaseTest
{
[
TestMethod]
public void ConstructorParameterShouldBeSaved()
{
Guid id = Guid.NewGuid();
BaseClass obj = CreateInstance(id);
Assert.AreEqual<Guid>(id, obj.Id);
}

protected abstract BaseClass CreateInstance(Guid id);

}

Now I create another test class that inherits from the BaseTest and provides an implementation for the abstract method. Nothing an NUnit framework cannot do. But guess what? It is not supported!!! The Visual Studio complains about the TestClass attribute defined on abstract class - error UTA003.

That defeats all my desire to use MS technology so I switched to NUnit.

kick it on DotNetKicks.com

Monday, September 11, 2006 9:53:38 AM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [1]  | 
 Wednesday, September 06, 2006

For a long time now I have been aware of this feature but still I think many users of Internet Explorer do not know about it. The feature that I'm talking about allows you to save a complete web page INCLUDING pictures to a single file so it can be used later without having to connect to the internet. This feature is accessible from the File->Save As... menu after you select the Web Archive (mht file) as a type of the saved file:

Note that there is also another option to save an entire page using the same dialog box and selecting Webpage, Complete option. This however creates one file for the page and a folder containing the rest of the required files such as images. This is not a very ellegant solution in my opinion.

As good as this feature is, there are occasionally problems with saving the page and the only information is that there was some kind of error (don't we love this kind of messages?). Other than that I think you will love it :-)

BTW: I haven't seen such a feature in the standard installation of the FireFox but I suppose that there is some plugin just for that.

Wednesday, September 06, 2006 9:41:13 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  |