5

我在本地使用 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. 还是我不能在 prem 上使用 sharedsecret 的事实?(我更喜欢使用这种身份验证方法)

谁能发现问题?

谢谢你的帮助

4

1 回答 1

3

刚刚开始工作!

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

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

    https://mypc:9355/ServiceBusDefaultNamespace
    

你可以通过运行这个 powershell cmd 找到它:

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

       sb://mypcname/servicebusdefaultnamespace/Orders
    

它现在正在发布到服务总线。

这是最终代码:

    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();
    }
于 2013-08-03T10:52:21.830 回答