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

网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要介绍了.core 写快递100的快递物流信息查询接口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧

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

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

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

我们在startup里接着写

 services.AddScoped<KuaiDi100Helper>();

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

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<KuaiDi100Helper>(); //这样注入

这样就没问题了。

到此这篇关于.core 写快递100的快递物流信息查询接口的实现的文章就介绍到这了,更多相关.core 快递查询接口内容请搜索狼蚁SEO以前的文章或继续浏览狼蚁网站SEO优化的相关文章希望大家以后多多支持狼蚁SEO!

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by