L
L
lucky42021-05-21 10:54:49
ASP.NET
lucky4, 2021-05-21 10:54:49

How to instantiate IOptions?

Quite a simple problem) There is a service that works with RabbitMQ.

public class MessageService : IMessageService
    {
        private readonly ConnectionFactory _factory;
        private readonly IConnection _connection;
        private readonly IModel _channel;
        private readonly IOptions<RabbitMQSettings> _rabbitSettings;

        public MessageService(IOptions<RabbitMQSettings> rabbitSettings)
        {
            _rabbitSettings = rabbitSettings;

            _factory = new ConnectionFactory()
            {
                HostName = rabbitSettings.Value.HostName,
                Port = rabbitSettings.Value.Port,
                UserName = rabbitSettings.Value.UserName,
                Password = rabbitSettings.Value.Password
            };
            _connection = _factory.CreateConnection();
            _channel = _connection.CreateModel();
        }


And I want to send a message to the client after certain operations. BUT) I can't instantiate in console app(static Main()) MessageService.

class Client
    {
        static void Main(string[] args)
        {
            IOptions<RabbitMQSettings> _rabbitSettings;

            var client = new MessageService(IOptions<RabbitMQSettings> rabbitSettings)
            {

            };
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-05-21
@lucky4

IOptions can be created with Options.Create
IOptions<A> options = Options.Create(new A());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question