Wcf-Service Post方法(Json格式化)

时间:2014-05-09 13:18:07

标签: c# json wcf

我正在为我们的业务需求开发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();
}

1 个答案:

答案 0 :(得分:-1)

请查看此输入链接说明的答案 http://social.msdn.microsoft.com/Forums/vstudio/en-US/ef4cfee3-4d96-44a5-9fa1-ebafdff9bee3/wcf-stream-with-more-than-one-parameter

使用&#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;
        }
    }   
}