【问题标题】:Autofac and RoutingServiceAutofac 和 RoutingService
【发布时间】:2011-11-10 23:48:55
【问题描述】:

我正在尝试构建路由基础架构,并使用 Autofac 作为 IoC 容器。我阅读了wiki,我知道这些步骤:

ContainerBuilder builder = new ContainerBuilder();
builder.Register(c => new Logger()).As<ILogger>();
builder.Register(c => new EchoService(c.Resolve<ILogger>())).As<IEchoService>();

using (IContainer container = builder.Build())
{
    Uri address = new Uri("http://localhost:8080/EchoService");
    ServiceHost host = new ServiceHost(typeof(EchoService), address);

host.AddServiceEndpoint(typeof(IEchoService), new BasicHttpBinding(), string.Empty);

host.AddDependencyInjectionBehavior<IEchoService>(container);

host.Description.Behaviors.Add(new ServiceMetadataBehavior {HttpGetEnabled = true, HttpGetUrl = address});
host.Open();

Console.WriteLine("The host has been opened.");
Console.ReadLine();

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

}

我这里确实有这个代码来满足我的场景:

builder.RegisterType<RoutingService>().As<ISimplexDatagramRouter>().InstancePerLifetimeScope();

        builder.Register(c =>
        {
            var routingConfiguration = new RoutingConfiguration();
            routingConfiguration.RouteOnHeadersOnly = false;
            return routingConfiguration;
        }).As<RoutingConfiguration>();

        builder.Register(c =>
            {
                var publisherServiceHost = new ServiceHost(typeof(RoutingService));
                publisherServiceHost.AddServiceEndpoint(typeof(ISimplexDatagramRouter), new NetTcpBinding(), "some address");
                publisherServiceHost.Description.Behaviors.Add(new RoutingBehavior(c.Resolve<RoutingConfiguration>()));
                return publisherServiceHost;
            }).As<ServiceHost>();

这不起作用,因为我从 Autofac 收到一个错误,因为它找不到 RoutingService 的构造函数(它的构造函数是私有的)。

你有什么提示吗?

【问题讨论】:

  • 使构造函数public.
  • 不能那样做,RoutingService 是 WCF Routing 的一部分...

标签: c# wcf autofac wcf-routing


【解决方案1】:

据我所知,RoutingService 类没有定义构造函数。如果您尝试这样做,您会看到:

RoutingService rs = new RoutingService();

你会从编译器那里得到一个错误,说: System.ServiceModel.Routing.RoutingService 类型没有定义构造函数。

【讨论】:

  • 也许您可以围绕 RoutingService 类创建一个包装器,以便能够在 Autofac 中使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多