无法将REST API用于Windows Server 1.1的Service Bus(内部部署)

时间:2014-02-10 13:13:57

标签: c# rest azure servicebus azureservicebus

我正在使用Window Server Service Bus 1.1。我能够使用以下基址

从Window Server Service Bus生成OAuth令牌
  string baseAddressHttp = "https://" + ServerName + ":9355/" + Namespace + "/$STS/OAuth/"; 

对于用于将消息发送到队列的Window Azure Service Bus,我可以在以下队列地址上发布请求

        string serviceNamespace = "ServiceBusDefaultNamespace";
        string queueName = "SampleQueuName";
        string message ="This is my first message";
        string queueAddress = "https://" + serviceNamespace + ".servicebus.windows.net/"+ queueName + "/messages";
        WebClient webClient = new WebClient();
        webClient.Headers[HttpRequestHeader.Authorization] = token;
        webClient.UploadData(baseAddress, "POST", Encoding.UTF8.GetBytes(message));

但对于窗口服务器服务总线,相同的技术不起作用。

Window Server Service Bus 1.1是否支持REST API? 任何帮助都将非常感激。

提前致谢!!

1 个答案:

答案 0 :(得分:4)

是的,它确实支持它。我们正在使用它。

  1. 我认为您的默认命名空间端点是错误的 - 您不需要 .servicebus.windows.net
  2. 您需要首先使用具有监听或发送服务总线权限的用户的用户名和密码从sts获取令牌。
  3. 必须以特定格式发布服务总线用户的用户名和密码。
  4. 然后,您需要将返回的令牌添加到授权标头中,并将实际的发送或侦听请求发送到/ messages端点。
  5. 此代码应该这样做:

    private const string DefaultNamespace = "https://yoursbserver:9355/servicebusdefaultnamespace";
    private const string QueueEndpoint = "https://yoursbserver:9355/servicebusdefaultnamespace/YourQueueNameOrTopicName/messages/?timeout=60";
    
    private static void Main()
    {
        var token = GetOAuthTokenFromSts(new Uri(DefaultNamespace), "usernamewithpermissiononServiceBus", "password", TimeSpan.FromMinutes(10));
        var messageXml = CreateXml();
        SendMessage(token, messageXml);
    
        Console.WriteLine("Successfully posted message!");
        Console.ReadLine();
    }
    
    private static string CreateMessage()
    {
        return "<SomeMessage xmlns='MyNamespace'></SomeMessage>";
    }
    
    public static string GetOAuthTokenFromSts(Uri namespaceBaseAddress, string userName, string userPassword, TimeSpan timeout)
    {
        const string stsPath = "$STS/OAuth/";
    
        var requestUri = new Uri(namespaceBaseAddress, stsPath);
        var requestContent = GetRequestContent(namespaceBaseAddress, userName, userPassword);
        var request = CreateRequest(timeout, requestUri, requestContent);
        return GetAccessToken(request, requestContent);
    }
    
    private static HttpWebRequest CreateRequest(TimeSpan timeout, Uri requestUri, byte[] requestContent)
    {
        var request = WebRequest.Create(requestUri) as HttpWebRequest;
        request.ServicePoint.MaxIdleTime = 5000;
        request.AllowAutoRedirect = true;
        request.MaximumAutomaticRedirections = 1;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = requestContent.Length;
        request.Timeout = Convert.ToInt32(timeout.TotalMilliseconds,
                                          CultureInfo.InvariantCulture);
        return request;
    }
    
    private static byte[] GetRequestContent(Uri namespaceBaseAddress, string userName, string userPassword)
    {
        const string clientPasswordFormat = "grant_type=authorization_code&client_id={0}&client_secret={1}&scope={2}";
        var requestContent = string.Format(CultureInfo.InvariantCulture,
                                              clientPasswordFormat, HttpUtility.UrlEncode(userName),
                                              HttpUtility.UrlEncode(userPassword),
                                              HttpUtility.UrlEncode(namespaceBaseAddress.AbsoluteUri));
        return Encoding.UTF8.GetBytes(requestContent);
    }
    
    private static string GetAccessToken(HttpWebRequest request, byte[] requestContent)
    {
        string accessToken;
        using (var requestStream = request.GetRequestStream())
        {
            requestStream.Write(requestContent, 0, requestContent.Length);
        }
    
        using (var response = request.GetResponse() as HttpWebResponse)
        using (var stream = response.GetResponseStream())
        using (var reader = new StreamReader(stream, Encoding.UTF8))
        {
            accessToken = reader.ReadToEnd();
        }
    
        return string.Format(CultureInfo.InvariantCulture, "WRAP access_token=\"{0}\"", accessToken);
    }
    
    private static void SendMessage(string token, string message)
    {
        var webClient = new WebClient();
        webClient.Headers[HttpRequestHeader.Authorization] = token;
        webClient.UploadData(QueueEndpoint, "POST", Encoding.UTF8.GetBytes(message));
    }