![](/img/trans.png)
[英]How to set remote url when web app is running as a service in .net core (2.2)
[英]How to set the proxy when connecting to AzureNotificationHub from a .Net core service
我想知道以前是否有人處理過這種情況。 我們有一個.NET core 2.1 API,該API需要連接到AzureNotificationHub來發送一些推送通知。
我們正在使用Microsoft的NotificationHubClient庫(nuget“ Microsoft azure通知中心” v2.0.1)。 它在所有需要配置代理的生產環境中都能正常工作。 該庫似乎沒有提供顯式設置代理的方法。
更有趣的是,在.NET內核內部,顯然無法設置用於所有傳出請求的默認Web代理。
WebRequest.DefaultWebProxy = new WebProxy(configuration.GetValue<string>("WebServices:Proxy"));
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
有多種方法可以通過“中間件”類型的解決方案在Kestrel管道中注入代理,但它們似乎都假定通信將由HttpClient完成。 當我們需要使用封閉庫時會發生什么?
任何幫助是極大的贊賞
在microsoft.azure.notification.hubs的3.0.0版本上,類NotificationHubClient具有一個構造函數,該構造函數接收類型為NotificationHubClientSettings的對象,您可以使用此對象來配置HttpMessageHandler或將要傳遞給NotificationHubClient對象的HttpClient的代理。創建。
這是配置代理的示例代碼:
NotificationHubClientSettings settings = new NotificationHubClientSettings();
settings.Proxy = new WebProxy("1.1.1.1",8080)
NotificationHubClient client = new NotificationHubClient("hubconnectionstring", "hubnotificationpath", settings);
希望這會有所幫助,如果您想使用其他選項,我建議您查看github上的源代碼( https://github.com/Azure/azure-notificationhubs-dotnet/blob/master/src/Microsoft.Azure.NotificationHubs /NotificationHubClient.cs )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.