0

I´m trying to send a message to the queue from my WebApi application. The first time the server starts everything goes fine and I can send a message to the queue from my controller, but the next time the action is called it throws the folloowing exception:

Cannot access a disposed object.
Object name: 'UnicastBus'.

This is how I´m binding the IBus interface

public static class NinjectWebCommon
{
    ...
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IBus>().ToConstant(CreateBus()).InSingletonScope();
    }

    private static IBus CreateBus()
    {
        Configure.Serialization.Xml();
        return Configure.With()
           .DefaultBuilder()
           .UseTransport<Msmq>()
           .UnicastBus()
           .SendOnly();
    }
}

And in my controller

public class CreatedOrderMessageController : ApiController
{
  private readonly IBus bus;

  public CreatedOrderMessageController(IBus bus)
  {
    this.bus = bus;
  }

  public string Get(int id)
  {
    bus.Send(new OrderCreatedMessage(id));
    return "true";
  }
}

Any thoughts on this error?

Thanks

Marcio Toshio
  • 178
  • 12
  • 1
    Why are you using the default builder for NSB (which is autofac) and then register it by constant? Try to use the extension method NinjectBuilder and pass in your kernel – Daniel Marbach Oct 22 '13 at 04:24
  • Changed to use the NinjectBuilder, now I can send one message like before but on the second try it throws another error: Error loading Ninject component ICache No such component has been registered in the kernel's component container. – Marcio Toshio Oct 22 '13 at 14:57
  • Did ypu reference the namedscope and the contextpreservation extension? This is required. Not referencing those will throw this obscure exception – Daniel Marbach Oct 22 '13 at 17:07
  • No, I don´t know anything about this extensions, can you tell me more about? – Marcio Toshio Oct 22 '13 at 17:39
  • Probably this modules are already been loaded, i try to put this this.kernel.Load(new NamedScopeModule()); this.kernel.Load(new ContextPreservationModule()); and the app throws that the modules are already been loaded – Marcio Toshio Oct 22 '13 at 17:44
  • Got it this thread helps me http://stackoverflow.com/questions/11356864/ninject-insingletonscope-with-web-api-rc I was disposing the kernel and this generates issue with singleton scopes – Marcio Toshio Oct 22 '13 at 18:43
  • Please create the answer below and accept it to help others, thanks. – Adam Fyles Oct 23 '13 at 13:25

1 Answers1

2

I changed de code to use the NinjectBuilder

in the RegisterServices method I used:

Configure.Serialization.Xml();
      Configure.Transactions.Disable();
      Configure.With()
      .NinjectBuilder(kernel)
      .UseTransport<Msmq>()
      .UnicastBus()
      .SendOnly();

And this thread give me a tip Ninject InSingletonScope with Web Api RC

In order to Ninject work with WebApi I needed to implement a custom IDependencyScope I was disposing the kernel and I think it generated a issue with singleton scopes, so in Dispose method in IDependencyScope I do nothing

Community
  • 1
  • 1
Marcio Toshio
  • 178
  • 12