Giter Club home page Giter Club logo

httpclientfactorylite's People

Contributors

ajaybhargavb avatar ajcvickers avatar analogrelay avatar aspnetci avatar benaadams avatar brennanconroy avatar bricelam avatar chengtian avatar damianedwards avatar davidfowl avatar dougbu avatar eilon avatar halter73 avatar haok avatar hishamco avatar javiercn avatar jkotalik avatar juntaoluo avatar kichalla avatar kirthik avatar lodejard avatar mikeharder avatar natemcmaster avatar ntaylormullen avatar pakrym avatar pranavkm avatar ryanbrandenburg avatar rynowak avatar tratcher avatar troydai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

httpclientfactorylite's Issues

The system cannot find the file specified in Production

Hi @uhaciogullari, we need your help please.

Running in azure web app, we encountered weird issue when trying to create the httpClient, this happens all the time to a specific server only.

Error: The system cannot find the file specified | NetworkInformationException | at System.Net.NetworkInformation.SystemIPGlobalProperties.GetFixedInfo()
↵ at System.Net.NetworkInformation.SystemIPGlobalProperties.get_FixedInfo()
↵ at System.Net.NetworkInformation.SystemIPGlobalProperties.get_DomainName()
↵ at System.Net.CookieContainer..ctor()
↵ at System.Net.Http.HttpClientHandler..ctor()
↵ at HttpClientFactoryLite.DefaultHttpMessageHandlerBuilder..ctor()
↵ at HttpClientFactoryLite.HttpClientFactory.CreateHandlerEntry(String name)
↵ at HttpClientFactoryLite.HttpClientFactory.<>c__DisplayClass10_0.<.ctor>b__1()
↵ at System.Lazy1.CreateValue() ↵--- End of stack trace from previous location where exception was thrown --- ↵ at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() ↵ at System.Lazy1.get_Value()
↵ at HttpClientFactoryLite.HttpClientFactory.CreateHandler(String name)
↵ at HttpClientFactoryLite.HttpClientFactory.CreateClient(String name)

This is our code in creating the http client factory:

private const string HTTP_CLIENT_FACTORY_NAME = "test";

    private static Lazy<IHttpClientFactory> httpClientFactoryLazy = new Lazy<IHttpClientFactory>(() =>
    {
        var httpClientFactory = new HttpClientFactory();


        //Source: https://github.com/uhaciogullari/HttpClientFactoryLite
        httpClientFactory.Register(HTTP_CLIENT_FACTORY_NAME, builder => builder
            .AddHttpMessageHandler(() => new PolicyHttpMessageHandler())
            // Customize the primary HttpClientHandler
            .ConfigurePrimaryHttpMessageHandler(() =>
            {
                var httpClientHandler = new HttpClientHandler()
                {
                    //Allow Cookies to be set manually in the Headers. Source: https://stackoverflow.com/questions/12373738/how-do-i-set-a-cookie-on-httpclients-httprequestmessage
                    UseCookies = false
                };

                return httpClientHandler;
            })
            .SetHandlerLifetime(TimeSpan.FromMinutes(5)));

        return httpClientFactory;

    }, LazyThreadSafetyMode.ExecutionAndPublication);


    private static IHttpClientFactory httpClientFactory
    {
        get
        {
            return httpClientFactoryLazy.Value;
        }
    }

    private static HttpClient getHttpClient()
    {
        return httpClientFactory.CreateClient(HTTP_CLIENT_FACTORY_NAME);
    }

Azure Functions V2 ASP.Net Core 2.1 - Could not load type 'Microsoft.Extentions.Http.HttpClientFactory' from assembly

Describe the bug

I have an Azure Functions V2 - ASP.Net Core 2.1 project that creates a class in a .Net Standard project which uses HttpClientFactoryLite to create a new HttpClientFactory and it throws the following exception:

Could not load type 'Microsoft.Extensions.Http.HttpClientFactory' from assembly 'Microsoft.Extensions.Http, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

To Reproduce

