.netcore 寫快遞100的快遞物流資訊查詢介面

沐雪小程式開發者發表於2021-04-22

快遞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>(); //這樣注入

這樣就沒問題了。

 

相關文章