使用C#运行进程,如何判断是否发生错误?

发布于 2024-11-26 13:54:43 字数 3945 浏览 3 评论 0原文

作为自动化 BizTalk 部署的一部分,我使用以下代码从 C# 运行流程。

        string commandArguments =
            "ImportApp /ApplicationName:\"" + ApplicationFullName +
            "\" /Package:\"" + MsiFilePath+ "\"";
        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.FileName = "BTSTask.exe";
        p.StartInfo.Arguments = commandArguments;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();
        StreamReader outputStream = p.StandardOutput;
        StreamReader errorStream = p.StandardError;
        // Read the standard output
        //string output = outputStream.ReadToEnd();
        string error = errorStream.ReadToEnd();
        //WriteToLogFile(output);
        if (!error.Equals(""))
        {
            WriteToLogFile(error);
            throw new Exception("Error occurred while importing application " + 
                ApplicationFullName +
                Environment.NewLine +
                error);
        }
        p.WaitForExit();
        p.Close();
        return;

我在日志文件中获得的该进程的输出如下所示:

Copyright (c) 2010 Microsoft Corporation. All rights reserved.
Information: Importing package "\\tc6218\BizTalkReceiveDrop\servername\Apps\Core.Artifacts.msi" into application "Core.Artifacts" in BizTalk configuration database (server="TC6218", database="BizTalkMgmtDb")...
Information: Performing action "Create" on host "ConfigurationDb" using package "\\ Apps\Core.Artifacts.msi".
Information: Validating resources (count=6)...
* Validating resource (-Type="System.BizTalk:Assembly" -Luid="TC.BizTalk.Functoid.AdvancedDatabaseLookup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f68c7d112162f09d")...
* Validating resource (-Type="System.BizTalk:Assembly" -Luid="TaylorCorp.Erp.ServiceBus.MasterData.Components.BusinessRules, Version=1.0.0.2, Culture=neutral, PublicKeyToken=f68c7d112162f09d")...
* Validating resource (-Type="System.BizTalk:BizTalkAssembly" -Luid="ICSM.Schemas, Version=4.0.1.0, Culture=neutral, PublicKeyToken=69c11018615fef04")...
* Validating resource (-Type="System.BizTalk:BizTalkAssembly" -Luid="ICSM.Pipelines, Version=4.0.1.0, Culture=neutral, PublicKeyToken=69c11018615fef04")...
* Validating resource (-Type="System.BizTalk:BizTalkAssembly" -Luid="ICSM.Maps, Version=2.0.0.0, Culture=neutral, PublicKeyToken=69c11018615fef04")...
* Validating resource (-Type="System.BizTalk:BizTalkBinding" -Luid="Application/Core.Artifacts")...
Information: Performing change requests...
Information: Calling BeginTypeChangeRequest for all selected resource types...
PerformingBeginChangeRequest
PerformingBeginChangeRequest
PerformingBeginChangeRequest
Adding resource (-Type="System.BizTalk:Assembly" -Luid="TC.BizTalk.Functoid.AdvancedDatabaseLookup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f68c7d112162f09d") to store.
Information: PerformingEndChangeRequestsWithRollBack
* Performing EndTypeChangeRequest for resource type "System.BizTalk:Assembly".
* Performing EndTypeChangeRequest for resource type "System.BizTalk:BizTalkAssembly".
* Performing EndTypeChangeRequest for resource type "System.BizTalk:BizTalkBinding".
Error: Resource (-Type="System.BizTalk:Assembly" -Luid="TC.BizTalk.Functoid.AdvancedDatabaseLookup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f68c7d112162f09d") already in store. 
1) Use BTSTask's overwrite flag or 
2) Set redeploy flag to true in BizTalk Project or 
3) Click overwrite all checkbox in Admin MMC  
to update if the resource exists in the specified target application "Core.Artifacts".
Overwrite flag will be ignored if the resource is associated with another application.


Command failed with 1 errors, 0 warnings.

现在,我需要做的就是查明进程是否出错。而且,我需要将输出流和错误流存储到字符串变量中。 (我不想解析这个字符串,你现在可能已经知道了。而且我不希望遇到微软所说的“死锁”问题,在这种情况下我不完全理解这些问题。

感谢您的帮助!

As part of automating BizTalk deployment, I'm using the following code to run process from C#.

        string commandArguments =
            "ImportApp /ApplicationName:\"" + ApplicationFullName +
            "\" /Package:\"" + MsiFilePath+ "\"";
        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.FileName = "BTSTask.exe";
        p.StartInfo.Arguments = commandArguments;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();
        StreamReader outputStream = p.StandardOutput;
        StreamReader errorStream = p.StandardError;
        // Read the standard output
        //string output = outputStream.ReadToEnd();
        string error = errorStream.ReadToEnd();
        //WriteToLogFile(output);
        if (!error.Equals(""))
        {
            WriteToLogFile(error);
            throw new Exception("Error occurred while importing application " + 
                ApplicationFullName +
                Environment.NewLine +
                error);
        }
        p.WaitForExit();
        p.Close();
        return;

The output of this process that I'm getting in the log file looks like this:

Copyright (c) 2010 Microsoft Corporation. All rights reserved.
Information: Importing package "\\tc6218\BizTalkReceiveDrop\servername\Apps\Core.Artifacts.msi" into application "Core.Artifacts" in BizTalk configuration database (server="TC6218", database="BizTalkMgmtDb")...
Information: Performing action "Create" on host "ConfigurationDb" using package "\\ Apps\Core.Artifacts.msi".
Information: Validating resources (count=6)...
* Validating resource (-Type="System.BizTalk:Assembly" -Luid="TC.BizTalk.Functoid.AdvancedDatabaseLookup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f68c7d112162f09d")...
* Validating resource (-Type="System.BizTalk:Assembly" -Luid="TaylorCorp.Erp.ServiceBus.MasterData.Components.BusinessRules, Version=1.0.0.2, Culture=neutral, PublicKeyToken=f68c7d112162f09d")...
* Validating resource (-Type="System.BizTalk:BizTalkAssembly" -Luid="ICSM.Schemas, Version=4.0.1.0, Culture=neutral, PublicKeyToken=69c11018615fef04")...
* Validating resource (-Type="System.BizTalk:BizTalkAssembly" -Luid="ICSM.Pipelines, Version=4.0.1.0, Culture=neutral, PublicKeyToken=69c11018615fef04")...
* Validating resource (-Type="System.BizTalk:BizTalkAssembly" -Luid="ICSM.Maps, Version=2.0.0.0, Culture=neutral, PublicKeyToken=69c11018615fef04")...
* Validating resource (-Type="System.BizTalk:BizTalkBinding" -Luid="Application/Core.Artifacts")...
Information: Performing change requests...
Information: Calling BeginTypeChangeRequest for all selected resource types...
PerformingBeginChangeRequest
PerformingBeginChangeRequest
PerformingBeginChangeRequest
Adding resource (-Type="System.BizTalk:Assembly" -Luid="TC.BizTalk.Functoid.AdvancedDatabaseLookup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f68c7d112162f09d") to store.
Information: PerformingEndChangeRequestsWithRollBack
* Performing EndTypeChangeRequest for resource type "System.BizTalk:Assembly".
* Performing EndTypeChangeRequest for resource type "System.BizTalk:BizTalkAssembly".
* Performing EndTypeChangeRequest for resource type "System.BizTalk:BizTalkBinding".
Error: Resource (-Type="System.BizTalk:Assembly" -Luid="TC.BizTalk.Functoid.AdvancedDatabaseLookup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f68c7d112162f09d") already in store. 
1) Use BTSTask's overwrite flag or 
2) Set redeploy flag to true in BizTalk Project or 
3) Click overwrite all checkbox in Admin MMC  
to update if the resource exists in the specified target application "Core.Artifacts".
Overwrite flag will be ignored if the resource is associated with another application.


Command failed with 1 errors, 0 warnings.

Now, all I need to do is find out whether a process errored or not. And, I need to store both the output stream as well as the error stream into string variables. ( I don't want to parse this string , you probably know this by now. And I don't wish to encounter problems stated by Microsoft as "Deadlocks" which I don't understand completely in this scenario.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌面超妹 2024-12-03 13:54:43

根据:
http://msdn.microsoft.com/en- us/library/aa559686(v=bts.20).aspx

如果成功,BTTSTask.exe 进程将返回 0;如果出现问题,则返回非零。此返回代码将显示在您的 p.ExitCode

According to:
http://msdn.microsoft.com/en-us/library/aa559686(v=bts.20).aspx

The BTSTask.exe process returns 0 if success and non-zero if there was an issue. This return code will show up in your p.ExitCode

任谁 2024-12-03 13:54:43

想到 3 个解决方案:

  1. 订阅 Process.ErrorDataRecieved 事件。
  2. 从进程中获取返回错误代码。通常如果它不为 0,则表明存在错误。
  3. 通过分析您的“关键字”,分析您的进程是否在 StandartOutput 上报告错误并读取此字符串。

不幸的是,使用单独的流程会“失去”控制,因此必须根据项目架构期间的决定在可靠的架构和容错之间进行权衡。

希望这有帮助。

问候。

Comes in mind 3 solutions:

  1. Subscribe to Process.ErrorDataRecieved event.
  2. Get return error code from the process. Usually if it's not 0, it's a signal about an error.
  3. Analyze if your process reports a error on StandartOutput and read this string, by analyzing your "keywords".

Unfortunately using separate process you "loose" control, so have to laverage between reliable architecture and fault tollerance based on desicions during the architecturing of your project.

Hope this helps.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文