Dependancy issue


(Steve Smith) #1

Hey, currently prototyping nservicebus using an existing app. app is .NET4.5.2 console app.

I have the following code in my program.cs file.

IUnityContainer container = new UnityContainer();

        //register
        container.RegisterType(typeof(IGenericHttpRequestRepository<>), typeof(GenericHttpRequestRepository<>), new TransientLifetimeManager());
        container.RegisterType<IOrderRepository, OrderRepository>();
        container.RegisterType<IShipmentRepository, ShipmentRepository>();
        container.RegisterType<IOrderProcessService, OrderProcessService>();
        container.RegisterType<IShipmentService, ShipmentService>();
        
        //resolve 
        var orderProcessService = container.Resolve<IOrderProcessService>();
        var shipmentService = container.Resolve<IShipmentService>();

        var endpointConfiguration = new EndpointConfiguration("NSB.ChannelAdvisorService");
        var transport = endpointConfiguration.UseTransport<LearningTransport>();

        endpointConfiguration.UseContainer<UnityBuilder>(
            customizations: customizations =>
            {
                customizations.UseExistingContainer(container);
            });

        var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false);

which unfortunately gives me an error when it trys to start the endpoint

The current type, NServiceBus.IEndpointInstance, is an interface and cannot be constructed. Are you missing a type mapping?

I have a service class that i wish to send a command message from.

public async Task PostNewOrderBatch()
    {
        var list = _orderRepository.GetBatchedOrders();
        
        foreach(var item in list)// parallel this?
        {
            await _endpoint.Send(item.ToObject<ProcessBatchOrdersCommand>()).ConfigureAwait(false);
            _orderRepository.DeleteFile(item.Property("FilePath").Value.ToString());
        }
    }

what am i missing here?


(Daniel Marbach) #2

Hi Steve,

The endpoint instance is not automatically registered in the container.

The endpoint instance is returned on Endpoint.Start. You need to register that as a binding on the container.

See the note on our documentation page

https://docs.particular.net/nservicebus/dependency-injection/#using-an-existing-instance-endpoint-resolution

Sorry if this caused any inconvenience. Please let us know how we could improve the documentation if things are not clearly spelled out on our doco page.

Regards
Daniel