简体   繁体   中英

Getting ConfigurationManager error when using NamespaceManager for Azure Service Bus

I have a simple class that I want to use to create a queue on my Azure service bus namespace. here's my class:

public class ServiceBusPublisher
{
    private readonly string _connString;        

    public ServiceBusPublisher(IConfiguration config)
    {
        _connString = config.GetSection("ServiceBus:Endpoint").Value;
    }

    public void CreateQueue(string queueName)
    {
        var namespaceManager = NamespaceManager.CreateFromConnectionString(_connString);

        if (!namespaceManager.QueueExists(queueName))
        {
            namespaceManager.CreateQueue(queueName);
        }            
    }
}

Everything is setup and seems to be working, I registered the service in my Startup, and my connection string comes through fine, as the service bus connection string:

Endpoint=sb://myservicebus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abcdefg12345

But when it tries to call NamespaceManager.CreateFromConnectionString(_connString) , I get this exception

System.IO.FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. File name: 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' at Microsoft.ServiceBus.Messaging.Configuration.KeyValueConfigurationManager.CreateNameValueCollectionFromConnectionString(String connectionString) at Microsoft.ServiceBus.Messaging.Configuration.KeyValueConfigurationManager.Initialize(String connection, Nullable`1 transportType) at Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString(String connectionString)

I feel like I'm crazy because this should be so simple, but it seems like it's trying to access the configuration file, and it can't. But I'm already passing it the connection string, so I'm not sure why it's doing that.

Am I doing something wrong?

I assume that you are using the WindowsAzure.ServiceBus library. It is not compatible with .net core platform.

Please note that this package requires .Net Framework 4.5.2 Full Profile.

If you want to create the queue in the .net core platform. I recommend that you'd better use Management library or wait till a replacement package for NamespaceManager is out.

Currently, the replacement package is not issued , for details info you could refer to this SO thread . For more details about how to use the Management Library, you could refer to another SO thread .

Following is demo code with Management Library.

var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"auth file path");
var azure = Azure
           .Configure()
           .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
           .Authenticate(credentials)
           .WithDefaultSubscription();
var sbNameSpace = "service bus namespace";
var resoureGroup = "resource group";
var servicebus = azure.ServiceBusNamespaces.GetByResourceGroup(resoureGroup, sbNameSpace);
var queue = servicebus.Queues.Define("queuename").Create()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM