Search code examples
c#asp.net-coreasp.net-core-2.1azure-notificationhub

How to set the proxy when connecting to AzureNotificationHub from a .Net core service


I'm wondering if someone has dealt with this scenario before. We have a .NET core 2.1 API that needs to connect to AzureNotificationHub to send some push notifications.

We're using the NotificationHubClient library by microsoft (nuget "microsoft azure notification hubs" v2.0.1). It works fine in all environments except production where we need to configure a proxy. This library doesn't seem to provide a way to set the proxy explicitely.

To make it more interesting, inside .NET core apparently setting the default web proxy used for all outgoing requests doesn't work either.

        WebRequest.DefaultWebProxy = new WebProxy(configuration.GetValue<string>("WebServices:Proxy"));
        WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;

There are ways to inject a proxy inside the Kestrel pipeline via a "middleware" type solution but they all seem to assume that the communication will be done by an HttpClient. What happens when we need to use a closed library?

Any help is greatly appreciated


Solution

  • On version 3.0.0 of microsoft.azure.notification.hubs the class NotificationHubClient has a constructor that receives an object of type NotificationHubClientSettings, you can use this object to configure the HttpMessageHandler or the Proxy that will be passed to the HttpClient that the NotificationHubClient object creates.

    This is a sample code to configure the proxy:

    NotificationHubClientSettings settings = new NotificationHubClientSettings();
    settings.Proxy = new WebProxy("1.1.1.1",8080)
    NotificationHubClient client = new NotificationHubClient("hubconnectionstring", "hubnotificationpath", settings);    
    

    Hope this helps, if you want to look other options you can use i suggest looking at the sourcecode on github (https://github.com/Azure/azure-notificationhubs-dotnet/blob/master/src/Microsoft.Azure.NotificationHubs/NotificationHubClient.cs).