我正在尝试构建路由基础结构,并使用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的condtructor(它的构造函数是私有的)。
你有任何提示吗?
答案 0 :(得分:0)
据我所知,RoutingService类没有定义构造函数。你可以看到,如果你试图这样做:
RoutingService rs = new RoutingService();
编译器会收到错误消息: System.ServiceModel.Routing.RoutingService类型没有定义构造函数。