连接到代理后面的 Azure 存储队列

Bre*_*rry 3 c# azure http-proxy azure-storage-queues .net-core

我有一个 .NET Core 2.2 控制台应用程序可以连接到 Azure 存储队列。它在我的电脑上工作,但我无法让它在公司代理后面工作。我需要做什么?(我对存储帐户名称和密钥以及代理主机名进行了匿名处理。)

.csproj:

<Project Sdk="Microsoft.NET.Sdk">    
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>8.0</LangVersion>
    <NullableContextOptions>enable</NullableContextOptions>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>    
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.21.0" />
    <PackageReference Include="Microsoft.Azure.Storage.Queue" Version="10.0.1" />
  </ItemGroup>    
</Project>
Run Code Online (Sandbox Code Playgroud)

包装类:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using System.Threading.Tasks;

namespace Queue
{
    public class Queue
    {
        public Queue(string connectionString, string queueName)
        {
            var storageAccount = CloudStorageAccount.Parse(connectionString);            
            var cloudQueueClient = storageAccount.CreateCloudQueueClient();
            CloudQueue = cloudQueueClient.GetQueueReference(queueName);            
        }

        private CloudQueue CloudQueue { get; }

        public async Task<string> PeekAsync()
        {
            var m = await CloudQueue.PeekMessageAsync();            
            return m.AsString;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

应用设置.json

{
  "StorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=someAccount;AccountKey=someKey==;EndpointSuffix=core.windows.net"
}
Run Code Online (Sandbox Code Playgroud)

应用配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
      <proxy 
        usesystemdefault="True" 
        proxyaddress="http://someProxy:8080" 
      />
    </defaultProxy>
  </system.net>
</configuration>
Run Code Online (Sandbox Code Playgroud)

Dan*_*iel 6

在官方azure-storage-net repo上找到了一些提示:

主意:

  1. 创建一个继承自的自定义类DelegatingHandler以在那里设置代理
  2. 在您的应用程序中使用该类

基于您的示例的实施:

DelegatingHandlerImpl.cs(取自https://github.com/Azure/azure-storage-net/blob/master/Test/Common/TestBase.Common.cs

public class DelegatingHandlerImpl : DelegatingHandler
{
    public int CallCount { get; private set; }

    private readonly IWebProxy Proxy;

    private bool FirstCall = true;

    public DelegatingHandlerImpl() : base()
    {

    }

    public DelegatingHandlerImpl(HttpMessageHandler httpMessageHandler) : base(httpMessageHandler)
    {

    }

    public DelegatingHandlerImpl(IWebProxy proxy)
    {
        this.Proxy = proxy;
    }

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        CallCount++;
        if (FirstCall && this.Proxy != null)
        {
            HttpClientHandler inner = (HttpClientHandler)this.InnerHandler;
            inner.Proxy = this.Proxy;
        }
        FirstCall = false;
        return base.SendAsync(request, cancellationToken);
    }
}
Run Code Online (Sandbox Code Playgroud)

队列文件

public class Queue
{
    public Queue(string connectionString, string queueName)
    {
        var storageAccount = CloudStorageAccount.Parse(connectionString);
        var proxy = new WebProxy()
        {
            // More properties here
            Address = new Uri("your proxy"),
        };
        DelegatingHandlerImpl delegatingHandlerImpl = new DelegatingHandlerImpl(proxy);
        CloudQueueClient cloudQueueClient = new CloudQueueClient(storageAccount.QueueStorageUri, storageAccount.Credentials, delegatingHandlerImpl);
        CloudQueue = cloudQueueClient.GetQueueReference(queueName);
    }

    private CloudQueue CloudQueue { get; }

    public async Task<string> PeekAsync()
    {
        var m = await CloudQueue.PeekMessageAsync();
        return m.AsString;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我落后于我们的代理时,成功地测试了这一点。

旁注:删除您的App.config设置,defaultProxy因为 dotnet core 不使用它。

  • 我看到 CloudQueueClient 构造函数的 2 个重载:`public CloudQueueClient(Uri baseUri, StorageCredentials credentials);``public CloudQueueClient(StorageUri storageUri, StorageCredentials credentials);`,并且您给出的示例接受 3 个参数。我升级到 Microsoft.Azure.Storage.Queue 10.0.2。 (2认同)
  • 当我从 Microsift.WindowsAzure.Storage 命名空间更改为 Microsift.Azure.Storage 命名空间时,新的构造函数就在那里。这是同一个 NuGet 包。 (2认同)