简体   繁体   English

wcf rest json返回集合

[英]wcf rest json return collection

I am attempting to build a Restful WCF Service which returns data in JSON format. 我正在尝试构建一个Restful WCF服务 ,该服务JSON格式返回数据。 My firsts methods work fine but when I try return a collection my test program receive the next exception: 我的firsts方法工作正常,但是当我尝试返回一个集合时,我的测试程序会收到下一个异常:

Unable to write data to the transport connection. 无法将数据写入传输连接。 An existing connection was forcibly closed by the remote host. 远程主机强行关闭了现有连接。

My Service code: 我的服务代码:

[ServiceContract]
public interface IService
{ 
  [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetModes")]
    OGetModesResponse OGetModes(OGetModesRequest oGetModes);
}
[DataContract]
public class OGetModesRequest
{
    private String m_sTicket;

    [DataMember]
    public String prTicket
    {
        get { return m_sTicket; }
        set { m_sTicket = value; }
    }
}

[DataContract]
public class OGetModesResponse
{
    [DataMember]
    public string sTicket;
    [DataMember]
    public emStatus emStatus;
    [DataMember]
    public IList<CTMode> aoModes;
}

And my test program: 而我的测试程序:

OGetModesRequest oGetModes = new OGetModesRequest { prTicket = sTicket };
ser = new DataContractJsonSerializer(typeof(OGetModesRequest));
mem = new MemoryStream();
ser.WriteObject(mem, oGetModes);
webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
//Exception here
bData = webClient.UploadData("http://localhost:26104/Service.svc/GetModes", "POST", mem.ToArray()); 
stream = new MemoryStream(bData);
obj = new DataContractJsonSerializer(typeof(OGetModesResponse));
OGetModesResponse OResultModes = obj.ReadObject(stream) as OGetModesResponse;

I debug my services and works fine. 我调试了我的服务,并且工作正常。 What can be happening? 会发生什么事?

Thanks for help. 感谢帮助。

Edit (solution): CTMode is a class used by managing object that I obtain using NHibernate so I create a new class serializable called CMode 编辑(解决方案):CTMode是管理对象使用的类,我使用NHibernate获取该类,因此我创建了一个可序列化的新类,称为CMode

[DataContract]
public class OGetModesResponse
{
    [DataMember]
    public string sTicket;
    [DataMember]
    public emStatus emStatus;
    [DataMember]
    public IList<CMode> aoModes;
}
[Serializable]
public class CMode
{
    public Int32 nId;
    public Int32 nCode;
    public String sName;
}

Try to check inner exception and add some logging/ trace on the server. 尝试检查内部异常并在服务器上添加一些日志记录/跟踪。

There are few possibilities for your (generic) error as you may not be aware about inner exception: 您(一般)错误的可能性很小,因为您可能不了解内部异常:

  • object CTMode is missing DataContract, DataMember attribute. 对象CTMode缺少DataContract,DataMember属性。
  • object CTMode is an enum that missing attributes or has incorrect value that cannot be serialized 对象CTMode是缺少属性或具有无法序列化的不正确值的枚举
  • previous connection is not closed correctly 先前的连接未正确关闭
  • there is a proxy server on the way and you need to bypassed it 途中有一个代理服务器,您需要绕过它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM