question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom HttpClientFactory doesnot work after upgrade to new version

See original GitHub issue

I got a “Unable to read data from the transport connection” error with the follow codes, but it works with the old version.

thank you.


           var proxy = "http://192.168.1.200:8080";
            var client = new DirectoryService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Gbackuper Directory Helper Class",
                HttpClientFactory = new ProxyHttpClientFactory(proxy),
            });
            
           var orgunitsRequest = client.Orgunits.List("my_customer");
           orgunitsRequest.Type = OrgunitsResource.ListRequest.TypeEnum.All;
           var orgs = await orgunitsRequest.ExecuteAsync(cancellationToken);


    public class ProxyHttpClientFactory : HttpClientFactory
    {
        private string _proxyUrl;
        public ProxyHttpClientFactory(string proxyUrl)
        {
            _proxyUrl = proxyUrl;
        }
        
        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var proxy = new WebProxy(_proxyUrl, true) { UseDefaultCredentials = false };
            return new HttpClientHandler { UseProxy = true,  Proxy = proxy};
        }    
        
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12

github_iconTop GitHub Comments

2reactions
mirror222commented, Mar 30, 2020

Please try this code, which I’d expect to work - basically you need your credential to know the client factory to use:

var scopes = new[]
{
    DirectoryService.Scope.AdminDirectoryDomainReadonly,
    DirectoryService.Scope.AdminDirectoryOrgunitReadonly,
    DirectoryService.Scope.AdminDirectoryUserReadonly,
    DirectoryService.Scope.AdminDirectoryDomainReadonly
};

var serviceCredentialFromFile = (ServiceAccountCredential) GoogleCredential.FromFile(sa_file).UnderlyingCredential;

// Specify the scopes, proxy and impersonation in the new initializer, all in one go, just for simplicity.
var initializer = new ServiceAccountCredential.Initializer(serviceCredentialFromFile.Id)
{
    ProjectId = serviceCredentialFromFile.ProjectId,
    Key = serviceCredentialFromFile.Key,
    User = user_email,
    HttpClientFactory = new ProxyHttpClientFactory(proxy),
    Scopes = scopes
};
var credential = GoogleCredential.FromServiceAccountCredential(new ServiceAccountCredential(initializer));

awesome, it works!!!

1reaction
jskeetcommented, Mar 30, 2020

Okay, great. Will close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make HTTP requests using IHttpClientFactory in ASP.NET ...
Learn about using the IHttpClientFactory interface to manage logical HttpClient instances in ASP.NET Core.
Read more >
vb.net - unable to make httpclientfactory working from a . ...
I am just trying to implement HttpClientFactory in my . net 4.7. 2 Winforms app for a rest-api client.
Read more >
Using HttpClientFactory in ASP.NET Core Applications
Let's learn how to use HttpClientFactory in .NET Core applications. Also, we will learn how to use Named and Typed client instances.
Read more >
Make SOAP requests using IHttpClientFactory in .NET Core
I updated NuGet packages and all started working as it should! I hit it from JMeter with 5 threads (and 100 calls per...
Read more >
You're using HttpClient wrong and it is destabilizing your ...
I've been using HttpClient wrong for years and it finally came back to bite me. My site was unstable and my clients furious,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found