0

I have two projects in a Visual Studio solution where one is a console app and the other is a WPF app. Both have a composite root where a common bootstrapper is instantiated in each and registers some common types.

I'm trying to get the console app to subscribe to some events published by the WPF app. When a WPF window is moved, opened, or resized, the console app prints a string on the console.

The Bootstrapper class is in a separate class library with the following code:

public class Bootstrapper
{
    public void Initialize()
    {           
        var container = IocContainer.Instance.Container;

        container.Register<IOrderRepository, SqlOrderRepository>(Lifestyle.Singleton);
        container.Register<ILogger, FileLogger>(Lifestyle.Singleton);
        container.Register<IEventPublisher, EventPublisher>(Lifestyle.Singleton);
        container.Register<CancelOrderHandler>();
        container.Register<IViewsIntegrationService, ViewsIntegrationService>(
            Lifestyle.Singleton);

        try
        {
            // Verify the container
            container.Verify();
        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex.Message);
            System.Console.ReadLine();
        }
    }
}

Both apps have this code in the composite root:

 var bs = new Bootstrapper();
 bs.Initialize();

The ViewsIntegrationService fires the events in the WPF app. The Console app subscribes to the ViewsIntegrationService events.

When I run both applications, the console app does not receive the events and nothing prints on the console.

Is it because each of the apps use separate instances of the bootstrapper? Hence two sets of identical registrations? Or is this not working because of another reason?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Ray
  • 4,679
  • 10
  • 46
  • 92
  • Both applications run in their own isolated Windows process. If you need them to communicate, you need to use some shared out of process state (such as a database or a message queue ) that thry both can read from and write to. – Steven Jun 16 '17 at 03:22

1 Answers1

2

Despite being 2 projects in the same Visual Studio solution, when compiled each one is an individual Windows executable program. These two programs will not automatically communicate with each other. If you need to transfer information from one program to the other, you will need to use some sort of communication channel such as TCP or MSMQ. See Send/Receive message To/From two running application.

Alternatively, you could make your "console application" into a class library (which when referenced from WPF will be part of the same application) and use a WPF window that looks like a console application window write out information.

Events and dependency injection only affect a single application, so neither are relevant in the case you are communicating across application boundaries.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Would I be able to create a class library shared by both these apps, and use the shared assembly as an intermediate communicator? I'm new to c# assemblies so I'm not sure. – Ray Jun 17 '17 at 00:22
  • No. Each executable (regardless of how many assemblies it references) is a separate Windows process. There is no way to make Windows processes communicate by sharing assemblies, as each DLL just becomes a part of the executable process. – NightOwl888 Jun 17 '17 at 01:57