【发布时间】:2017-06-06 22:21:04
【问题描述】:
我写了一个 WCF RESTful 服务,它接受字符串和字节参数。问题是,如果字节为空白,Web 服务可以正常工作,但如果字节参数中有一个值,那么我会收到以下错误消息:
'反序列化 System.Byte[] 类型的对象时出错。应来自命名空间“”的结束元素“文档”。
这是我的代码
WCF 接口
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "IDocument")]
string IndexDocument(byte[] Document, string DocumentType);
WCF 接口实现
public string IndexDocument(byte[] Document, string DocumentType)
{
}
客户端程序
private class Documentt
{
public byte[] Document { get; set; }
public string DocumentType { get; set; }
}
static async Task RunAsync()
{
byte[] bytes = System.IO.File.ReadAllBytes(openFileDialog.FileName);
var parameters = new Documentt()
{
Document = bytes,
DocumentType = "AA"
};
using (HttpClient client = new HttpClient())
{
var request = new StringContent(JsonConvert.SerializeObject(parameters), Encoding.UTF8, "application/json");
var response = client.PostAsync(new Uri("http://localhost:59005/ServiceCall.svc/IDocument"), request);
var result = response.Result;
}
}
我在这里做错了什么?我想使用字节,因为我想编写一个跨平台(供 java、c++、c# 等使用)Web 服务。
【问题讨论】: