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

/// <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="com">查询的快递公司的编码, 一律用小写字母</param>
        /// <param name="num">查询的快递单号, 单号的最大长度是32个字符</param>
        /// <param name="phone">收、寄件人的电话号码(手机和固定电话均可,只能填写一个,顺丰单号必填,其他快递公司选填。如座机号码有分机号,分机号无需上传。)</param>
        /// <returns></returns>
        public async Task<QueryTackResult> 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<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>(); //这样注入

这样就没问题了。

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

点赞(0)

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部