简体   繁体   中英

What causes Azure Service Bus Relay WCF Service to throw AddressAlreadyInUseException

When we try to start our WCF Service using our Azure Service Bus Relay address and webHttpRelayBinding we get an AddressAlreadyInUseException.

We are using the example here: https://code.msdn.microsoft.com/Relayed-Messaging-Bindings-a6477ba0

The sample doesn't work correctly unless you create the Relay with this code:

string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

var tRelayExists = namespaceManager.RelayExistsAsync("Image");
if (!tRelayExists.Result)
{
    var t = namespaceManager.CreateRelayAsync(new RelayDescription("Image", RelayType.Http));
    Task.WaitAll(t);
    RelayDescription result = t.Result;
}

Before doing any other work in Program.Main() and before you can do that you need to add the Azure Service Bus nuget package. You'll then need to update the ConnectionString in the App.config under appSettings named Microsoft.ServiceBus.ConnectionString with your Azure credentials.

We have used TCPViewer to see the ports in use and see no conflicts. In our actual project we've tried webHttpRelayBinding and netTcpRelayBinding. At the end of the day we want to use netTcpRelayBinding so we can use DuplexChannels.

Any ideas on what is causing our issue? Are we missing some undocumented configuration step? Every tutorial makes this look simple, but we've found that each tutorial is missing some key steps. So I wouldn't be surprised if there are more steps we've missed.

For service bus entities created with the NamespaceManager I discovered that the binding IsDynamic property needs to be set to false. This stops the AddressAlreadyInUseException.

    var binding = new NetTcpRelayBinding();
    binding.IsDynamic = false;

This is where I found the answer: http://www.codit.eu/blog/2014/12/securing-azure-service-bus-relay-endpoints-with-sharedaccesssignatures/ ?

Turns out the solution here is simple. If you use the NamespaceManager to create a relay you will get an AddressAlreadyInUseException. I guess that's why the NamespaceManager is not documented anywhere related to relays.

The sample works great as long as your namespace is created in the cloud and your credentials are set correctly. In my case I needed to use SharedAccessSignature not SharedSecret. All of the samples I had found over the last 3 days used SharedSecret up until about Friday last week.

When the WCF Service is hosted it creates the relay path in the namespace automatically. If it can't create it because it already exists you get the AddressAlreadyInUseException. As long as your creds are good then everything is happy.

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