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.
}
}
添加任何類型的使用系統的「東西」似乎並沒有爲我工作。我該怎麼辦?
謝謝,我沒有DataContract,我有一個類get; set;雖然。我會更新它。這將使我的url:使用ajax時不同嗎? –
不,它不應該。 DataContract是服務和客戶之間的正式協議,抽象地描述要交換的數據。如果你使用一個,它應該讓事情變得更清楚。 –
我收到並執行此錯誤: 「不能隱式轉換類型'System.Collentions.Generic.List'到'System.Collentions.Generic.List '' –