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