简体   繁体   中英

Autofac and RoutingService

I am trying to build a routing infrastructure and I use Autofac as IoC container. 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).

Do you have any hint?

As far as I know the RoutingService class has no constructors defined. You can see that if you try to do this:

RoutingService rs = new RoutingService();

You will get an error from the compiler saying: The type System.ServiceModel.Routing.RoutingService has no constructors defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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