服务总线1.0和WCF NetMessagingBinding - 令牌提供程序无法提供安全令牌

时间:2013-08-03 09:31:06

标签: wcf azure servicebus

我正在使用Service Bus for Windows Service 1.0(客户端引用Microsoft.ServiceBus.dll 1.8.0.0)

我正在尝试使WCF客户端和服务NetMessagingBinding示例正常工作。

我只是想向服务总线提交一条消息,并将其发送到我的wcf服务。

目前我提交时遇到问题,因为它无法找到令牌提供程序:

我有以下代码:

  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();
    }

//我的订单类如下所示:

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

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

但是当我尝试发送到队列时(client.ProcessOrder(order);)

我得到了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] DNS Lookup for "servicebusdefaultnamespace-sb.accesscontrol.windows.net" failed. The requested name is valid, but no data of the requested type was found                          

所以我想知道问题是什么:

  1. sb://servicebusdefaultnamespace.servicebus.windows.net/Orders/ 是否设置了我的端点的正确URI?我问这个是因为当我得到get-sbClientConfiguration时 - 我的连接字符串是:

       Endpoint=sb://myPC/ServiceBusDefaultNamespace;
       StsEndpoint=https://myPC:9355/ServiceBusDefaultNamespace;
       RuntimePort=9354;ManagementPort=9355
    
  2. 或者我不能在事先使用sharedsecret吗? (我更愿意使用这种身份验证方法)

  3. 有人能发现问题吗?

    感谢您的帮助

1 个答案:

答案 0 :(得分:3)

刚开始工作!

有两件事是错的:我想通过添加servicebus.windows.net命名空间,我试图调用azure令牌提供程序。

  1. 所以我创建了一个windowsTokenProvider,而URI是sts地址

    https://mypc:9355/ServiceBusDefaultNamespace
    
  2. 你可以通过运行这个powershell cmd来找到它:

       get-sbclientconfiguration
    
    1. 然后我将服务端点地址更改为:(删除servicebus.windows.net)

         sb://mypcname/servicebusdefaultnamespace/Orders
      
    2. 现在它正在发布到服务总线。

      以下是最终代码:

          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();
          }