5

I am using Service Bus for Windows Service 1.0 on prem (clients are referencing Microsoft.ServiceBus.dll 1.8.0.0)

I am trying to get a WCF client and service NetMessagingBinding example working.

I simply want to submit a message to service bus and have it pumped out to my wcf service.

At the moment I have having problems submitting, as it cannot find the token provider:

I have the following code:

  private const string Issuer = "myIssuerName";
  private const string Key = "BBBBBB=";
  private static readonly Uri ServiceBusEndpointAddress =
        new Uri("{sb://servicebusdefaultnamespace.servicebus.windows.net/Orders/}");

  private static void Main(string[] args)
    {
        //SetUp
        var binding = new NetMessagingBinding();
        var contract = ContractDescription.GetContract(typeof (IOrderService), typeof (OrderService));

        var transportBehavior = new TransportClientEndpointBehavior();
        transportBehavior.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(Issuer, Key);

        var endpoint = new ServiceEndpoint(contract, binding,
                                           new EndpointAddress(ServiceBusEndpointAddress.AbsoluteUri));
        endpoint.Behaviors.Add(transportBehavior);

        var host = new ServiceHost(typeof (OrderService), ServiceBusEndpointAddress);
        host.AddServiceEndpoint(endpoint);

        host.Open();


        //send message to queue

        var factory = new ChannelFactory<IOrderService>(endpoint);
        var client = factory.CreateChannel();

        var order = new Order();
        order.OrderId = 42;

        //this is where the exception is raised
        client.ProcessOrder(order);
        factory.Close();

        Console.ReadLine();
    }

//my order class looks like this:

 [ServiceContract()]
 public interface IOrderService
 {
    [OperationContract(IsOneWay = true)]
    void ProcessOrder(Order order);
 }

[DataContract()]
public class Order
{
    [DataMember]
    public Int64 OrderId;
}

But when I try to send to the queue (client.ProcessOrder(order);)

I get a 502:

  unauthorisedaccessexception
   The token provider was unable to provide a security token while accessing  
'https://servicebusdefaultnamespace-sb.accesscontrol.windows.net/WRAPv0.9/'. Token 
  provider returned message: 'The remote name could not be resolved:       'servicebusdefaultnamespace-sb.accesscontrol.windows.net''.
 The remote name could not be resolved: 'servicebusdefaultnamespace- sb.accesscontrol.windows.net'

 at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
 at System.Net.HttpWebRequest.GetRequestStream()
 at Microsoft.ServiceBus.TokenProviderHelper.GetAccessTokenCore(Uri requestUri, String 
 appliesTo, String requestToken, String simpleAuthAssertionFormat, TimeSpan timeout,  
  String& expiresIn)

fiddler tells me:

  [Fiddler] DNS Lookup for "servicebusdefaultnamespace-sb.accesscontrol.windows.net" failed. The requested name is valid, but no data of the requested type was found                          

So I was wondering what the problem is:

  1. Is sb://servicebusdefaultnamespace.servicebus.windows.net/Orders/ the correct URI to set up my endpoint on?. I ask this because when I get get-sbClientConfiguration - my connection string is:

       Endpoint=sb://myPC/ServiceBusDefaultNamespace;
       StsEndpoint=https://myPC:9355/ServiceBusDefaultNamespace;
       RuntimePort=9354;ManagementPort=9355
    
  2. Or is it the fact that I cant use sharedsecret on prem? (I would prefer to use this authentication approach)

Can anyone spot the problem?

Thanks for your help

jonho
  • 1,680
  • 2
  • 19
  • 29

1 Answers1

3

Just got it working!

There were two things wrong: One I think by adding the servicebus.windows.net namespace I was trying to call out to azure token provider.

  1. So instead I created a windowsTokenProvider, and the URI was the sts address

    https://mypc:9355/ServiceBusDefaultNamespace
    

you can find this by running this powershell cmd:

   get-sbclientconfiguration
  1. Then I changed my Service endpoint address to: (dropping the servicebus.windows.net)

       sb://mypcname/servicebusdefaultnamespace/Orders
    

and it is now publishing to service bus.

Here is the final code:

    private static void Main(string[] args)
    {

        var ServiceBusEndpointAddress = new Uri("sb://mypc/servicebusdefaultnamespace/Orders");

        //SetUp
        var binding = new NetMessagingBinding();
        var contract = ContractDescription.GetContract(typeof (IOrderService), typeof (OrderService));
        var uri = new Uri("https://mypc:9355/ServiceBusDefaultNamespace");
        var uris = new List<Uri> { uri };
        // Get credentials as Endpoint behavior
        var securityBehavior = new TransportClientEndpointBehavior
            {
                TokenProvider = TokenProvider.CreateWindowsTokenProvider(uris)
            };

        var endpoint = new ServiceEndpoint(contract, binding,
                                           new EndpointAddress(ServiceBusEndpointAddress));
        endpoint.Behaviors.Add(securityBehavior);

        var host = new ServiceHost(typeof(OrderService), uri);
        host.AddServiceEndpoint(endpoint);

        host.Open();


        //Client

        var factory = new ChannelFactory<IOrderService>(endpoint);
        var client = factory.CreateChannel();

        var order = new Order();
        order.OrderId = 42;

        client.ProcessOrder(order);
        factory.Close();

        Console.ReadLine();
    }
jonho
  • 1,680
  • 2
  • 19
  • 29