【问题标题】:System.NotSupportedException: ... API HTTPPost with .net and c#System.NotSupportedException: ... API HTTPPost 与 .net 和 c#
【发布时间】:2022-09-27 19:14:44
【问题描述】:

我是一名工科学生,基于 xamarin 应用程序完成我的最终学位项目,包括客户端 (xamarin) 和 API (.net) 之间的连接。我正在尝试将 json 对象中包含的一些加密数据(以 base64 编码)作为请求发送。在 API 方面,它接受 json 请求,执行一些完全同态的加密功能,并在新的加密信息中返回响应。

问题是当我试图在 API 中接收作为名为 \"Post.cs\" 的自创建类的响应时,其中包括下一个属性;

public class Post
    {
        public ulong ? userId { get; set; }
        public int ? id { get; set; }
        public string? title { get; set; }
        public string? body { get; set; }
        public string? userIdEncrypted { get; set; }
        public string? userIdEncryptedReturned { get; set; }
        public string? parmsStr { get; set; }

        
        public Post(ulong? userId, int? id, string? title, string? body, string? userIdEncrypted, string? userIdEncryptedReturned, string? parmsStr)
        {
            this.userId = userId;
            this.id = id;
            this.title = title;
            this.body = body;
            this.userIdEncrypted = userIdEncrypted;
            this.userIdEncryptedReturned = userIdEncryptedReturned;
            this.parmsStr = parmsStr;
        }

因此,我的 API 接受请求并对其进行反序列化,以创建一个 \"Post\" 并用它做一些事情。 我正在尝试复制 HttpPost,如下所示:

[Route(\"api/[controller]\")]
    [ApiController]
    public class PostController
    {
        
        #region CONSTRUCTOR
        
        public PostController()
        {
        }
        
        #endregion
        

        //POST ://api/Post
        [Route(\"api/[controller]\")]
        [HttpPost]
        public Post ReceivePost([FromBody] Post post)
        {
                ...
           var _post = new Post(post.userId, post.id, post.title, post.body, post.userIdEncrypted
            post.userIdEncryptedReturned, post.parmsStr);
        
  ... FHE functions...
            return _post;
         }
}

因此,当我在 xamarin 上从客户端发布 \"Post\" 时,我正在发送一个 Post 作为已经提到的结构,其中 userIdEncrypted 和 parmsStr 包含一个 base64 编码的字符串。当它到达 API 服务器时,会出现以下问题:

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with \'JsonConstructorAttribute\' is not supported. Type \'System.IO.Stream\'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
       ---> System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with \'JsonConstructorAttribute\' is not supported. Type \'System.IO.Stream\'.
         --- End of inner exception stack trace ---

XAMARI 应用程序的客户端这是我从客户端发布的 json 字符串:

PostModel postAux = new PostModel()
            {
                userId = 2,
                id = 1,
                title = \"Title for post 1\",
                body = \"Body for post 1\",

            };

            

            

            /******************************************************
             * Encrypt data of the post (userId)
             ******************************************************/

            PostModel newPost = initializeFHE(postAux);
            //Here is where I fill the encrypted data (base64 string) included in the Post object


            /******************************************************
             * POST encrypted data to the server in csharp
             ******************************************************/


            Uri requestUri = new Uri(\"http://myHost:3000/api/Post\");
            var client = new HttpClient();
            
            client.DefaultRequestHeaders
                  .Accept
                  .Add(new MediaTypeWithQualityHeaderValue(\"application/json\")); // ACCEPT header

            var json = JsonConvert.SerializeObject(newPost);

            var contentJson = new StringContent(json.ToString(), Encoding.UTF8, \"application/json\");
            //Console.WriteLine(contentJson);
            var response = await client.PostAsync(requestUri, contentJson);
...

其中 PostModel 指的是这个自建模型:

public class PostModel : BaseViewModel
    {
        public ulong userId { get; set; }
        public int id { get; set; }
        public string  title { get; set; }
        public string  body { get; set; }
        public string  userIdEncrypted { get; set; }
        public string  userIdEncryptedReturned { get; set; }
        public string  parmsStr { get; set; }

    }

我知道我在 .Net 和 c# 上的编程经验不足,因此欢迎任何帮助和解释。

问候, 劳尔。

    标签: c# asp.net .net xamarin seal


    【解决方案1】:

    这是告诉您的罕见错误消息之一确切地问题是什么

    不支持对没有无参数构造函数、单一参数化构造函数或带有“JsonConstructorAttribute”注释的参数化构造函数的类型进行反序列化。

    您需要将默认构造函数添加到Post

    public Post() {}
    

    【讨论】:

      【解决方案2】:

      尝试了一些不同的方法,现在接受构造函数,但出现了另一个问题:

          [Route("api/[controller]")]
          [ApiController]
          public class PostController 
          {
             
              public ulong ? _userId { get; set; }
              public int ? _id { get; set; }
              public string? _title { get; set; }
              public string? _body { get; set; }
              public string? _userIdEncrypted { get; set; }
              public string? _userIdEncryptedReturned { get; set; }
              public string? _parmsStr { get; set; }
      
              [JsonConstructor]
              public PostController(ulong? userId, int? id, string? title, string? body, string? userIdEncrypted, string? userIdEncryptedReturned, string? parmsStr)
              {
                  _userId = userId;
                  _id = id;
                  _title = title;
                  _body = body;
                  _userIdEncrypted = userIdEncrypted;
                  _userIdEncryptedReturned = userIdEncryptedReturned;
                  _parmsStr = parmsStr;
              }
              
              
              
              //POST ://api/Post
              [Route("api/[controller]")]
              [HttpPost]
              public PostController ReceivePost([FromBody] PostController req)
              {
                  /*************************************************************
                   * Recuperación de los parámetros de cifrado del objeto JSON
                   *************************************************************/
      
                  SEALContext context;
                  Evaluator evaluator;
                  
                 
                 var auxPost = JsonConvert.DeserializeObject<PostController>(req.ToString());
                 var _post = new PostController(auxPost._userId, auxPost._id, auxPost._title, auxPost._body, auxPost._userIdEncrypted, auxPost._userIdEncryptedReturned, auxPost._parmsStr);
                 Console.WriteLine(_post);
                  
      
                  
                  try{ ...
      

      出现此错误:

      System.InvalidOperationException: Unable to resolve service for type 'System.Nullable`1[System.UInt64]' while attempting to activate 'apiFHE.Controllers.PostController'.
      

      有谁知道如何解决这个问题?

      先感谢您, 劳尔。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-29
        • 1970-01-01
        • 1970-01-01
        • 2012-05-12
        • 2022-07-27
        • 1970-01-01
        相关资源
        最近更新 更多