我已经在我们的应用程序中使用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);
}
答案 0 :(得分:0)
快速:
如果你只想返回一个json序列化字符串,那么在方法的最后只是
return _jsonReturn;
而不是
Context.Response.Write(_jsonReturn);
不要忘记将getcandidates()的返回值从void更改为string。