2

I need help. I create Windows Service with Autofac container. And I use MEF Integration service for create several alternative components for my service.

For example:

Module 1

  [Export(typeof(IClass1))]
  public class Class1 : IClass1
  {
        public void Show()
        {
              Console.WriteLine("Hallo from Class1");
        }
  }

Module 2

  [Export(typeof(IClass2))]
  public class Class2 : IClass2
  {
        public void Show()
        {
              Console.WriteLine("Hallo from Class2");
        }
  }

Basic class for modules integration - example

  class Program
  {
        private static IContainer Container { get; set; }

        static void Main(string[] args)
        {

              // Create your builder.
              var builder = new ContainerBuilder();

              /** find all modules in selected folder */
              var catalog = new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\modules", "*Module.dll");

              /** register finded modules */
              builder.RegisterComposablePartCatalog(catalog);


              builder.RegisterType<MyClass>().As<IMyClass>().SingleInstance();

              Container = builder.Build();

              var cls = Container.Resolve<IMyClass>();

              cls.Show();

              Console.WriteLine("Class ready. Press Enter");
              Console.ReadKey(true);
        }
  }


  class MyClass: IMyClass
  {
        private readonly IClass1 _class1;
        private readonly IClass1 _class3;


        private readonly IClass2 _class2;
        private readonly IClass2 _class4;

        public MyClass(IClass1 class1, IClass2 class2)
        {
              _class1 = class1;
              _class2 = class2;

              _class3 = class1;
              _class4 = class2;
        }

        public void Show()
        {
              _class1.Show();

              Console.WriteLine("Class1 ready. Press Enter");
              Console.ReadKey(true);

              _class2.Show();

              Console.WriteLine("Class1 ready. Press Enter");
              Console.ReadKey(true);
        }
  }

  internal interface IMyClass
  {
        void Show();
  }

In this example all work fine.This principle I use in my service. For test start and debug my service I use Service.Helper from Nuget packages repository. Everithyng work fine too. But. If i create install package in Advance installer and install my service in system (Windows 8.1 x64) service do not start. Logging exception from service write System.ArgumentNullException in system Event log. Exception most likely in this line

builder.RegisterComposablePartCatalog(catalog);

Service do not load any modules from start folder. Access denied from service to his subfolder. Help please. Thanks.

1 Answers1

0

Try Assembly.GetEntryAssembly().Location insted of Assembly.GetExecutingAssembly().Location

Daniele
  • 191
  • 1
  • 4