Json запрос на asp. net ядро ​​3.1 - PullRequest
0 голосов
/ 15 января 2020

Я пытаюсь ie передать мой предмет Reach_DeclarationB c

    public class Reach_DeclarationBc
    {
        public int ArticleId { get; set; }
        public DateTime DateDeclaration { get; set; }
    }

Я использую этот код для вызова моего API

var client = new HttpClient();

client.BaseAddress = new Uri("http://localhost:3001/");
Reach_DeclarationBc reach_DeclarationBc = new Reach_DeclarationBc
{
    ArticleId = 129,
    DateDeclaration = DateTime.Now
};

Reach_DeclarationBc result = await client.PostJsonAsync<Reach_DeclarationBc>("http://localhost:3009/reach", reach_DeclarationBc);

Но эта строка дает мне ошибка

Reach_DeclarationBc result = await client.PostJsonAsync<Reach_DeclarationBc>("http://localhost:3009/reach", reach_DeclarationBc);

Ошибка: «TypeLoadException: не удалось загрузить тип« Microsoft.JSInterop. Json »из сборки» Microsoft.JSInterop, версия = 3.1.0.0, культура = нейтральная, PublicKeyToken = adb9793829ddae60 '. "

Использование в моем классе

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Projet_Airbus.Data;
using Projet_Airbus.Models;
using Projet_Airbus.Models.Declaration;
using Microsoft.AspNetCore.Blazor;  
using System.Net.Http;  

Для решения я использую asp. net core 3.1, но это не работает

1 Ответ

0 голосов
/ 15 января 2020

Вот как я обычно это делаю.

  1. Я внедряю синглтон HttpClient и регистратор с помощью инжектора зависимостей, например Ninject .
  2. Я создаю шаблон c SendAsync, который может обрабатывать PUT / DELETE / POST / GET.
  3. Я создаю ApiResponse класс, который содержит интересующие меня свойства.
  4. Я создаю Request класс (InitRequest), который я использую для запроса.
  5. Я создаю Response класс (InitResponse), который я использую для ответа.
  6. У меня есть TimeOutInMs для установки времени ожидания вызова API.
  7. У меня logger, что регистрирует ошибку.
    using System;
    using System.Net;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web.Mvc;
    using Newtonsoft.Json;

    public class HomeController : Controller
    {
        private readonly WebApi _webApi;
        //Inject singleton httpclient and logger
        public HomeController(HttpClient httpClient, ILogger logger)
        {
            _webApi = new WebApi(httpClient, logger);
            _webApi.BaseAddress = "https://www.webapi.com/";
            _webApi.TimeOutInMs = 2000;
        }

        public async Task<ActionResult> Index()
        {
            //You might want to move the web api call to a service class
            var method = "init";
            var request = new InitRequest
            {
                Id = 1,
                Name = "Bob"
            };

            var response = await _webApi.SendAsync<InitResponse>(method, request, HttpMethod.Post);
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var viewModel = response.Data.ToIndexViewModel();
            }
            else
            {
                //Handle Error
            }

            return View(viewModel);
        }
    }

    public class IndexViewModel
    {
        public string Name { get; set; }
        public string Address { get; set; }
    }

    public static class ModelMapper
    {
        public static IndexViewModel ToIndexViewModel(this InitResponse response)
        {
            return new IndexViewModel
            {
                Name = response.Name,
                Address = response.Address
            };
        }
    }

    public class InitRequest
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class InitResponse
    {
        public string Name { get; set; }
        public string Address { get; set; }
    }

    public class WebApi
    {

        private readonly HttpClient _httpClient;
        private readonly ILogger _logger;
        public Uri BaseAddress { get; set; }
        public int TimeOutInMs { get; set; }

        public WebApi(HttpClient httpClient, ILogger logger)
        {
            _logger = logger ?? throw new Exception($"Missing constructor ceference - {nameof(logger)} can not be null!");
            _httpClient = httpClient ?? throw new Exception($"Missing constructor ceference - {nameof(httpClient)} can not be null!");
        }

        public async Task<ApiResponse<TOut>> SendAsync<TOut>(string method, object param, HttpMethod httpMethod)
        {
            if (string.IsNullOrWhiteSpace(BaseAddress.ToString()))
                throw new Exception($"{nameof(BaseAddress)} can not be null or empty.");


            if (string.IsNullOrWhiteSpace(method))
                throw new Exception($"{nameof(method)} can not be null or empty.");

            var paramListForLog = JsonConvert.SerializeObject(param);

            //Set timeout
            if (TimeOutInMs <= 0)
            {
                TimeOutInMs = (int)TimeSpan.FromSeconds(100.0).TotalMilliseconds;
            }

            var cts = new CancellationTokenSource();
            cts.CancelAfter(TimeSpan.FromMilliseconds(TimeOutInMs));

            var cancellationToken = cts.Token;

            var url = new Uri($"{BaseAddress}{method}", UriKind.Absolute);

            try
            {

                HttpResponseMessage response;
                using (var request = new HttpRequestMessage(httpMethod, url))
                {
                    //Add content
                    if (param != null)
                    {
                        var content = JsonConvert.SerializeObject(param);
                        request.Content = new StringContent(content, Encoding.UTF8, "application/json");
                    }
                    //Add headers
                    request.Headers.Accept.Clear();
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    _logger.Info($"Calling {httpMethod} for {url}", paramListForLog);
                    //Send the request
                    response = await _httpClient.SendAsync(request, cancellationToken);
                }

                //If success
                if (response.IsSuccessStatusCode)
                {
                    _logger.Info($"Successfully called {httpMethod} for {url}", paramListForLog);
                    var data = await response.Content.ReadAsAsync<TOut>(cancellationToken);

                    return new ApiResponse<TOut>
                    {
                        StatusCode = response.StatusCode,
                        Data = data
                    };
                }

                //If failure
                var error = await response.Content.ReadAsStringAsync();
                _logger.Error($"An error occured calling {httpMethod} for {url}. Error was {error}", paramListForLog);
                return new ApiResponse<TOut>
                {
                    StatusCode = response.StatusCode,
                    Message = error
                };
            }
            //If timeout
            catch (OperationCanceledException ex)
            {
                var message = cancellationToken.IsCancellationRequested ?
                    $"Request timed out after {TimeOutInMs} ms occured calling {httpMethod} for {url}. Error was: {ex.Message}" :
                    $"An error occured calling {httpMethod} for {url}. Error was: {ex.Message}";

                var webEx = new Exception(message, ex);
                _logger.Error(webEx, webEx.Message, paramListForLog);

                return new ApiResponse<TOut>
                {
                    StatusCode = HttpStatusCode.RequestTimeout,
                    Message = message
                };
            }
            //If unknown error
            catch (Exception ex)
            {
                var webEx = new Exception($"An error occured calling {httpMethod} for {url}. Error was: {ex.Message}", ex);
                _logger.Error(webEx, webEx.Message, paramListForLog);
                throw webEx;
            }
        }
    }
    public interface ILogger
    {
        void Info(string message, string param);
        void Error(string message, string param);
        void Error(Exception e, string message, string param);
    }

    public class ApiResponse<T>
    {
        public HttpStatusCode StatusCode { get; set; }
        public string Message { get; set; }
        public T Data { get; set; }
    }
...