2014-02-10 88 views
2

我正在使用Window Server Service Bus 1.1。我能夠使用下面的基地址無法爲Windows Server 1.1(OnPremises)的Service Bus使用REST API

string baseAddressHttp = "https://" + ServerName + ":9355/" + Namespace + "/$STS/OAuth/"; 

對於窗口天青服務總線用於發送消息到隊列我可以張貼在以下隊列地址的請求,以生成從窗口服務器服務總線OAuth令牌

 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? 任何幫助將非常感激。

在此先感謝!

+0

在那裏我的回答有問題? – jonho

+0

它的工作!非常感謝Jonho! –

回答

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)); 
    } 
    
相關問題