Steps to reproduce the behavior:

  1. Create an Azure Functions V2 (ASP.Net Core 2.1) project
  2. Create a .Net Standard Class Library and add HttpClientFactoryLite NuGet.
  3. Create a basic Class in the Class Library and create a HttpClientFactory in the constructor
using Microsoft.Extensions.Http;
using System;

namespace ClassLibrary1
{
    public class Class1
    {
        public Class1()
        {
            var httpClientFactory = new HttpClientFactory();
        }
    }
}
  1. Create a simple timer function in the Functions project
using System;
using ClassLibrary1;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([TimerTrigger("*/15 * * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            try
            {
                var class1 = new Class1();
            }
            catch(Exception ex)
            {
                throw;
            }
        }
    }
}

  1. Run this and when stepping over the var class1 = new Class1(); line it will throw the following exception Could not load type 'Microsoft.Extensions.Http.HttpClientFactory' from assembly 'Microsoft.Extensions.Http, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

Suspected Cause

I suspect this is because HttpClientFactoryLite uses the same assembly name and namespaces as the actual Microsoft.Extensions.Http NuGet package that the Azure Functions V2 is using. That being version 2.2.0.0 and HttpClientFactoryLite DLL being 0.2.0.

Sample Solution that replicates the issue.

HttpClientFactoryLiteAzureFunctionIssue.zip

I couldn't agree with you more

Can this library be used directly like new .net?Because a lot of times we don't need dependency injection, like a generic library.Dependency injection is obviously unnecessary,
Is that all I need when I use it?
var httpClientFactory = new HttpClientFactory();

Cannot access a disposed object after the HandlerLifetime

Hi,

First of all, this library is great. It has a pure http only and no any dependencies which makes it good. We are encountering some issues when using this as a static and reusing it for our entire lifecycle of our application and web apps.

Using latest version from nuget, after the time has passed (depending on what you set in the Handlerlifetime), it will start giving Cannot access a disposed object when trying to access the same httpClient.

Here is the exception:

Cannot access a disposed object.
Object name: 'System.Net.Http.HttpClientHandler'. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Http.HttpClientHandler'.
at System.Net.Http.HttpClientHandler.CheckDisposed()
at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

We assume that the Factory auto disposed the httpClient and creates a new one so that it will refresh the DNS issue stuffs right?

We followed the instruction on how to use it, this is our code:

var httpClientFactory = new HttpClientFactory();

var httpClientHandler = new HttpClientHandler()
{
UseCookies = false
};

httpClientFactory.Register("test", builder => builder
.ConfigurePrimaryHttpMessageHandler(() => httpClientHandler)
.SetHandlerLifetime(TimeSpan.FromSeconds(30)));

then we make a singleton instance (static instance) of httpClientFactory, then we keep on reusing this method whenever we gonna make any rest api request:

var client = httpClientFactory.CreateClient("test");

var response = await client.SendAsync(request);

After sometime, the client.SendAsync will throw an exception: Cannot access a disposed object.

Please help.

HttpClientFactoryLite.dll is not strong-named

Describe the bug

  • HttpClientFactoryLite.dll is not strong-named
  • So it cannot be used from a project that is strong-named, as strong-named assemblies can only load other strong-named assemblies.

To Reproduce

Steps to reproduce the behavior:

  1. Create a new .NET Framework 4.8 Console project.
  2. Give that project a strong-name (VS > Project Properties).
  3. Add a NuGet reference to HttpClientFactoryLite.
  4. Instantiate a HttpClientFactoryLite.HttpClientFactory instance in your Main method.
  5. The project builds, but fails to run with this exception:

Error: System.IO.FileLoadException: Could not load file or assembly 'HttpClientFactoryLite, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044).

Expected behavior

I expected this assembly to be strong-named.

Screenshots

N/A

Additional context

N/A

I agree with you

Can this library be used directly like new .net?Because a lot of times we don't need dependency injection, like a generic library.Dependency injection is obviously unnecessary,
Is that all I need when I use it?
var httpClientFactory = new HttpClientFactory();

Nuget Package

Is it possible to have this library in a nuget package?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.