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

时间:2013-02-19 03:20:59

标签: c# .net-4.0 mutex process.start

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

  2. 有没有办法在C#中启动一个应用程序,以便它在完全时表现得像从命令shell启动时那样?

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

    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: Object synchronization method was called from an unsynchronized block of code.)仍然显示并保持不变。

    了解我做错了什么的帮助,以及我如何让这个第三方应用程序无论是从C#应用程序内部还是外部启动都行为相同,我们将不胜感激!

1 个答案:

答案 0 :(得分:2)

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

请发布InvalidCastException

的内容