четверг, 28 февраля 2013 г.

Microsoft.WebApplication.targets was not found


Based on the post here http://lucbei.wordpress.com/2011/12/04/new-build-server-microsoft-webapplication-targets-was-not-found/ you can simply download the "Microsoft Visual Studio 2010 Shell (Integrated) Redistributable Package" from http://www.microsoft.com/download/en/confirmation.aspx?id=115 and the targets are installed.
This avoids the need to install Visual Studio on the build server.
I have just tried this out now, and can verify that it works:
Before:
error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.
After the install:
[Builds correctly]
This is a far better solution than installing Visual Studio on a build server, obviously.

вторник, 26 февраля 2013 г.

How To Run MongoDB As Windows Service


1. mongod –help

Get to know all the Windows service related commands by typing “mongod --help“.
C:\MongoDB\bin>mongod --help
 
Windows Service Control Manager options:
  --install                install mongodb service
  --remove              remove mongodb service
  --reinstall             reinstall mongodb service (equivilant of mongod
                             --remove followed by mongod --install)
  --serviceName arg           windows service name
  --serviceDisplayName arg windows service display name
  --serviceDescription arg    windows service description
  --serviceUser arg              user name service executes as
  --servicePassword arg       password used to authenticate serviceUser
Two “--install” and “--remove” arguments are what you need.

2. Install as Windows Service

To install as Windows service, issue “mongod --install“, for example :
#> mongod --dbpath "c:\mymongodb" --logpath "c:\mymongodb\logs.txt" --install --serviceName "MongoDB"
all output going to: c:\mymongodb\logs.txt
Creating service MongoDB.
Service creation successful.
Service can be started from the command line via 'net start "MongoDB"'.
It means, install a MongoDB, which point to “c:\mymongodb” data directory, log outputs to “c:\mymongodb\logs.txt“, and a Windows service named “MongoDB“.
Figure : MongoDB is installed as Windows Service
mongodb as windows service

3. Uninstall It

To uninstall above installed MongoDB service, issue “mongod --remove“, along with the installed service name.
#> mongod --remove --serviceName "MongoDB"
Deleting service MongoDB.
Service deleted successfully.
Fri Apr 29 18:39:06 dbexit:
Fri Apr 29 18:39:06 shutdown: going to close listening sockets...
Fri Apr 29 18:39:06 shutdown: going to flush diaglog...
Fri Apr 29 18:39:06 shutdown: going to close sockets...
Fri Apr 29 18:39:06 shutdown: waiting for fs preallocator...
Fri Apr 29 18:39:06 shutdown: closing all files...
Fri Apr 29 18:39:06 closeAllFiles() finished
Fri Apr 29 18:39:06 dbexit: really exiting now
Done.

суббота, 9 февраля 2013 г.

Creating generic type object dynamically using reflection



Type type = typeof(MessageProcessor<>).MakeGenericType(key);
That's the best you can do, however without actually knowing what type it is, there's really not much more you can do with it.
EDIT: I should clarify. I changed from var type to Type type. My point is, now you can do something like this:
object obj = Activator.CreateInstance(type);
obj will now be the correct type, but since you don't know what type "key" is at compile time, there's no way to cast it and do anything useful with it.

Invoking Generic Methods Using Reflection


This is by no means any detailed post on how to use reflection. The other day I had the need to use reflection to call a generic method. So after playing around with reflection for few minutes I figured it out.  Here is how the code looks like.

namespace ConsoleApplication7
{
    class Printing
    {
        public void PrintAnyThing(T print)
        {
            Console.WriteLine(print);
        }
    }
 
class Program
{
    static void Main(string[] args)
    {
        Type type = typeof(Printing);
        Object instance = Activator.CreateInstance(type);
        MethodInfo genericmethod = type.GetMethod("PrintAnyThing");
        MethodInfo closedmethod = genericmethod.MakeGenericMethod(typeof(string));
        closedmethod.Invoke(instance, new object[]{"Zeeshan"});
 
    }
}
}
Here is how the generic method looks like.
image
Here is how you would invoke it.
image
The generic method is pretty simple. I am taking a parameter of type T which makes it a generic method. I simply print that generic parameter value to the console as the method name states.
In the order to call the generic method, I first create an instance of Printing class by calling Activator.CreateInstance passing in type of object I want to instantiate. The reason I am creating an instance of Printing type is because PrintAnyThing generic method is an instance method. If the method would have been static I do not need to create an instance of it. Next from the type, I try to get method info for PrintAnyThing. Notice at this point it is still a generic method and I cannot invoke it directly. I need to convert it to closed method by specifying the value for T which I do by making a call to MakeGenericMethod. Once I have the closed method, I pass in the instance on which this method is to be invoked along with the method arguments that the method requires.