Archive for the '.net' Category

Visual Studio TFS: How to undelete file(s)

What if you need to undelete a file or bunch of files that at some point have been deleted from TFS? Naive option would be to get the content of the file, copy it, create a new file, and paste the copied content to just created file. Of course, this is not a good option because history of changes to the file(s) will be lost.

Continue reading ‘Visual Studio TFS: How to undelete file(s)’

C#: How to get service name listening at specific port number?

How to get service name listening at specific port in C#? What you have as input is only two pieces of information: host name and the port number the service is listening at.

Solution

Apparently, .NET does not provide such feature so one needs to stretch a bit to get the answer. What I can suggest (I’m far from saying it’s good approach, though) is to get the name in two steps:

  1. Use netstat -a -o and parse the output (ouch!) to get ID of the process (PID) that is listening at given port number
  2. Perform a WMI call to get the name of the service: SELECT Name FROM Win32_Service where ProcessId = PID

Following this will give you what you want, but to be honest any time I need to parse output to get some information I feel anxious… This is the first place in the code where errors can be introduced.

If there is/are better/safer way(s) to retrieve service name having the host name and port it’s listening at, please share it.

Visual Studio: Improved navigation through the files with RockScroll

If you have never used RockScroll you are probably most comfortable with standard scrollbar Visual Studio offers. I guarantee you, however, that the moment you install RockScroll and work with it for a while, you will miss it a lot if you switch to Visual Studio that’s not extended with it. I’ve experienced that many times when kneeled at a teammate’s desk trying to help him move on with their task. This is probably best moment when you will realize that Visual Studio misses a thing without RockScroll icon smile Visual Studio: Improved navigation through the files with RockScroll

Here are most important pros that make me think RockScroll is must-have plugin for Visual Studio:

Continue reading ‘Visual Studio: Improved navigation through the files with RockScroll’

Book review: LINQ Unleashed for C#

linq unleashed Book review: LINQ Unleashed for C#

I find this book relevant and very informative. If you want to master LINQ lingo, just read it.

For broader evaluation see my review on DZone.

P.S. DZone’s IT Book Zone is another great initiative. In short DZone gives you a free copy of a book and expects to receive its review in return. Fair agreement – one can stretch the envelope of IT domains for free, while DZone broadens thier public resources.

MSTest: Unit Test Adapter threw exception: Type is not resolved for member XXX

This was not an easy one… I was trying to run a unit test with MSTest but I was always getting the following error:
Unit Test Adapter threw exception: Type is not resolved for member ‘XXX,XXX Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’

As usual in such case – a message which does not really say what’s wrong. I googled the problem but there was not much about it on the web. The best resource I found was post titled VSTS Unit Test ‘Type is not resolved’ exception. It describes how VSTestHost process runs the test and explains what the possible problem might be in this case.

The author suggests that data required for test (e.g. a dll file) is not found in base directory for AppDomain (i.e. unit test ‘Out’ directory) because it’s already switched back to directory that holds VSTestHost.exe. There are two links to MSDN given where Microsoft admits this is a known bug and provides a hack to work around the problem – supply VSTestHost with copies of required artifacts (again, this is described in details in above mentioned post).

Unfortunately that didn’t work with my case. I’ve found the root cause though…

Continue reading ‘MSTest: Unit Test Adapter threw exception: Type is not resolved for member XXX’

C#: GetHashCode() might cause OverflowException

Microsoft recommends if you overload Equals method you should also overload GetHashCode. Now, how to properly implement GetHashCode? There are many resources on the web that describe it. A good starting point might be this article on Stack Overflow.

Following MSDN guidlines GetHashCode must fulfill these requirements:

  • If two objects of the same type represent the same value, the hash function must return the same constant value for either object.
  • For the best performance, a hash function must generate a random distribution for all input.
  • The hash function must return exactly the same value regardless of any changes that are made to the object.

Sticking to first bullet, you (probably?) should consider the same fields in Equals and GetHashCode methods. Let’s have a look at the example in which I did so:

public class Contact
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override bool Equals(object obj)
    {
        // If parameter is null return false.
        if (obj == null)
        {
            return false;
        }

        // If parameter cannot be cast to Contact return false.
        Contact c = obj as Contact;
        if c == null)
        {
            return false;
        }

        // Return true if the fields match:
        return ID == c.ID
            && FirstName == c.FirstName
            && LastName == c.LastName;;
    }

    public override int GetHashCode()
    {
        return ID.GetHashCode()
            + FirstName.GetHashCode()
            + LastName.GetHashCode();
    }
}

