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

问题描述 投票:3回答:1

我有一个.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>

包装类:

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;
        }
    }
}

AppSettings.json

{
  "StorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=someAccount;AccountKey=someKey==;EndpointSuffix=core.windows.net"
}

App.config中

<?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>
c# azure .net-core http-proxy azure-storage-queues
1个回答
4
投票

在官方azure-storage-net回购中找到一些提示:

理念:

  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);
    }
}

Queue.cs

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;
    }
}

当我在我们的代理后面时成功测试了这个。

旁注:删除App.configdefaultProxy设置,它不被dotnet核心使用。

© www.soinside.com 2019 - 2024. All rights reserved.