【问题标题】:Autofac with WCF4 Self Hosted Service (no .svc)带有 WCF4 自托管服务的 Autofac(无 .svc)
【发布时间】:2012-08-08 03:08:09
【问题描述】:

对于没有.svc的自托管服务,是否需要指定服务实现类型和宿主工厂?当我尝试运行下面的控制台应用程序时,我收到一个错误,即没有默认构造函数,所以似乎我的容器注册没有被使用。我错过了什么?

var builder = new ContainerBuilder();
builder.Register(c => new GenericRepository()).As<IRepository>();
builder.Register(c => new BusinesLogic(c.Resolve<IRepository>())).As<IBusinesLogic>();
builder.Register(c => new MyService(c.Resolve<IBusinesLogic>())).As<IMyService>();

using (IContainer container = builder.Build())
{
    var address = new Uri("net.tcp://localhost:8523/MyService");
    var host = new ServiceHost(typeof(MyService), address);

    host.AddServiceEndpoint(typeof(IMyService), new NetTcpBinding(), string.Empty);
    host.AddDependencyInjectionBehavior<IMyService>(container);
    host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = false });
    host.Open();

    Console.WriteLine("Navigate to the following URI to see the service.");
    Console.WriteLine(address);
    Console.WriteLine("Press enter to exit...");
    Console.ReadLine();

    host.Close();
    Environment.Exit(0);
}

【问题讨论】:

    标签: wcf dependency-injection autofac


    【解决方案1】:

    我想我已经弄清楚了我在 Alex Meyer-Gleaves 的博客文章中遗漏了什么。我需要调用 ComponentRegistry.TryGetRegistration

    https://alexmg.com/posts/self-hosting-wcf-services-with-the-autofac-wcf-integration

    这是我的更新代码:

    var builder = new ContainerBuilder();
    builder.Register(c => new GenericRepository()).As<IRepository>();
    builder.Register(c => new BusinessLogic(c.Resolve<IRepository>())).As<IBusinessLogic>();
    builder.Register(c => new MyService(c.Resolve<IBusinessLogic>())).As<IMyService>();
    
    using (IContainer container = builder.Build())
    {
        var address = new Uri("net.tcp://localhost:8523/MyService");
        var host = new ServiceHost(typeof(MyService), address);
    
        host.AddServiceEndpoint(typeof(IMyService), new NetTcpBinding(), string.Empty);
    
        IComponentRegistration registration;
        if (!container.ComponentRegistry.TryGetRegistration(new TypedService(typeof(IMyService)), out registration))
        {
            Console.WriteLine("The service contract has not been registered in the container.");
            Console.ReadLine();
            Environment.Exit(-1);
        }
    
        host.Description.Behaviors.Add(new AutofacDependencyInjectionServiceBehavior(container, typeof(MyService), registration));
        host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = false });
        host.Open();
        Console.WriteLine("Navigate to the following URI to see the service.");
        Console.WriteLine(address);
        Console.WriteLine("Press enter to exit...");
        Console.ReadLine();
    
        host.Close();
        Environment.Exit(0);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      相关资源
      最近更新 更多