2017-04-04 109 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 '' –