簡體   English   中英

使用ServiceStack客戶端POST的序列化對象為null

[英]Serialized object POST'd using the ServiceStack client is null

我正在使用服務堆棧在C#中構建一個restful服務。

這是我的服務實現。

namespace cloudfileserver
{   
    [Route("/updatefile", "POST")]
    public class UpdateFile
    {
        public UserFile file { get; set; }
    }

    public class CloudFileService : Service
    {
        public InMemoryFileSystem filesystem { get; set; }
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger (typeof(CloudFileService));

        public void Post (UpdateFile request)
        {
            try{
                logger.Debug("File received is :"  + request.file);
                filesystem.addFileSynchronized (request.clientId, request.file);
            }catch(Exception e){
                logger.Debug(e);
                throw e;
            }
        }
    }
}

我通過以下servicestack客戶端代碼調用此服務:

JsonServiceClient client  = new JsonServiceClient(ENDPOINT);
UpdateFile arg = new UpdateFile();
UserFile file = new UserFile ("x.txt", "piyush");
file.SetFileContent (getByteArrayFromString ("Filecontent"), 0);
arg.file = file;
client.Post<Object>("/updatefile", arg);

問題在於,每當我通過上述客戶端代碼進行調用時,在服務器端收到的文件對象為NULL(我通過將其寫入日志文件來驗證)。

這是通過網絡發送File File類是可serialisable因為我可以通過GET調用正確發送它。

知道這里可能出了什么問題嗎?

我已經測試了您的方案,無法重現您遇到的問題。 我建議你驗證你的SetFileContent方法和getByteArrayFromString方法,因為你可能有錯誤。 隨意發布該實現。

除此之外,如果您捕獲了對服務器發出的HTTP請求,以確定問題所在。

下面是自托管ServiceStack v4測試應用程序的完整工作源代碼,它使用您概述的實現,我希望這會有所幫助。

using System;
using ServiceStack;
using System.Collections.Generic;

namespace v4
{
    class MainClass
    {
        // Assumed implementation of your getByteArrayFromString
        static byte[] getByteArrayFromString(string path)
        {
            return System.IO.File.ReadAllBytes(path);
        }

        public static void Main()
        {
            // Simple Self-Hosted Console App
            var appHost = new AppHost(500);
            appHost.Init();
            appHost.Start("http://*:9000/");

            // Test the service
            string filename = "image.png";
            JsonServiceClient client  = new JsonServiceClient("http://localhost:9000");

            // Create and set the file contents
            UserFile file = new UserFile (filename, "username");
            file.SetFileContent(getByteArrayFromString(filename), 0);

            // Post the file
            client.Post<Object>("/updatefile", new UpdateFile { file = file } );

            Console.ReadKey();
        }
    }

    public class AppHost : AppHostHttpListenerPoolBase
    {
        public AppHost(int poolSize) : base("Test Service", poolSize, typeof(TestService).Assembly) { }
        public override void Configure(Funq.Container container) { }
    }

    [Route("/updatefile","POST")]
    public class UpdateFile
    {
        public UserFile file { get; set; }
    }

    [Serializable]
    public class UserFile 
    {

        public string filepath { get; set;}
        public string owner { get; set;}
        public byte[] filecontent { get; set;}
        public long filesize { get; set;}
        public List<string> sharedwithclients { get; set;}
        public long versionNumber {get;set;}
        object privateLock = new object();

        public UserFile (string filepath, string owner)
        {      
            this.filepath = filepath;
            this.owner = owner;
            versionNumber = -1;
            filesize = 0;
            filecontent = new byte[0];
            sharedwithclients = new List<string>();
        }

        public void SetFileContent(byte[] contents, long version)
        {
            filecontent = contents;
            filesize = filecontent.Length;
            versionNumber = version;
        }
    }

    public class TestService : Service
    {
        public void Post(UpdateFile request)
        {
            // Request is populated correctly i.e. not null.
            Console.WriteLine(request.file.ToJson());
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM