在 Startup.cs 中键入客户端设置,无法获取注入服务中的信息

发布于 2025-01-09 17:24:03 字数 801 浏览 1 评论 0原文

我有一个演示 Azure 功能并按如下方式设置类型化客户端: 但我无法在注入的 httpclient 实例中获取基地址。有什么建议吗?感谢

            builder.Services.AddSingleton<IMyService, MyService>();//Comment out this line will make it work
            builder.Services.AddHttpClient<IMyService, MyService>(client =>
            {
                client.BaseAddress = new Uri("https://api.github.com/");
            });

        }

    public class MyService : IMyService
    {
        private readonly HttpClient httpClient;

        public MyService(HttpClient httpClient)
        {
            this.httpClient = httpClient;
        }

        public async Task LogMsg()
        {
            Console.WriteLine(httpClient.BaseAddress);
        }

更新: 删除 AddSignleton 行将起作用... 见评论里的链接

I have a demo Azure function and setup the typed client as follow:
But I can't get base address in injected httpclient instance. Any advice? Thanks

            builder.Services.AddSingleton<IMyService, MyService>();//Comment out this line will make it work
            builder.Services.AddHttpClient<IMyService, MyService>(client =>
            {
                client.BaseAddress = new Uri("https://api.github.com/");
            });

        }

    public class MyService : IMyService
    {
        private readonly HttpClient httpClient;

        public MyService(HttpClient httpClient)
        {
            this.httpClient = httpClient;
        }

        public async Task LogMsg()
        {
            Console.WriteLine(httpClient.BaseAddress);
        }

Updates:
Remove the AddSignleton line will work...
See link in comment

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2025-01-16 17:24:03

无法获取注入的httpclient实例中的基地址

尝试删除配置部分中的AddSingleton()注册。

public class Startup : FunctionsStartup
   {
       public override void Configure(IFunctionsHostBuilder builder)
       {
           builder.Services.AddScoped<MyService>();
           builder.Services.AddScoped<Customlog>();
           //Removed AddSingleton in service registration
              //builder.Services.AddSingleton<MyServiceshttp>()    
              builder.Services.AddHttpClient<MyServicehttp>(client =>
           {
               // this code never runs
               client.BaseAddress = new Uri("https://api.github.com/");
           });
       }
   }

如果您想使用单例,请尝试将 HttpClientFactory 注入您的 Service 实例中,例如 (MyServiceshttp)。

请参阅此处

can't get the base address in injected httpclient instance

Try to remove the AddSingleton<MyServicehttp>() Registration in Configuration Portion.

public class Startup : FunctionsStartup
   {
       public override void Configure(IFunctionsHostBuilder builder)
       {
           builder.Services.AddScoped<MyService>();
           builder.Services.AddScoped<Customlog>();
           //Removed AddSingleton in service registration
              //builder.Services.AddSingleton<MyServiceshttp>()    
              builder.Services.AddHttpClient<MyServicehttp>(client =>
           {
               // this code never runs
               client.BaseAddress = new Uri("https://api.github.com/");
           });
       }
   }

If you want to use the singleton, try to inject HttpClientFactory inside your Service instance like (MyServiceshttp).

Refer here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文