Autofac and RoutingService

本文关键字:RoutingService and Autofac | 更新日期: 2023-09-27 18:13:59

我正在尝试构建一个路由基础设施,我使用Autofac作为IoC容器。我读了维基百科,我知道这些步骤:

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>();

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

你有什么提示吗?

Autofac and RoutingService

据我所知,RoutingService类没有定义构造函数。你可以看到,如果你试着这样做:

RoutingService rs = new RoutingService();

你会从编译器得到一个错误:type System.ServiceModel.Routing.RoutingService没有定义构造函数