从另一个.exe调用.exe以运行Web服务 [英] calling .exe from another .exe to run a webservice

查看:90
本文介绍了从另一个.exe调用.exe以运行Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了几种类型的示例,从另一个带有参数的.exe文件中调用一个.exe文件,其参数为

运行 Web服务,但有时会得到 500 –内部服务器错误异常。

i have tried with several type of examples to call a .exe file from another .exe file with parameters to
run a 'web service',but i am getting some times '500 – Internal Server Error exception'.

1. code in First .Exe(code for only for one event, i have 8 event like this to run in Button click)    

    dateTimePicker4.CustomFormat = "yyyy-MM-dd";   
            string frodate = dateTimePicker4.Value.Date.ToShortDateString();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = @"C:\WebserviceClient.exe";           // this is the second .EXE file
            startInfo.Arguments = "300000 supplier" + " " + frodate;   // thesse are the 3 parameters
            Process.Start(startInfo);

 2. My second .Exe file receive these parameters and call the Web-Service like below

WebRequest webRequest = WebRequest.Create("http://My Server Path/epos/getproduct.asmx"); // this is web service in another location 
            HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
            httpRequest.Method = "POST";
            httpRequest.ContentType = "text/xml; charset=utf-8";
            httpRequest.Headers.Add("SOAPAction: http://tempuri.org/getCategory");
            httpRequest.ProtocolVersion = HttpVersion.Version11;
            httpRequest.Credentials = CredentialCache.DefaultCredentials;
            Stream requestStream = httpRequest.GetRequestStream();
            //Create Stream and Complete Request             
            StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);

        StringBuilder soapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
        soapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
        soapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
        soapRequest.Append("<getCategory xmlns=\"http://tempuri.org/\">");
        soapRequest.Append("<inBranch>" + strParam + "</inBranch>");
        soapRequest.Append("<dir>" + strParamDir + "</dir>");
        soapRequest.Append("<modifyDateFrom>" + strModifyDateFrom + "</modifyDateFrom>");
        soapRequest.Append("<modifyDateTo>" + strModifyDateTo + "</modifyDateTo>");
        soapRequest.Append("</getCategory>");

        soapRequest.Append("</soap:Body></soap:Envelope>");

        streamWriter.Write(soapRequest.ToString());
        streamWriter.Close();
        //Get the Response    
        HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();  // here i am getting the ERROR
        StreamReader srd = new StreamReader(wr.GetResponseStream());
        string resulXmlFromWebService = srd.ReadToEnd();

//注意:当我调用此服务时,我必须使用不同的参数运行该服务8次来自8个批处理文件的第二个.EXE文件,然后没有问题。

// NOTE-- i have to run this service 8 times with different parameters, when i call this second .EXE from 8 batch files one by one, then no issue.

现在我正在尝试从我的批处理文件的第一个.exe实例逐个运行此服务。在按钮单击事件中,那么当第一个事件完成第二个事件开始时,我得到500错误。

now i am trying to Run this service from my First .Exe instad of Batch file one by one in button click events , then i am getting the 500 error when first event complete second event start.

我做错了,请给我一些建议。

what i am doing wrong,Please give me some suggestions.

推荐答案

替换此行:

        startInfo.FileName = @"C:\WebserviceClient.exe";

具有以下行:

ThreadPool.QueueUserWorkItem(delegate { Process.Start("C:\\WebserviceClient.exe"); });

另一种方法如下:

    public static void TestCommands()
    {
        var command = "WebserviceClient.exe";
        ExecuteCommand(command, 5000);
        var command = "WebserviceClient2.exe";
        ExecuteCommand(command, 5000);
    }

    public static int ExecuteCommand(string command, int timeout)
    {
        var processInfo = new ProcessStartInfo(command)
        {
            CreateNoWindow = true,
            UseShellExecute = false,
            WorkingDirectory = @"C:\\",
        };

        var process = Process.Start(processInfo);
        process.WaitForExit(timeout);
        var exitCode = process.ExitCode;
        process.Close();
        return exitCode;
    } 

这篇关于从另一个.exe调用.exe以运行Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