C# 使用linq到sql的WCF REST WebServiceHostFactory

C# 使用linq到sql的WCF REST WebServiceHostFactory,c#,wcf,linq-to-sql,rest,C#,Wcf,Linq To Sql,Rest,我正在尝试使用WCF实现一个REST服务,它将获取参数P1、P2、P3、P4并将它们传递给一个存储过程,该存储过程将执行查询并返回结果 我的服务代码为C: using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using Syste

我正在尝试使用WCF实现一个REST服务,它将获取参数P1、P2、P3、P4并将它们传递给一个存储过程,该存储过程将执行查询并返回结果

我的服务代码为C:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ServiceModel.Activation;
using System.Data;

namespace RestServicePublishing
{
  [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
  [ServiceContract]
  public class RestService
  {
    [OperationContract]
    [WebGet]
    public List<Simulated_service_State> GetState(decimal P1, decimal P2, decimal P3,  decimal P4)
    {
       using (ServiceDataContext db = new ServiceDataContext())
       {
           List<Simulated_service_State> states = 
              db.GetStateByLongLat(P1, P2, P3, P4).ToList();
           db.SubmitChanges();
           return states;
       }
    }
  }
}
有谁能帮助我了解如何使用存储过程和REST WCF服务吗?如何将临时表中的值返回到客户端

更新

我无法将所有这些数据发布到评论部分,因此我将其发布在这里。 这是我现在使用的代码,但它给了我0作为结果,这意味着我的查询没有从表中选择任何内容:

public class RestService
{        
    [OperationContract]
    [WebGet (ResponseFormat = WebMessageFormat.Xml)]
    public string GetState(decimal P1, decimal P2, decimal P3, decimal P4)
    {
        IList<Simulated_service_State> query = new List<Simulated_service_State>();
        IList<string> Mac = new List<string>();
        int j;
        int jj;

        using (ServiceDataContext db = new ServiceDataContext())
        {
            query = db.GetStateByLongLat1(P1, P2, P3, P4).ToList();
            db.SubmitChanges();
            var query2 = from Simulated_service_State state in db.Simulated_service_States
                         select state.MAC;
            Mac = query2.ToList();
            jj = Mac.Count;
            j = query.Count;
        }
        return j.ToString() + "," + jj.ToString();
    }
}
如果我使用的代码不是存储过程,那么我将从表中获取结果,因此我假设linq to sql工作良好ServiceDataContext:

public class RestService
{

    [OperationContract]
    [WebGet (ResponseFormat = WebMessageFormat.Xml)]
    public string GetState(decimal P1, decimal P2, decimal P3, decimal P4)
    {
        IList<Simulated_service_State> query = new List<Simulated_service_State>();
        IList<string> Mac = new List<string>();
        int j;
        int jj;
        using (ServiceDataContext db = new ServiceDataContext())
        {
            var query3 = from SimulatedNode node in db.SimulatedNodes
                        select node.MAC;
            Mac = query3.ToList();
            j = (Mac.Count);
        }
        return j.ToString();
我得到的结果是4,表示列表中的条目数


知道如何继续吗?

我认为GetStateByLongLat返回null。然后ToList会抛出“Object reference not set to a instance of a Object.”错误。

我放弃了使用存储过程进行查询,因此我使用了标准的sql查询函数,该函数可以工作并提供结果。以下是最终代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ServiceModel.Activation;
using System.Data;

namespace RestServicePublishing
{
[AspNetCompatibilityRequirements(RequirementsMode =     AspNetCompatibilityRequirementsMode.Allowed)]
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[ServiceContract]

public class RestService
{

    [OperationContract]
    [WebGet (ResponseFormat = WebMessageFormat.Json)]
    public List<Last_status_by_LongLat> GetState(decimal P1, decimal P2, decimal P3, decimal P4)
    {
        List<Last_status_by_LongLat> Mac = new List<Last_status_by_LongLat>();

        using (ServiceDataContext db = new ServiceDataContext())
        {
            var query = from view node in db.views
                         where table.param > P1 && table.param2 < P2 && table.param2 > P3 && table.param < P4
                         select table;
            Mac = query.ToList();


        }
        return Mac;

    }
}
}

我将P1-P4参数传递给sql调用的where语句,然后将变量查询传递给LISTMAC,它将sql视图表示为LINQtoSQL。最后,我将列表内容重新发送到GetState WebGet调用。

您可以调试GetState方法并查看空值吗?我假设您的ServiceDataContext由于某种原因无法创建,不确定该原因是什么-错误消息似乎指向此原因…不幸的是,我无法使用调试器。我已经根据这里的说明设置了web.config文件:但是我仍然无法调试代码。关于如何进入代码有什么想法吗?顺便说一句:当我设置WCFSOAP服务时,使用LINQtoSQL存储过程进行相同的代码查询是有效的。但是这次我需要设置其余的。@马克:您应该可以在Visual Studio中按F5来运行WCF服务,然后在浏览器中键入URL来请求您的服务。您应该能够在代码中有一个断点,即调用的方法,当您从浏览器发出请求时,该方法将被带到那里……我已经在一个基于SOAP的服务中测试了该存储过程,它将在那里返回结果。此外,我还将其作为一个基于控制台的客户端应用程序进行了测试,没有通过web服务公开结果,并且工作正常。我怀疑GetStateByLongLat的结果没有正确地将结果传递给列表。但这只是一个猜测,因为我无法深入到代码中查看到底发生了什么。旧式调试:显式地执行操作:将结果放入变量中,检查null,对其进行迭代并将其添加到新列表中,返回列表我不再收到错误,我假设它与linq to sql身份验证问题有关。这是我正在使用的当前代码,但它将0作为值返回。如果任何人有与存储过程一起工作的解决方案,请共享它。存储过程的编码更简单、更清晰,数据检索逻辑是在SQL server端完成的。我个人的意见和偏好。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ServiceModel.Activation;
using System.Data;

namespace RestServicePublishing
{
[AspNetCompatibilityRequirements(RequirementsMode =     AspNetCompatibilityRequirementsMode.Allowed)]
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[ServiceContract]

public class RestService
{

    [OperationContract]
    [WebGet (ResponseFormat = WebMessageFormat.Json)]
    public List<Last_status_by_LongLat> GetState(decimal P1, decimal P2, decimal P3, decimal P4)
    {
        List<Last_status_by_LongLat> Mac = new List<Last_status_by_LongLat>();

        using (ServiceDataContext db = new ServiceDataContext())
        {
            var query = from view node in db.views
                         where table.param > P1 && table.param2 < P2 && table.param2 > P3 && table.param < P4
                         select table;
            Mac = query.ToList();


        }
        return Mac;

    }
}
}