Summary
Goal
I have two C# projects. The output of the first project is a DLL (output type in Visual Studio is set to Class Library) that lets me control my application from other languages. It basically starts an instance of my application without showing any of its UI windows. I can use this DLL in other programming languages (like Python, Matlab) without any problems.
But now I want to use that DLL in another C# project. To do this, I have added the DLL as a reference in my project. To initiate an instance of my application, I create an object of the type that is defined in the DLL:
namespace MyDLL
{
class Program
{
static void Main(string[] args)
{
MyApp app = new MyApp();
}
}
}
This results in an exception:
System.InvalidOperationException: 'The 'ResourceAssembly' property of the 'Application' type cannot be changed after it has been set.'
The cause of this exception is the fact that MyApp
sets the ResourceAssembly
in order to load the application's resources (things like UI images):
System.Windows.Application.ResourceAssembly = typeof(AppBase.MainWindow).Assembly;
What I've tried
Since Application.ResourceAssembly
can only be set once, I tried a way around it as described in the second solution of this StackOverflow post. This prevents the ResourceAssembly exception from being thrown.
The DLL object the attempts to start but then throws the following exception:
System.InvalidOperationException: 'The Application object is being shut down.'
The InnerException value of this exception is null
, but its source value is PresentationFramework
. This is the StackTrace of the exception:
at System.Windows.Application.GetResourcePackage(Uri packageUri)
at System.Windows.Application.GetResourceOrContentPart(Uri uri)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at App.MainWindow..ctor(Boolean showWindow)
at MyDLL.MyApp.startApp()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()