.netCoreWebAPI中, 使用IHttpClientFactory 避免端口号被耗尽

using MyFrontCodeTest.Models;
using MyFrontCodeTest.Models.PLC;
using MyFrontCodeTest.Service;
using System.Net.Http;
using System.Text.Json;

namespace MyFrontCodeTest.Utils
{
    public class StartupInitializationService : IHostedService
    {
        private readonly MyS7Entry myS7Entry;
        private readonly IS7ConnService s7ConnService;
        private readonly ILogger<StartupInitializationService> logger;
        private readonly HttpClient httpClient;
        public StartupInitializationService(MyS7Entry myS7Entry
            , IS7ConnService s7ConnService
            , IHttpClientFactory httpClientFactory
            , ILogger<StartupInitializationService> logger
            )
        {
            this.myS7Entry = myS7Entry;
            this.s7ConnService = s7ConnService;
            this.logger = logger;
            this.httpClient = httpClientFactory.CreateClient();
        }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            //try
            //{
            //    s7ConnService.ConnPlc();
            //}
            //catch (Exception ex)
            //{
            //    logger.LogError($"网络错误:{ex.Message}");
            //    return Task.CompletedTask;
            //}


            //myS7Entry.PropertyChanged += MyS7Entry_PropertyChanged;
            logger.LogWarning("初始化函数成功");

            myS7Entry.PropertyChanged += async (s, e) =>
            {
                if (e.PropertyName == "MyBool1")
                {
                    //目标服务器Api地址
                    string? url = "http://127.0.0.1:8881/endpoint/mes/kx/reportA";
                    Student stu = new()
                    {
                        Id = 1,
                        Name = "MyBool1",
                        Age = 999
                    };
                    var json = JsonSerializer.Serialize(stu);
                    var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
                    var response = await httpClient.PostAsync(url, content);
                    if (response.IsSuccessStatusCode)
                    {
                        logger.LogInformation("Data sent successfullyS1.");
                    }
                    else
                    {
                        logger.LogInformation("Failed to send dataS1.");
                    }
                }

                if (e.PropertyName == "MyShort2")
                {

                }
            };

            return Task.CompletedTask;
        }

        private void MyS7Entry_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "MyBool1")
            {

            }

            if (e.PropertyName == "MyBool2")
            {

            }
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            // 在应用程序停止时执行清理逻辑(如果有必要)
            return Task.CompletedTask;
        }
    }
}

builder.Services.AddHttpClient("testClient", (client) =>
{
    client.BaseAddress = new Uri("http://127.0.0.1:8889");
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值