gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:14:08 26 4
gpt4 key购买 nike

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

.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"
}

应用程序配置

<?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>

最佳答案

在官方找到一些提示azure-storage-net repo 协议(protocol):

想法:

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

队列.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;
}
}

当我位于代理后面时,成功测试了这一点。

旁注:删除 defaultProxyApp.config 设置,它不被 dotnet core 使用。

关于c# - 连接到代理后面的 Azure 存储队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55927663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com