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)
在官方azure-storage-net repo上找到了一些提示:
主意:
DelegatingHandler以在那里设置代理基于您的示例的实施:
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 不使用它。
| 归档时间: |
|
| 查看次数: |
1551 次 |
| 最近记录: |