implement autofac with rabbit mq service bus

518 views
Skip to first unread message

Munish kumar

unread,
Apr 3, 2017, 7:19:28 AM4/3/17
to Autofac
Hi,

Can you please help me to register service bus EasyNetQ  using autofac?


Alex Meyer-Gleaves

unread,
Apr 3, 2017, 8:15:49 AM4/3/17
to Autofac
Hi Munish,

Your question is rather broad and to get a reasonable answer you need to be more specific.

The documentation for EasyNetQ details how a DI container can be used with the library.


There is also an existing Autofac integration and NuGet package.


I would start by taking a look at these.

Cheers,

Alex.
Message has been deleted

Munish kumar

unread,
Apr 3, 2017, 8:53:17 AM4/3/17
to aut...@googlegroups.com
i want to register EasyNetQ ServiceBus with AutoFac.
 

So that i can use this bus in my entire api project .

For ex:

 builder.RegisterType<BuiltInEventTagManager>().As<IBuiltInEventTagManager>();

In this way i want to register my EasyNetQ service bus. 

This is the code i have written and i am using factory in this. But i want to register it using AutoFac container.

   public static class ServiceBusFactory
    {
        /// <summary>
        /// The current bus
        /// </summary>
        private static IBus currentBus = null;

        /// <summary>
        /// Gets the instance of <see cref="IBus"/>.
        /// </summary>
        /// <returns><see cref="IBus"/>.</returns>
        public static IBus GetInstance()
        {
            return currentBus;
        }

        /// <summary>
        /// Sets the bus.
        /// </summary>
        /// <param name="bus">The bus.</param>
        public static void SetBus(IBus bus)
        {
            currentBus = bus;
        }
    }


    private xyz()
        {  
            this.bus = EasyNetQBusFactory.GetInstance();
            ServiceBusFactory.SetBus(this.bus);
        }



  public static class EasyNetQBusFactory
    {
        /// <summary>
        /// The lock.
        /// </summary>
        private static readonly object Lock = new object();

        /// <summary>
        /// The current bus
        /// </summary>
        private static IBus currentBus = null;

        /// <summary>
        /// Gets the instance of <see cref="IBus"/>.
        /// </summary>
        /// <returns>The instance of <see cref="IBus"/></returns>
        public static IBus GetInstance()
        {
            if (currentBus == null)
            {
                lock (Lock)
                {
                    if (currentBus == null)
                    {
                        var connectionString = ConfigurationManager.AppSettings["RabbitMQ.ConnectionString"];
                        var encryptionKey = ConfigurationManager.AppSettings["RabbitMQ.EncryptionKey"];
                        var logger = LogManager.GetLogger(typeof(IAdvancedBus));
                        currentBus = RabbitHutch.CreateBus(
                            connectionString,
                            service => service
                                .Register<IEasyNetQLogger>(_ => new Log4NetLogger(logger))
                                .Register<ISerializer>(_ => new EncryptionEnabledJsonSerializer(encryptionKey, new TypeNameSerializer())));
                        currentBus.Advanced.Connected += (o, args) =>
                        {
                            LogManager.GetLogger(typeof(EasyNetQBusFactory)).Debug("Connected.");
                        };

                        currentBus.Advanced.Disconnected += (o, args) =>
                        {
                            LogManager.GetLogger(typeof(EasyNetQBusFactory)).Error("Disconnected.");
                        };

                        currentBus.Advanced.Blocked += (o, args) =>
                        {
                            LogManager.GetLogger(typeof(EasyNetQBusFactory)).Fatal("Blocked.");
                        };

                        currentBus.Advanced.Unblocked += (o, args) =>
                        {
                            LogManager.GetLogger(typeof(EasyNetQBusFactory)).Info("Unblocked.");
                        };

                        currentBus.Advanced.MessageReturned += (o, args) =>
                        {
                            LogManager.GetLogger(typeof(EasyNetQBusFactory)).FatalFormat("MessageReturned. [{0}]", args.MessageBody);
                        };
                    }
                }
            }

            return currentBus;
        }

        /// <summary>
        /// Kills the instance of <see cref="IBus"/>.
        /// </summary>
        public static void KillInstance()
        {
            if (currentBus != null)
            {
                currentBus.Dispose();
                currentBus = null;
            }
        }
    }

On Mon, Apr 3, 2017 at 6:09 PM, Munish kumar <mun...@insteptechnologies.com> wrote:
i want to register EasyNetQ ServiceBus with AutoFac.
 

So that i can use this bus in my entire api project .

For ex:

 builder.RegisterType<BuiltInEventTagManager>().As<IBuiltInEventTagManager>();

In this way i want to register my EasyNetQ service bus. 

--
You received this message because you are subscribed to a topic in the Google Groups "Autofac" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/autofac/cklOMx60ldY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to autofac+unsubscribe@googlegroups.com.
To post to this group, send email to aut...@googlegroups.com.
Visit this group at https://groups.google.com/group/autofac.
For more options, visit https://groups.google.com/d/optout.



--
Thanks & Regards 
   Munish Singla
Instep Technologies



--
Thanks & Regards 
   Munish Singla
Instep Technologies

Alex Meyer-Gleaves

unread,
Apr 4, 2017, 9:29:15 AM4/4/17
to Autofac
Looking at the code of the Autofac adaptor it looks like this is the expected usage:

var builder = new ContainerBuilder();

// Add other registrations here.

// Use the helper to register the factory for creating IBus and get IContainer instance back.
var container = builder.RegisterAsEasyNetQContainerFactory(() => RabbitHutch.CreateBus("host=localhost"));

// Do stuff with IBus or have injected into other services.|
var bus = container.Resolve<IBus>();

// Disposing adaptor to dispose container.
((AutofacAdapter)bus.Advanced.Container).Dispose();
bus.Dispose();

Munish

unread,
Apr 10, 2017, 8:14:41 AM4/10/17
to Autofac
i did it in this way 

 builder.Register(c => EasyNetQBusFactory.GetInstance()).As<IBus>().InstancePerLifetimeScope();

Thanks for your help.
Reply all
Reply to author
Forward
0 new messages