简体   繁体   English

Autofac和RoutingService

[英]Autofac and RoutingService

I am trying to build a routing infrastructure and I use Autofac as IoC container. 我正在尝试构建路由基础结构,并且将Autofac用作IoC容器。 I read the wiki and I know these steps: 我阅读了维基,并且知道以下步骤:

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

} }

I do have this code here to satisfy my scenario: 我在这里确实有以下代码可以满足我的情况:

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

This doesn't work, as I get an error from Autofac as it can't find condtructor for RoutingService (its constructor is private). 这是行不通的,因为我从Autofac收到一个错误,因为它找不到用于RoutingService的构造器(其构造函数是私有的)。

Do you have any hint? 你有什么提示吗?

As far as I know the RoutingService class has no constructors defined. 据我所知,RoutingService类没有定义构造函数。 You can see that if you try to do this: 您可以看到,如果尝试这样做:

RoutingService rs = new RoutingService(); RoutingService rs = new RoutingService();

You will get an error from the compiler saying: The type System.ServiceModel.Routing.RoutingService has no constructors defined. 您将从编译器中收到一条错误消息:类型System.ServiceModel.Routing.RoutingService没有定义构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM