Alexander Hristov
Alexander Hristov

Reputation: 311

WCF Service Context.Response

I'm trying to conver a ASMX webservice to a WCF webservice, the problem is it keeps saying Context doesn't exist in the current context. Here's my code:

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

}

Adding any type of using System."something" doesn't seem to work for me. What should I do?

P.S. This is my ajax request. enter image description here

Upvotes: 1

Views: 612

Answers (1)

William Xifaras
William Xifaras

Reputation: 5312

I'm not sure if you included all your code, but a WCF service typically includes several things such as an interface that represents your contract. In your case, you should just return a list of cities.

That being said, I would structure your code a bit differently:

  • DataContract for your city type
  • Interface that represents your ServiceContract which includes the OperationContract
  • A class that implements the ServiceContract (interface)

    [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;
        }
    }
    

Upvotes: 1

Related Questions