WCF服务Context.Response

时间:2017-04-04 14:15:35

标签: c# web-services wcf

我试图将ASMX Web服务转换为WCF Web服务,但问题在于它一直表示Context在当前上下文中不存在。 这是我的代码:

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

}

添加任何类型的使用系统。&#34;东西&#34;似乎不适合我。我该怎么办?

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

1 个答案:

答案 0 :(得分:1)

我不确定您是否包含了所有代码,但WCF服务通常包含一些内容,例如代表合同的界面。在您的情况下,您应该返回一个城市列表。

话虽这么说,我会以不同的方式构建你的代码:

  • DataContract为您的城市类型
  • 代表您的ServiceContract的接口,包括 OperationContract的
  • 实现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;
        }
    }