Realization of Express Logistics Information Query Interface Based on. netcore Express 100

  • 2021-11-29 06:40:48
  • OfStack

The logistics information inquiry interface of Express 100 officially provides 1 demo;; Fortunately, the code provided by the official is. netcore version, but it is written a little low;; According to the official code, I reconstructed the code according to the style of. netcore; The core code is as follows:


/// <summary>
    ///  Muxue Weitao Express 100 Help class .
    /// </summary>
    public class KuaiDi100Helper
    {
        private ILogger _logger;
        private MuXueConfigHelper _configHelper;
        HttpClient _client;
 
        /// <summary>
        ///  Express delivery 100 Help class 
        /// </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>
        ///  Real-time express inquiry interface 
        /// </summary>
        /// <param name="tenant_id"></param>
        /// <param name="shop_code"></param>
        /// <param name="com"> The code of the courier company inquired,  1 Lowercase letters for law </param>
        /// <param name="num"> Query express order number,   The maximum length of a single number is 32 Characters </param>
        /// <param name="phone"> Telephone number of sender and receiver (mobile phone and fixed telephone can be filled in only 1 A, SF odd number is required, and other express delivery companies choose to fill in. If the landline number has an extension number, the extension number does not need to be uploaded.) </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, $" Express delivery 100 Real-time express query interface exception: {ex.Message}");
                return null;
            }
            return result;
        }
 
 
    }

When you look at the above code 1, you know that you must use dependency injection; We see that the HttpClient _ client thing is used in the constructor; (Because you want to call the interface of Express 100),

We go on to write in startup:


 services.AddScoped<KuaiDi100Helper>();

The above code should be the most commonly used registration method; The result is an error, and the error message is as follows:

System. AggregateException: "Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MuXue. WeTao. Mall. Core. kuaidi100. KuaiDi100Helper Lifetime: MuXue. WeTao. Mall. kuaidi100. KuaiDi 100Helper' Unable EN.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'.

According to the error message, there should be something wrong with httpclient; It took a long time to find a solution to modify the registration method in startup


services.AddHttpClient<KuaiDi100Helper>(); // Inject like this 

So there will be no problem.


Related articles: