0
  1. 为什么第 3 方应用程序从命令行启动时的行为与使用 C# 应用程序启动时的行为不同System.Diagnostics.Process.Start("ThirdPartyApp.exe");

  2. 有没有办法在 C# 中启动应用程序,使其行为与从命令 shell 启动时完全一样?

详细信息: 我的 PC 上安装并运行了第 3 方 .NET 4.0 应用程序(没有可用的源代码)。它带有在 IIS 中运行的 Web 服务。我编写了一个使用 SOAP 消息调用这些 Web 服务的 C# 应用程序。(这两个应用程序都安装并在同一台 PC 上运行。)如果在启动我的应用程序之前运行第 3 方应用程序,我可以毫无问题地与之通信。如果在启动我的应用程序之前第 3 方应用程序没有运行,我希望能够启动它。我尝试了以下代码:

if (!System.Diagnostics.Process.GetProcesses().Select(rec => rec.ProcessName).Contains("ThirdPartyApp"))
{
    System.Diagnostics.Process.Start("ThirdPartyApp.exe");
}

如果我尝试通过我的 SOAP 客户端访问 Web 服务:

using (var soapClient = new ThirdPartyAppSoapClient())
{
    soapClient.SomeWebService();
}

在调用 时SomeWebService,第 3 方应用程序抛出以下异常:

2013-02-18 19:43:33,884 [17] ERROR ThirdPartyApp.Manager Error Exception Caught
System.IO.FileNotFoundException: Could not load file or assembly 'file:///C:\Pro
gram Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\ThirdPartyDependen
cy.dll' or one of its dependencies. The system cannot find the file specified.
File name: 'file:///C:\Program Files (x86)\Common Files\Microsoft Shared\DevServ
er\10.0\ThirdPartyDependency.dll'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String cod
eBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark&
stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppre
ssSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName as
semblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntr
ospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Ev
idence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm,
Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackM
ark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at ...

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\M
icrosoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure lo
gging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fus
ion!EnableLog].


Unhandled Exception: System.ApplicationException: Object synchronization method
was called from an unsynchronized block of code.
   at System.Threading.Mutex.ReleaseMutex()
   at ...
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

如果在我启动我的应用程序之前 3rd 方应用程序已经在运行,我可以正常调用SomeWebService,所以我很难相信缺少依赖项;但为了幽默这个第 3 方应用程序,我将丢失的文件复制到指定的文件夹中,看看是否能解决问题。关于缺少依赖项的第一条错误消息随后更改为 InvalidCastException,但第二条错误消息 ( System.ApplicationException: Object synchronization method was called from an unsynchronized block of code.) 仍然出现并且保持不变。

任何有助于理解我做错了什么,以及如何让这个第 3 方应用程序无论是从 C# 应用程序内部还是外部启动都表现相同,将不胜感激!

4

1 回答 1

2

它可能与您的应用程序在其中运行的用户上下文有关,例如,如果您以管理员身份运行您的应用程序,Process.Start将尝试在相同的上下文中启动该进程。

请发布以下内容InvalidCastException

于 2013-02-19T11:25:25.983 回答