Now, what is wrong with this example of GetHashCode? There’s one drawback here. The hash is calculated as a sum of three integer values, which might give a value that is greater than int.MaxValue and that will result in OverflowException.

Continue reading ‘C#: GetHashCode() might cause OverflowException’

Comparison of .Net libraries for fetching emails via POP3

Sending emails in C# is easy; for basic use cases you don’t need external resources to send a note because .NET BCL already ships it. On .Net Developer Center, there’s a short description how to do it.

Now, how to fetch the email? It turns out it is not that easy – it’s not supported by .Net BCL. I spent a while researching for the best library that matched my purposes and I want to share my views on a couple of components I looked at.

Note: Please bear in mind I was interested only in a small piece of functionality such library could provide. My need was only to fetch an email (in plain text) with attachments. That was supposed to be done via POP3. I was not really interested in features like advanced sending emails (e.g. email templates), request and delivery receipts, support for iCalendar, email in HTML, etc. To sum up, I did not test libraries from that angle and therefore this comparison will not suit needs of all developers.

Continue reading ‘Comparison of .Net libraries for fetching emails via POP3′

Visual Studio: Common problems with VSMDI files

Visual Studio provides solid support for unit testing. One of the features are VSMDI files – test meta data file. The file is not much readable but Visual Studio comes with easy to use GUI for managing tests (grouping them in test lists, filtering, etc.).

All in all, VSMDI files are really helpful but…

  1. After a while there are several VSMDI files (MySolutionName1.vsmdi, MySolutionName2.vsmdi, MySolutionName3.vsmdi, …) in your project although only one is in use (and therefore added to source control). This is a known bug discovered in Visual Studio 2005. More information can be found there.
  2. Painful merging. Merging can be smooth or really painful. It is the latter with VSMDI. Sorry, with VSMDI there’s no such thing like merging. If you discover someone else has changed and checked in VSMDI file (conflict), just replace your local changes with server version and repeat your changes.

    The reason for the mess here is each test is given ID which is a GUID which tends to change once in a while.

  3. Not runnable tests. This doesn’t happen too often but I’ve experienced it several times already. When you try to run some tests you are told they are not runnable because there are multiple tests with the same ID (again, IDs…) – see below. Of course you haven’t played with IDs…
    notrunnable 350x83 Visual Studio: Common problems with VSMDI files

    At least this is an easy one (but not when you see that for the first time). Just refresh the whole test list view – select List of Tests in Test List Editor window and click refresh.

Ok, so that’s my list. Any points to add here?

Visual Studio Team System: Files are not checked out automatically when edited

It happened to me after connection to TFS was dropped. and I was moved to offline work mode. Default VSTS settings say whenever you start editing a file it will be automatically checked out and it will appear on Pending Changes window. However, after those connection problems I no longer experienced that behavior.

Solution

First of all it’s worth checking VSTS Source Control related settings according to this article. In my case everything was fine there.

The problem on my side was somewhere else. After I had gone offline I had to go online again (what a surprise?!). I was expecting that to happen after reconnecting to TFS, but that is not entirely true. What I had to do as well was use ‘Go online’ button at the top of solution explorer. Once pressed it listed all the files I had modified while in offline mode and VSTS started working as before.

An attempt was made to load a program with an incorrect format. Exception from HRESULT: 0x8007000B (BadImageFormatException)

Note: I assume you can rebuild the program you are having problems with because changes in its configuration settings are required.

If you are struggling with this problem you are probably running 64bit OS and executing 64bit exe that loads 32bit dll, or the other way – 32bit OS on which 32bit exe tries loading 64bit dll. For the sake of this post, let’s assume this is the former matter.

Solution

You need to assure that 32bit dll is loaded by the program with the same bittness, even if it’s running on 64bit platform.

In order to achieve that you need to change the configuration settings of the project whose outcome is that exe so that platform target is always x86, disregarding configuration platform. Let’s assume that program is written in C#.

Open project’s properties, go to Build tab and make change as below:

cs project settings 350x140 An attempt was made to load a program with an incorrect format. Exception from HRESULT: 0x8007000B (BadImageFormatException)