.NET 4.0 application throws ApplicationException when started from another .NET 4.0 (C#) application

StackOverflow https://stackoverflow.com/questions/14949282

Question

  1. Why does a 3rd-party application behave differently if it is launched from within a command shell than it does when launched from within a C# application using System.Diagnostics.Process.Start("ThirdPartyApp.exe");?

  2. Is there a way to launch an application within C# so that it behaves exactly like when it is launched from the command shell?

Details: I have a 3rd-party .NET 4.0 application (no source code available) installed and running on my PC. It comes with web services running in IIS. I have written a C# application that uses SOAP messages to call these web services. (Both applications are installed and running on the same PC.) If the 3rd-party app is running prior to launching my app, I'm able to communicate with it without a problem. If the 3rd-party app is not running prior to launching my app, I want to be able to start it. I tried the following code:

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

If I then try to access the web service through my SOAP client:

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

on the call to SomeWebService, the 3rd-party app throws the following exception:

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()

I am able to call SomeWebService just fine if the 3rd-party application was already running before I started my application, so I find it hard to believe there are missing dependencies; but to humor this 3rd-party app, I copied the missing files into the specified folder to see if that would fix the problem. The first error message about a missing dependency then changes to an InvalidCastException, but the second error message (System.ApplicationException: Object synchronization method was called from an unsynchronized block of code.) still appears and remains unchanged.

Any help on understanding what I'm doing wrong, and how I can get this 3rd-party app to behave the same regardless of whether it is launched from inside a C# app or outside, will be greatly appreciated!

Was it helpful?

Solution

It may have something to do with the user context your application is running in, for example if you run your app as an administrator Process.Start will attempt to start the process in the same context.

And please, post the contents of InvalidCastException

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top