Skip to main content
 首页 » 编程设计

c#之Wcf 返回通常的响应

2024年08月06日38lyj

我正在尝试返回一个列表

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 得到的响应,因此我无法将响应反序列化 到列表中。

<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> 

契约(Contract)

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

反序列化时出错

Additional information: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

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> 

请您参考如下方法:

我认为您的 webinvoke 应该如下所示。 (指定响应格式)

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