xamarin System.NotSupportedException:... API HTTPPost与.net和C#的结合使用

sz81bmfz  于 2022-12-07  发布在  .NET
关注(0)|答案(1)|浏览(210)

I am an engineering student, doing my final degree project based on xamarin apps, including a connection between the client (xamarin) and API (.net). I am trying to send some encrypted data (in base64 encoding) included on a json object as the Request. On the side of the API, it takes the json request, does some fully homomorphic encryption functions and returns the response within new encrypted information.
The problem is when I am trying to receive the response in API as a self-created class named "Post.cs" which includes the next properties;

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;
        }

So, my API takes the request and deserialize it in order to create a "Post" and do some stuff with it. I am trying to reproduce HttpPost as follows:

[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;
         }
}

So, at the time I post the "Post" from the client on xamarin, I am sending a Post as the already mentioned structure, where userIdEncrypted and parmsStr contains a base64 encoded string. When it arrives to the API server, the following issue appears:

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 ---

CLIENT ON XAMARIN APP This is the json string that I post from the client:

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);
...

In which PostModel refers to this self-created Model:

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; }

    }

I am aware of my inexperience programming on .Net and c#, so any help and explanations are welcome.
Regards, Raul.

mitkmikd

mitkmikd1#

这是一个罕见的错误消息,告诉你确切问题是什么
不支持对没有无参数构造函数、单个参数化构造函数或用“JsonConstructorAttribute”注解的参数化构造函数的类型进行反序列化。
您需要将默认构造函数添加到Post

public Post() {}

相关问题