2017-03-26 15 views

答えて

3

current clientは.NETコア互換ではありませんが、進行中の作業であるnew clientは100%互換です。プレリリースパッケージは4月3日に利用可能になり、ステータスはhereとなります。コースのコードをプルダウンして、チームが設計の詳細を洗い出ししようとしているときにAPIが変更されるという警告を出して、それを今日コンパイルすることができます。

+0

感謝あなたは非常に! Nugetパッケージをインストールすることでメッセージを送信できます。 –

+0

昨日、ナゲットにアルファ版がリリースされました。だから今は本当です:) –

+0

.NET Core 2.0または.NET Standard 2をターゲットにしたい場合はどうすればよいですか? – pseabury

1

REST APIを使用してキューにメッセージを送信する方法を教えていただけますか?彼のコメントで述べた4c74356b41よう

、我々はthis REST API経由のAzure Service Busのキューにメッセージを送ることができます:ここに

POST http{s}://{serviceNamespace}.servicebus.windows.net/{queuePath|topicPath}/messages

enter image description here

例です上記の要求では、私は共有アクセス署名(トークン)をg共有アクセス署名(トークン)を生成するには、this articleを参照してください。

0

フレッドさんの答えを受けて、署名付きの認証ヘッダーの投稿方法を説明しました。

public class AzureServiceBusSettings 
{ 
    public string BaseUrl { get; set; } 
    public string SharedAccessKey { get; set; } 
    public string SharedAccessKeyName { get; set; } 
} 

public interface IServiceBus 
{ 
    /// <summary> 
    /// Publish domain events to domain topic. 
    /// </summary> 
    Task PublishAsync<T>(T @event) 

    /// <summary> 
    /// Send commands to command queue. 
    /// </summary> 
    Task SendAsync<T>(T command) 
} 

public class ServiceBus : IServiceBus 
{ 
    private readonly AzureServiceBusSettings _settings; 

    public ServiceBus(IOptions<AzureServiceBusSettings> azureServiceBusSettings) 
    { 
     _settings = azureServiceBusSettings.Value; 
    } 

    /// <summary> 
    /// Publish domain events to domain topic. 
    /// </summary> 
    public async Task PublishAsync<T>(T @event) 
    { 
     await SendInternalAsync(@event, "domain"); 
    } 

    /// <summary> 
    /// Send commands to command queue. 
    /// </summary> 
    public async Task SendAsync<T>(T command) 
    { 
     await SendInternalAsync(command, "commands"); 
    } 

    private async Task SendInternalAsync<T>(T command, string queueName) 
    { 
     var json = JsonConvert.SerializeObject(command); 
     var content = new StringContent(json, Encoding.UTF8, "application/json"); 

     using (var httpClient = new HttpClient()) 
     { 
      httpClient.BaseAddress = new Uri(_settings.BaseUrl); 

      try 
      { 
       var url = $"/{queueName}/messages"; 

       httpClient.DefaultRequestHeaders.Authorization = 
        new AuthenticationHeaderValue("SharedAccessSignature", GetSasToken(queueName)); 

       var response = await httpClient.PostAsync(url, content); 

       // Success returns 201 Created. 
       if (!response.IsSuccessStatusCode) 
       { 
        // Handle this. 
       } 
      } 
      catch (Exception ex) 
      { 
       // Handle this. 
       // throw; 
      } 
     } 
    } 

    private string GetSasToken(string queueName) 
    { 
     var url = $"{_settings.BaseUrl}/{queueName}"; 
     // Expiry minutes should be a setting. 
     var expiry = (int)DateTime.UtcNow.AddMinutes(20).Subtract(new DateTime(1970, 1, 1)).TotalSeconds; 
     var signature = GetSignature(url, _settings.SharedAccessKey); 
     var token = $"sr={WebUtility.UrlEncode(url)}&sig={WebUtility.UrlEncode(signature)}&se={expiry}&skn={_settings.SharedAccessKeyName}"; 
     return token; 
    } 

    private static string GetSignature(string url, string key) 
    { 
     var expiry = (int)DateTime.UtcNow.AddMinutes(20).Subtract(new DateTime(1970, 1, 1)).TotalSeconds; 
     var value = WebUtility.UrlEncode(url) + "\n" + expiry; 
     var encoding = new UTF8Encoding(); 
     var keyByte = encoding.GetBytes(key); 
     var valueBytes = encoding.GetBytes(value); 
     using (var hmacsha256 = new HMACSHA256(keyByte)) 
     { 
      var hashmessage = hmacsha256.ComputeHash(valueBytes); 
      var result = Convert.ToBase64String(hashmessage); 
      return result; 
     } 
    } 
} 

とポストに簡単なのxUnitテスト:

public class ServiceBusTests 
{ 
    public class FooCommand : ICommand 
    { 
     public Guid CommandId { get; set; } 
    } 

    private Mock<IOptions<AzureServiceBusSettings>> _mockAzureServiceBusOptions; 

    private ServiceBus _sut; 

    public ServiceBusTests() 
    { 
     var settings = new AzureServiceBusSettings 
     { 
      BaseUrl = "https://my-domain.servicebus.windows.net", 
      SharedAccessKey = "my-key-goes-here", 
      SharedAccessKeyName = "RootManageSharedAccessKey" 
     }; 

     _mockAzureServiceBusOptions = new Mock<IOptions<AzureServiceBusSettings>>(); 
     _mockAzureServiceBusOptions.SetupGet(o => o.Value).Returns(settings); 

     _sut = new ServiceBus(
      _mockAzureServiceBusOptions.Object); 
    } 

    [Fact] 
    public async Task should_send_message() 
    { 
     // Arrange. 
     var command = new FooCommand {CommandId = Guid.NewGuid()}; 

     // Act. 
     await _sut.SendAsync(command); 

     // Assert. 
     // TODO: Get the command from the queue and assert something. 
    } 
} 
関連する問題