从另一个.NET 4.0(C#)应用程序启动时,.NET 4.0应用程序引发ApplicationException

从另一个.NET 4.0(C#)应用程序启动时,.NET 4.0应用程序引发ApplicationException,c#,.net-4.0,mutex,process.start,C#,.net 4.0,Mutex,Process.start,为什么使用System.Diagnostics.Process.Start(“ThirdPartyApp.exe”)从命令shell中启动的第三方应用程序与从C#应用程序中启动的第三方应用程序的行为有所不同 是否有一种方法可以在C#中启动应用程序,使其行为与从命令shell启动时完全相同 详细信息: 我的电脑上安装并运行了第三方.NET 4.0应用程序(没有可用的源代码)。它附带了在IIS中运行的web服务。我编写了一个C#应用程序,它使用SOAP消息调用这些web服务。(两个应用程序都安装在同

为什么使用
System.Diagnostics.Process.Start(“ThirdPartyApp.exe”)从命令shell中启动的第三方应用程序与从C#应用程序中启动的第三方应用程序的行为有所不同

  • 是否有一种方法可以在C#中启动应用程序,使其行为与从命令shell启动时完全相同

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

    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
    时,第三方应用程序引发以下异常:

    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()
    
    如果在我启动应用程序之前第三方应用程序已经在运行,我可以调用
    SomeWebService
    很好,因此我发现很难相信缺少依赖项;但是为了搞笑这个第三方应用程序,我将丢失的文件复制到指定的文件夹中,看看这是否能解决问题。关于缺少依赖项的第一条错误消息随后更改为InvalidCastException,但第二条错误消息(
    System.ApplicationException:对象同步方法是从未同步的代码块调用的。
    )仍然显示并保持不变


    如果您能帮助我理解我做错了什么,以及我如何让这个第三方应用程序保持一致,无论它是从C#应用程序内部还是外部启动的,我们将不胜感激

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


    请把
    无效castexception

    的内容贴出来,你完全正确。显然,我的第三方应用程序在很大程度上取决于启动时的工作目录。在ProcessStartInfo中设置此参数解决了我的问题。谢谢太棒了,很高兴能帮到你!:)