【问题标题】:Wcf returning ususual responseWcf 返回正常响应
【发布时间】:2015-02-06 22:45:55
【问题描述】:

我正在尝试返回一个列表

public List<tblcourse> GetData(string value)
{
    testEntities1 db = new testEntities1();

    int v = Convert.ToInt32(value);
    testEntities1 t = new testEntities1();
    var u = (from g in t.tblcourses
             select new  { g.C_Id,  g.C_Name }).ToList();

    List<tblcourse> lisstt = new List<tblcourse>();

    foreach (var item in u)
    {
        tblcourse b = new tblcourse();
        b.C_Id = item.C_Id;
        b.C_Name = item.C_Name;
        lisstt.Add(b);
    }

    return lisstt;
}

当我使用此服务时,从客户端,当我执行客户端代码时,我会从服务中得到以下响应

string strServiceUrl1 = "htttp://localhost:1967/Service1.svc/GetData/" + id;

HttpWebRequest objHttpWebRequest1 = WebRequest.Create(strServiceUrl1) as HttpWebRequest;
objHttpWebRequest1.Method = "GET";
objHttpWebRequest1.ContentType = "application/json-urlencoded";

StreamReader onjStreamReader1 = new StreamReader(objHttpWebRequest1.GetResponse().GetResponseStream());
string strResponse1 = onjStreamReader1.ReadToEnd().ToString();

List<Course> cc = JsonConvert.DeserializeObject<List<Course>>(strResponse1);
return View(cc);

以下是我收到的response 的回复,因此我无法将deserialize 回复到列表中。

<ArrayOftblcourse xmlns="htttp://schemas.datacontract.org/2004/07/app2" 
                  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <tblcourse>
      <C_Id>1</C_Id>
      <C_Name>DOt Net</C_Name>
   </tblcourse>
   <tblcourse>
      <C_Id>2</C_Id>
      <C_Name>EF</C_Name>
   </tblcourse>
   <tblcourse>
      <C_Id>3</C_Id>
      <C_Name>MVC</C_Name>
   </tblcourse>
   <tblcourse>
      <C_Id>4</C_Id>
      <C_Name>MS SQL</C_Name>
   </tblcourse>
</ArrayOftblcourse>

合同

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}") ]
List<tblcourse> GetData(string value);

反序列化时出错

附加信息:解析值时遇到意外字符:

tbclcourses的自动生成类文件

[DataContract]
[KnownType(typeof(tblstudent))]
public partial class tblcourse
{
        public tblcourse()
        {
            this.tblstudents = new HashSet<tblstudent>();
        }

        [DataMember]
        public int C_Id { get; set; }

        [DataMember]
        public string C_Name { get; set; }

        public virtual ICollection<tblstudent> tblstudents { get; set; }
}

web.config 文件 webHttpBinding:

<webHttpBinding>
    <binding name="webHttpBinding" 
             maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" 
             transferMode="StreamedResponse">
       <security mode="None">
           <transport clientCredentialType="Basic"></transport>
       </security>
       <readerQuotas maxArrayLength="100000" maxStringContentLength="2147483647" />
   </binding>
</webHttpBinding>

【问题讨论】:

  • 请提供您的服务合同的样子。
  • @dotnetstep :请参阅我的编辑。我无法对这样的字符串进行反序列化。
  • @marc_s :我收到错误为附加信息:解析值时遇到意外字符:
  • 你是否在配置文件中配置了 WebHttpBidning 和 webhttp 行为?
  • JsonConvert.DeserializeObject&lt;List&lt;Course&gt;&gt;(strResponse1); 您正在使用WebHttpBinding,它是(来自 MSDN)WCF Web 编程模型允许开发人员通过使用“普通旧 XML”( POX) 风格的消息传递,而不是基于 SOAP 的消息传递。 JSON 序列化程序将如何反序列化 XML?

标签: c# asp.net-mvc-4 wcf-rest


【解决方案1】:

我认为您的 webinvoke 应该是这样的。 (指定响应格式)

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}",ResponseFormat = WebMessageFormat.Json) ]
List<tblcourse> GetData(string value);

【讨论】:

  • +用于理解 XML 和 JSON 的细微差别以及 XML 不是 JSON 的原因。
  • @dotnetstep :工作就像一个魅力。谢谢。
  • @dotnetstep :我可以减少代码并仍然得到相同的结果吗?我可以跳过 foreach 循环吗?
  • 如果两个实体相同,那么您应该得到相同的结果。您可以尝试仅返回 var u = (from g in t.tblcourses select new { g.C_Id, g.C_Name }).ToList();返回你;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
相关资源
最近更新 更多