我正在为我们的业务需求开发wcf休息服务。但是当我使用Web Invoke方法进行POST时,我收到了一个错误。如果我使用stream作为输入参数,则不会发生该错误。我的问题是如何使用我自己定义的类而不是流为Json类型数据。请提供相同的代码示例。
以下是我的代码示例
接口:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "Addnewclaim")]
Addclaimreport Addnewclaim(Stream JSONData);
Inplementation:
public Addclaimreport Addnewclaim(Stream JSONData)
{
StreamReader read = new StreamReader(JSONData);
string data = read.ReadToEnd();
read.Close();
read.Dispose();
object yourOjbect = new JavaScriptSerializer().DeserializeObject(data);
Dictionary<string, object> accident_details = (Dictionary<string, object>)((Dictionary<string, object>)yourOjbect)["Accident_Details"];
Dictionary<string, object> Driver_details = (Dictionary<string, object>)((Dictionary<string, object>)yourOjbect)["Drv_Details"];
Dictionary<string, object> Genaral_details = (Dictionary<string, object>)((Dictionary<string, object>)yourOjbect)["Drv_Details"];
string Acc_dtls = Genaral_details["Accident_Details"].ToString();
return new Addclaimreport();
}
答案 0 :(得分:-1)
使用&#34;代码&#34;下次功能,因为你的帖子不容易理解!
找到解决方案来自:http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP
[ServiceContract]
public interface ITransferService
{
[OperationContract]
RemoteFileInfo DownloadFile(DownloadRequest request);
[OperationContract]
void UploadFile(RemoteFileInfo request);
}
[MessageContract]
public class DownloadRequest
{
[MessageBodyMember]
public string FileName;
}
[MessageContract]
public class RemoteFileInfo : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public long Length;
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}