同时构建wcf服务显示错误,因为当前上下文中不存在上下文

时间:2014-05-10 07:46:59

标签: c# json wcf

我已经在我们的应用程序中使用Web服务实现了服务,现在我们正在将这些服务修改为WCF,我使用契约在IService.cs中声明了所有服务方法,并在Service.svc中实现了它们,但是在构建服务时它显示错误

  

名称' context'在当前背景下不存在。

我尝试在<service.webmodels>文件和

中添加web.config
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

Service.svc和响应格式:json都没有效果..如果有人尝试或经历过这个错误请。请分享......

以下是ICandidateService.cs中的代码:

[ServiceContract]
public interface ICandidateService
{
    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    void getcandidates(int candidate_id, string whereclause, string hotlist_ind);
}

CandidateService.svc

[AspNetCompatibilityRequirements(RequirementsMode = 
    AspNetCompatibilityRequirementsMode.Allowed)]

public class CandidateService : ICandidateService
{
    [Dependency]
    public ICandidateDB candidateDb { get; set; }

    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
    public void getcandidates(int candidate_id, string whereclause, string hotlist_ind)
    {
        List<Candidatedetails> canddetails = new List<Candidatedetails>();

        DataSet ds = candidateDb.GetCandidates(candidate_id, whereclause, hotlist_ind);

        foreach (DataRow dtrow in ds.Tables[0].Rows)
        {
            Candidatedetails cand = new Candidatedetails();
            //cand.code = "";// dtrow["code"].ToString();
            //cand.avalability = dtrow["avalability"].ToString();
            //if(dtrow["available_date"].ToString()==""){
            //    cand.available_date = "";
            //}
            //else
            //cand.available_date = Convert.ToDateTime(dtrow["available_date"]).ToString("MM/dd/yyyy");

            //  cand.exptd_loaded_cost = dtrow["exptd_loaded_cost"].ToString();
            cand.candidate_ID = dtrow["candidate_ID"].ToString();
            cand.Display_Name = dtrow["display_name"].ToString();
            cand.Mobile = dtrow["mobile"].ToString();
            cand.Email = dtrow["email1"].ToString();
            cand.qualification = dtrow["qualification"].ToString();
            cand.skills = dtrow["skills"].ToString();
            cand.experience = dtrow["experience"].ToString();
            cand.Salary = dtrow["expectedSalary"].ToString();
            cand.category = dtrow["category"].ToString();
            //cand.subcategory = dtrow["subCategory"].ToString();
            cand.attachmentFlag = dtrow["attachmentFlag"].ToString();
            cand.city = dtrow["city"].ToString();
            cand.State = dtrow["State"].ToString();
            cand.country = dtrow["country"].ToString();

            cand.currentEmployer = dtrow["currentEmployer"].ToString();
            cand.dateOfBirth = dtrow["dateOfBirth"].ToString();
            canddetails.Add(cand);
        }

        System.Web.Script.Serialization.JavaScriptSerializer jSearializer =
             new System.Web.Script.Serialization.JavaScriptSerializer();

        string _jsonReturn = jSearializer.Serialize(canddetails);
        _jsonReturn = "{\"aaData\":" + _jsonReturn + "}";

        Context.Response.Write(_jsonReturn);
       //return _jsonReturn;
        //  return details.ToArray();
        //ResponseFormat.Json(_jsonReturn);
    }

1 个答案:

答案 0 :(得分:0)

快速:

如果你只想返回一个json序列化字符串,那么在方法的最后只是

  

return _jsonReturn;

而不是

  

Context.Response.Write(_jsonReturn);

不要忘记将getcandidates()的返回值从void更改为string。