在构建WCF服务时,由于上下文在当前上下文中不存在而显示错误

本文关键字:上下文 不存在 错误 显示 WCF 构建 服务 | 更新日期: 2023-09-27 18:02:35

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

名称'context'在当前上下文中不存在。

我尝试在web.config文件和

中添加<service.webmodels>
[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);
}

and 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);
    }

在构建WCF服务时,由于上下文在当前上下文中不存在而显示错误

一个简单的例子:

如果你只想返回一个json序列化字符串,在你的方法结束时,只需

返回_jsonReturn;

代替

Context.Response.Write (_jsonReturn);

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