.netcore 写快递100的快递物流信息查询接口的实现

快递100的物流信息查询接口,官方提供了一些demo;还好官方提供的代码是.netcore版本写的,不过写的有点low;根据官方提供的代码,我按照.netcore 的风格重构了代码;核心代码如下:

/// 
    /// 沐雪微淘快递100帮助类.
    /// 
    public class KuaiDi100Helper
    {
        private ILogger _logger;
        private MuXueConfigHelper _configHelper;
        HttpClient _client;
 
        /// 
        /// 快递100帮助类
        /// 
        /// 
        /// 
        public KuaiDi100Helper(ILogger logger, HttpClient client, MuXueConfigHelper configHelper)
        {
            _configHelper = configHelper;
            _logger = logger;
            _client = client;
        }
 
        /// 
        /// 实时快递查询接口
        /// 
        /// 
        /// 
        /// 查询的快递公司的编码, 一律用小写字母
        /// 查询的快递单号, 单号的最大长度是32个字符
        /// 收、寄件人的电话号码(手机和固定电话均可,只能填写一个,顺丰单号必填,其他快递公司选填。如座机号码有分机号,分机号无需上传。)
        /// 
        public async Task QueryTrack(long tenant_id, string shop_code,string com,string num,string phone="")
        {
            QueryTackResult result = new QueryTackResult();
            try
            {
 
                TenantConfig config = await _configHelper.GetTenantAllAsync(tenant_id, shop_code);
 
                QueryTrackParam queryTrackParam = new QueryTrackParam();
                if (com== "shunfeng")
                {
                      queryTrackParam = new QueryTrackParam()
                    {
                        com = com,
                        num = num,
                        phone = phone
                    };
                }
                else
                {
                      queryTrackParam = new QueryTrackParam()
                    {
                        com = com,
                        num = num, 
                    };
                }
               
 
                QueryTrackReq query = new QueryTrackReq()
                {
                    customer = config.KuaiDi100CustomerID,
                    sign = SignUtils.GetMD5(queryTrackParam.ToString() + config.KuaiDi100Key + config.KuaiDi100CustomerID),
                    param = queryTrackParam
                };
                var requestParam = ObjectToDictionaryUtils.ObjectToMap(query);
                if (requestParam == null)
                {
                    return null;
                }
                 
                 result = await HttpClientHelper.PostFormAsync(_client, ApiInfoConstant.QUERY_URL, requestParam);
                 
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"快递100实时快递查询接口异常:{ex.Message}");
                return null;
            }
            return result;
        }
 
 
    }

上面的代码一眼看,就知道必须要使用依赖注入;我们看到 在构造函数里使用了HttpClient _client    这个东西;(因为要调用快递100的接口),

我们在startup里接着写:

 services.AddScoped();

如上代码应该是最常用的注册方法;结果报错,错误信息如下:

System.AggregateException:“Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MuXue.WeTao.Mall.Core.kuaidi100.KuaiDi100Helper Lifetime: Scoped ImplementationType: MuXue.WeTao.Mall.Core.kuaidi100.KuaiDi100Helper': Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'MuXue.WeTao.Mall.Core.kuaidi100.KuaiDi100Helper'.)”
 
 
InvalidOperationException: Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'MuXue.WeTao.Mall.Core.kuaidi100.KuaiDi100Helper'.

根据错误信息看,应该是httpclient出了问题了;找了很久才找到解决方法,修改startup里的注册方法

services.AddHttpClient(); //这样注入

这样就没问题了。

到此这篇关于.netcore 写快递100的快递物流信息查询接口的实现的文章就介绍到这了,更多相关.netcore 快递查询接口内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(.netcore 写快递100的快递物流信息查询接口的实现)