2017-04-04 78 views
1

我想将ASMX web服务转换为WCF web服务,问题是它一直说上下文在当前上下文中不存在。 这里是我的代码:WCF服务上下文。响应

using System; 
using System.Collections.Generic; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.Script.Serialization; 


[ServiceContract(Namespace = "")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class PhBookService 
{ 

    [OperationContract] 
    public void GetAllCities() 
    { 
     List<Cities> listCity = new List<Cities>(); 

     SqlConnection connection = new SqlConnection("Data Source=BS-HP-PC-11\\SQLEXPRESS;Initial Catalog=PhoneBookData; Integrated Security=true; User ID=phonebook;Password=phone"); 
     using (connection) 
     { 
      SqlCommand command = new SqlCommand("select * from d_City", connection); 
      connection.Open(); 
      SqlDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       Cities City = new Cities(); 
       City.CityID = Convert.ToInt32(reader["CityID"]); 
       City.CityN = reader["CityN"].ToString(); 
       listCity.Add(City); 
      } 
     } 
     JavaScriptSerializer javascript = new JavaScriptSerializer(); 
     Context.Response.Write(javascript.Serialize(listCity)); -- this line here is where I get the error. 
    } 

} 

添加任何类型的使用系统的“东西”似乎并没有为我工作。我该怎么办?

P.S.这是我的ajax请求。 enter image description here

回答

1

我不确定你是否包含了所有的代码,但是WCF服务通常包含一些东西,比如代表你的合同的接口。在你的情况下,你应该返回一个城市列表。

话虽这么说,我会组织你的代码有点不同:

  • DataContract为你的城市型代表您的ServiceContract其中包括 OperationContract的
  • 实现A类

  • 接口ServiceContract(接口)

    [DataContract] 
    public class Cities 
    { 
        [DataMember] 
        public string CityN {get;set;} 
    
        [DataMember] 
        public int CityID {get;set;} 
    } 
    
    [ServiceContract] 
    public interface ICities 
    { 
        [OperationContract] 
        List<Cities> GetAllCities(); 
    } 
    
    public class PhBookService : ICities 
    { 
        public List<Cities> GetAllCities() 
        { 
         List<Cities> listCity = new List<Cities>(); 
    
         SqlConnection connection = new SqlConnection("Data Source=BS-HP-PC-11\\SQLEXPRESS;Initial Catalog=PhoneBookData; Integrated Security=true; User ID=phonebook;Password=phone"); 
         using (connection) 
         { 
          SqlCommand command = new SqlCommand("select * from d_City", connection); 
          connection.Open(); 
          SqlDataReader reader = command.ExecuteReader(); 
          while (reader.Read()) 
          { 
           Cities City = new Cities(); 
           City.CityID = Convert.ToInt32(reader["CityID"]); 
           City.CityN = reader["CityN"].ToString(); 
           listCity.Add(City); 
          } 
         } 
    
         return listCity; 
        } 
    } 
    
+0

谢谢,我没有DataContract,我有一个类get; set;虽然。我会更新它。这将使我的url:使用ajax时不同吗? –

+1

不,它不应该。 DataContract是服务和客户之间的正式协议,抽象地描述要交换的数据。如果你使用一个,它应该让事情变得更清楚。 –

+0

我收到并执行此错误: “不能隐式转换类型'System.Collentions.Generic.List '到'System.Collentions.Generic.List '' –