Autofac and RoutingService

328 views Asked by At

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?

1

There are 1 answers

1
Diego On

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.