none
Invoke console application from WCF RRS feed

  • Question

  • Hi,

    I came across a scenario to invoke a console application that is located in a remote server and call should be made from WCF service.How to achieve this?Any code sample to invoke would be helpful

    Monday, April 7, 2014 9:52 AM

Answers

  • The Process.Start method calls external applications. It allows you execute EXE programs and command line utilities from a C# application. Here is the code snippet.

    using System.Diagnostics;
    
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "C:\\Some.EXE";
    Process.Start(startInfo);
    
    Note that, Process object only allows access to remote processes not the ability to start or stop remote processes/program. So for your scenario you need to build a WCF service with above code and deploy that on the same computer where your external program exists.

    You can consider two alternative approaches as well.

    You can use PsExec a project acquired by Microsoft from http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

    Use WMI. Sample code snippet as below.

    object theProcessToRun() = { "YourFileHere" };
    ManagementClass theClass = new ManagementClass(@"\\server\root\cimv2:Win32_Process");
    theClass.InvokeMethod("Create", theProcessToRun);

    Lingaraj Mishra

    Tuesday, April 8, 2014 7:20 AM
  • Hi Parthasarathy,

    As far as I know, a WCF service cannot make a call spontaneously, but you can implement a duplex service with WCF, a duplex contract allows clients and servers to communicate with each other independently so that either can initiate calls to the other. Here are some reference for your information.

    #How to: Create a Duplex Contract

    http://msdn.microsoft.com/en-us/library/ms731184(v=vs.110).aspx

    #Duplex Serviceshttp://msdn.microsoft.com/en-us/library/ms731064(v=vs.110).aspx

    In addition, you can take a look at this post which includes a simple sample of calling WCF from another machine.

    Best Regards


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 8, 2014 3:27 AM
    Moderator

All replies

  • Hi Parthasarathy,

    As far as I know, a WCF service cannot make a call spontaneously, but you can implement a duplex service with WCF, a duplex contract allows clients and servers to communicate with each other independently so that either can initiate calls to the other. Here are some reference for your information.

    #How to: Create a Duplex Contract

    http://msdn.microsoft.com/en-us/library/ms731184(v=vs.110).aspx

    #Duplex Serviceshttp://msdn.microsoft.com/en-us/library/ms731064(v=vs.110).aspx

    In addition, you can take a look at this post which includes a simple sample of calling WCF from another machine.

    Best Regards


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 8, 2014 3:27 AM
    Moderator
  • The Process.Start method calls external applications. It allows you execute EXE programs and command line utilities from a C# application. Here is the code snippet.

    using System.Diagnostics;
    
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "C:\\Some.EXE";
    Process.Start(startInfo);
    
    Note that, Process object only allows access to remote processes not the ability to start or stop remote processes/program. So for your scenario you need to build a WCF service with above code and deploy that on the same computer where your external program exists.

    You can consider two alternative approaches as well.

    You can use PsExec a project acquired by Microsoft from http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

    Use WMI. Sample code snippet as below.

    object theProcessToRun() = { "YourFileHere" };
    ManagementClass theClass = new ManagementClass(@"\\server\root\cimv2:Win32_Process");
    theClass.InvokeMethod("Create", theProcessToRun);

    Lingaraj Mishra

    Tuesday, April 8, 2014 7:20 AM