Call your Win Service from your Web Application

Some quick code snippets from my brother.  If you have a running Windows Service, you can call it from your Web Application to run a custom command. UPDATE: There’s some better VB Sample Code here and quick and easy of both the Windows Service and the ASP.Net

So you have your Win Service named “Encoder” and when you say, upload a video, you then call the Service and send it an argument value of 10.  Then in your web service when it see’s the value 10 come in, it knows to start encoding any videos uploaded. 

Here’s some sample code:

Web application must have full trust. Partial trust not allowed.

In your application: (add reference to System.ServiceProcess.dll)

System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController(“serviceName”)controller.ExecuteCommand(commandinteger);

In your Win service here is your code:

protected override void OnCustomCommand(int commandinteger){

switch(commandinteger){
case 129:
//do something
break;
case 130:
//do something else
break;
}

}

commandinteger values must be between 128 and 256.

128 and below are system reserved.

Check the link below for msdn description
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.oncustomcommand.aspx

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.oncustomcommand(VS.80).aspx

Found this blog post with more info, cause I’m lazy :p

http://blogs.msdn.com/kaevans/archive/2005/03/17/397505.aspx

Updated Blog Post On This Topic

.Net – Improve garbage collection and performance

SQL 2008 Enabling Change Data Capture Made Easy