About Me

Tuesday, May 24, 2011

Castle Windsor error : "Could not convert from ‘Class.Name, Assembly.Name, Version=x.x.x.x, Culture=x’ to System.Type – Maybe type could not be found"

Today I got this exception during my work under some part of code. My code used Castle Windsor as IoC container.
I used to realize that most of errors that coming from the Castle Windsor project are not user friendly.
Its difficult to understand where is the error. Fortunately, every time when I'm having such errors I'm starting looking for the reason from the configuration section. In all my cases, the problem was in there.

In the situation when Castle Windsor returns such error, try to do what I did:

  • Open Castle Windsor configuration 
  • Find configuration of  "Class.Name" that was in the exception
  • Make sure that there is an assembly "Assembly.Name" specified in the configuration line. The assembly should be in the scope of the application (usually, the assembly DLL file should be in the same directory)
  • Make sure that "Class.Name" has correct namespace at the beginning. 
  • Make sure that the "Class.Name" is locating in the assembly that was specified in the configuration line. You can use Reflector to open the Dll file and double check that the "Class.Name" is really there.
As you can see, there is nothing complex, just to pay attention to the configuration.

Friday, May 13, 2011

How to start and stop windows service during building the Visual Studio solution's project.

I have a Visual Studio solution that contains few projects. One of them is a windows service.
You may know how it might be frustrating to turn it off and during each build of the solution.
Today i had a lot of small fixes in the service. Each of them requires to be tested. So my service had to be turned off and on every time. Somewhere in the middle, I lost my patience and decided to make my life easier.

As you may know, each Visual Studio project file you are working on is actually a MSBuild script.
MSBuild is a great script language that can help you to automate things you usually do to compile and build your project. Also it can do a lot of other things. You can read a MSBuild reference about it.
Each time when you building the project, it makes Visual Studio to execute the script.
So, in our case, the obvious solution is to specify a commands that stops the service then makes a build and then starts it again. That commands should be added into YourProject.csproj file.

Unfortunately default MSBuild package of commands does not have one for managing services. But, there is a great library http://msbuildtasks.tigris.org that can do that. As you may see, there is a "ServiceController" task in the list of available commands in the list. That one the command we are going to use.
Download the MSBuild.Community.Tasks.msi package from the "Download the latest release section" of the same page and install it to your computer. The installer will create a directory with the library that contains the task we need - MSBuild.Community.Tasks.dll

The next step is to find the place where we can inject that commands. As I told above, that should be YourProject.csproj file. Open it with Notepad and find there two targets - BeforeBuild and AfterBuild


As you see,  they are empty and commented out.
You need to uncomment it and add calling of "ServiceController" commands:


<target name="BeforeBuild">
    <servicecontroller action="Stop" servicename="ServiceName"/>
</target>
<target name="AfterBuild">
    <servicecontroller action="Start" servicename="ServiceName"/>
</target>

If we try to build our project now, we may get an exception. That's because Visual Studio's MSBuild process does not know the command we specified. All we need to do is to add a reference to the MSBuild.Community.Tasks.dll library that  we downloaded and installed:

<usingtask assemblyfile="..\lib\MSBuild.Community.Tasks.dll" taskname="ServiceController"/>

You may add the "UsingTask" line right after project definition and specify a path to the library in the AssemblyFile attribute.
After that you can freely reload your project in the Visual Studio, compile your service and forget that horrible time when you had to stop and start after each build.