我正在尝试构建路由基础设施,并使用 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 的构造函数(它的构造函数是私有的)。
你有什么提示吗?