我正在使用服务堆栈在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(我通过将其写入日志文件来验证)。
正在通过电汇发送的 Here is the File
class。
File
课程为serialisable
,因为我可以通过GET
电话正确发送。{/ p>
知道这里可能出现什么问题吗?
答案 0 :(得分:2)
我已经测试了您的方案,无法重现您遇到的问题。我建议您验证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());
}
}
}