在Wcf Services中返回JSON

时间:2016-11-18 17:06:48

标签: c# json oracle wcf

我正在创建wcf服务,查询Oracle数据库并以JSON格式返回结果。如何将Oracle查询的结果转换为JSON消息

 public class Service1 : IService1
 {
  public HttpResponseMessage GetData(string ROOM)
{
     List<OracleParameter> prms = new List<OracleParameter>();
    prms.Add(new OracleParameter("ROOM", OracleDbType.Varchar2, ROOM, ParameterDirection.Input));
    string connStr = ConfigurationManager.ConnectionStrings["TGSDataBaseConnection"].ConnectionString;            using (OracleConnection dbconn = new OracleConnection(connStr))
    {
        DataSet userDataset = new DataSet();
        var strQuery = "SELECT * from LIMS_SAMPLE_RESULTS_VW where ROOM = :ROOM";
        var returnObject = new { data = new OracleDataTableJsonResponse(connStr, strQuery, prms.ToArray()) };
        var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json"));
        ContentDispositionHeaderValue contentDisposition = null;
        if (ContentDispositionHeaderValue.TryParse("inline; filename=TGSData.json", out contentDisposition))
        {
            response.Content.Headers.ContentDisposition = contentDisposition;
        }
        return response;
    }

它会引发错误,如名称&#39;请求&#39;在行

中的当前上下文中不存在
var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json"));

我添加了

 using System.Net.Http;
 using System.Net.Http.Formatting;
 using System.Net.Http.Headers;

并添加了他们的程序集作为参考。但仍然显示相同的错误。 enter image description here

1 个答案:

答案 0 :(得分:0)

尝试将Request更改为HttpContext.Current.Request