1

I am currently working on a console application which references a class in a WPF project, with hopes of launching the WPF Window when specific conditions are met.

I have a simple object I would like to call from the console application, and it looks like this:

namespace MyNamespace
{
    public class TestLoad
    {
        public string Property { get; set; }

        public TestLoad()
        {
            Property = "hey there";
        }
    }
}

In my console application, I am using this simple constructor to call to the object (I have a reference to the WPF executable):

using MyNamespace;
...
TestLoad myObject = new TestLoad();

This builds and compiles with no issues; however, at runtime, when I get to the function that contains the above line, it jumps of the function with this error:

System.TypeLoadException was unhandled by user code
  HResult=-2146233054
  Message=Could not load type 'MyNamespace.TestLoad' from assembly 'MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
  Source=MyProject
  TypeName=MyNamespace.TestLoad
  StackTrace:
       at MyProject.DiskInit.UploadNewDocument(String DrivePath)
       at MyProject.DiskInit.<>c__DisplayClass67_0.<CbFsWriteFile>b__0() in C:\<somepath>\DiskInit.cs:line 1130
       at System.Threading.Tasks.Task.InnerInvoke()
       at System.Threading.Tasks.Task.Execute()
  InnerException: 

I have checked that both projects are on the same .NET version (4.5) and run on the same architecture (x86). Even if the class is changed to a static reference, the same TypeLOadException error occurs. I have been looking into this and read that it is a potential bug with Visual Studio, essentially compiling something it should not since it will cause this runtime error. I have also read that it is a potential bug with referencing WPF projects, and the only way to resolve that is to change the project to output a dll. My only issue with that is that I seem to lose the ability to create a WPF window that can be launched externally from the executing application. Is this correct?

Has anyone encountered this type of issue before or know how to get around it? If not, is there a better alternative to using a WPF Window in this manner, which is essentially to load a (pretty) user input dialog when the console application's conditions are met? Thank you in advance for any feedback.

Community
  • 1
  • 1
Bobby Byrnes
  • 411
  • 5
  • 18
  • Have you tried to [enable fusion logging](http://stackoverflow.com/questions/255669/how-to-enable-assembly-bind-failure-logging-fusion-in-net) and examine the log? – alex.b Jun 24 '16 at 14:39
  • Yes, I viewed the log and the reference I am dealing with does not show up in the log. I'm playing around with different settings in it to see if I can get it to show up. – Bobby Byrnes Jun 24 '16 at 15:04
  • No mentions of `MyNamespace` assembly in the logs? That's strange. I think you should post whole project somewhere, e.g. in github - hard to say something w/o even logs – alex.b Jun 24 '16 at 15:14
  • This is essentially the whole project though. It's just a new WPF application project with this referenced class. – Bobby Byrnes Jun 27 '16 at 13:17

1 Answers1

1

You need to add references to the following dlls in your console project

PresentationCore

PresentationFramework

WindowsBase

Once you've done that you can call a WPF window from your console app like below:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Application app = new Application();
        app.Run(new MainWindow());         
    }
}
cvraman
  • 1,687
  • 1
  • 12
  • 18