Value cannot be NULL at NServiceBus.IMessageSessionExtensions.Send(IMessageSession session, Object message)

Hi,
We are getting the following error while sending an object.

My code looks like this:
public IMessageSession Bus { get; set; }

var m = new OrderCommand() { InvoiceId = invoiceId, CallbackUrl = callbackUrl };
Bus.Send(m);

Error messge:
Exception = Value cannot be null.
Parameter name: session, Inner Exception = , Stack Trace = at NServiceBus.IMessageSessionExtensions.Send(IMessageSession session, Object message)

Update: Here the Bus is coming as NULL.
I tried with IMessageSession, IEndpointInstance and IMessageHandlerContext but the result is same.

Hi Kirans!

Can you share your endpoint configuration? Or even better try to create a minimal reproduction of the issue and share the project with us.

Cheers,

Andreas

I have the endpoint config settings in endpointconfig.cs file.

public class EndpointConfig : IConfigureThisEndpoint
{
public void Customize(EndpointConfiguration endpointConfiguration)
{
endpointConfiguration.UseSerialization();
log4net.Config.XmlConfigurator.Configure();
endpointConfiguration.SendFailedMessagesTo(“Error”);
endpointConfiguration.DisableFeature();
endpointConfiguration.DisableFeature();
LogManager.Use();
}
public void Init()
{
var endpointConfiguration = new EndpointConfiguration(“Radiator.ServiceBus.Inventory.Gateway”);
Customize(endpointConfiguration);
}
}

And also placed in Global.asax.cs file. But it was not considered and was giving me the NULL exception.
Global.asax.cs
var endpointConfiguration = new EndpointConfiguration(“Radiator.ServiceBus.Inventory.Gateway”);
endpointConfiguration.UseSerialization();
var transport = endpointConfiguration.UseTransport();
transport.Transactions(TransportTransactionMode.None);

		endpointConfiguration.SendFailedMessagesTo("Error");
		endpointConfiguration.DisableFeature<TimeoutManager>();
		endpointConfiguration.DisableFeature<MessageDrivenSubscriptions>();
        
        endpointConfiguration.EnableInstallers();

Finally, I placed this in my controller just before sending the object and this worked.
controller.cs
public IMessageSession Bus { get; set; }
public static IMessageSession InitiliseIMessageSession()
{
var cfg = new EndpointConfiguration(“Radiator.ServiceBus.Inventory.Gateway”);
cfg.UseTransport();
cfg.UsePersistence();
cfg.PurgeOnStartup(true);
cfg.EnableInstallers();
cfg.UseSerialization();
return Endpoint.Start(cfg).GetAwaiter().GetResult();
}
public ActionResult SubmitOrder(long invoiceId, string callbackUrl)
{
Bus = InitiliseIMessageSession();
var service = new InventoryService();
string serviceName = service.ToString() + " - SubmitOrder";
var m = new OrderCommand() { InvoiceId = invoiceId, CallbackUrl = callbackUrl };
Bus.Send(m).ConfigureAwait(false);
Global.LogRoot.Info(String.Format(“ServiceControler 6”));
return Content((true).ToString());
}

With this NULL exception is gone but I am not sure whether is correct or not.
And why is it not worked with my endpointconfig.cs file and global.asax.cs?