小编给大家分享一下.netcore如何写快递100的快递物流信息查询接口,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
快递100的物流信息查询接口,官方提供了一些demo;还好官方提供的代码是.netcore版本写的,不过写的有点low;根据官方提供的代码,我按照.netcore 的风格重构了代码;核心代码如下:
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的快递物流信息查询接口”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。