即使URI调用有效,调用WCF RIA服务也会产生空值

时间:2014-05-05 19:31:12

标签: c# wcf wcf-ria-services visual-studio-lightswitch

我觉得我在这个WCF RIA服务中一遍又一遍地碰到砖墙。

我有一个WCF RIA服务方法,它根据您传递给它的ID参数返回员工。它使用以下方法处理对URI的ajax调用:

/WCF_RIA_ServiceData.svc/GetEmployeeByID?EmployeeID=1

我想在MVC控制器中使用相同的查询,所以我试过这个:

// GET: /Employee/Details/5
public ActionResult Details(int id)
{
    WCF_RIA_ServiceData y = new WCF_RIA_ServiceData();
    WCF_RIA_ServiceDataService z = new WCF_RIA_ServiceDataService();

    WCF_RIA_Project.Employee employee = (WCF_RIA_Project.Employee)y.GetEmployeeByID(id);
    WCF_RIA_Project.Employee employee2 = (WCF_RIA_Project.Employee)z.GetEmployeeByID(id);

    ViewData["EmployeeID"] = id;
    ViewData["EmployeeFirstName"] = employee.FirstName;
    ViewData["EmployeeLastName"] = employee.LastName;
    return View();
}

以下是WCF RIA服务中的方法:

[Query(HasSideEffects = true)]
    public IQueryable<WCF_RIA_Project.Employee> GetEmployeeByID(int? EmployeeID)
    {
        var empData = from Employee in this.Context.Employees
                      where Employee.Id == EmployeeID
                      select new WCF_RIA_Project.Employee
                      {
                           ID = Employee.Id,
                           FirstName = Employee.FirstName,
                           LastName = Employee.LastName
                      };
        return empData;
    } 

我已经尝试了很多方法来让它返回正确的数据,但我每次都得到null。有什么东西我不见了吗?

我已经看到了其他使用数据库上下文的例子,但我找不到任何方法让我的MVC应用程序看到它。非常感谢任何帮助!

编辑: 因为这个项目是作为Lightswitch应用程序启动的,所以我通过oData公开了Employees表,并且查询oData服务的结果完全相同(null)。

ApplicationData appdata = new ApplicationData();
Employee employee = appdata.Employees_Single(id);

1 个答案:

答案 0 :(得分:0)

我能够解决这个问题,似乎我的主要问题是在错误的命名空间中。默认的是Lightswitch Implementation(我认为?)是自动生成的。

以下是所有工作代码,如果它感兴趣的话:

控制器方法:

 public ActionResult Details(int id)
    {
        WCF_RIA_Project.WCF_RIA_Service wcfria = new WCF_RIA_Project.WCF_RIA_Service();
        WCF_RIA_Project.Employee employee = wcfria.GetEmployeeByID(id);

        ViewData["EmployeeID"] = id;
        ViewData["EmployeeFirstName"] = employee.FirstName;
        ViewData["EmployeeLastName"] = employee.LastName;
        return View();
    }

WCF RIA服务方法:

[Query(IsComposable=false)]
    public Employee GetEmployeeByID(int? EmployeeID)
    {
        var empData = from Employee in this.Context.Employees
                      where Employee.Id == EmployeeID
                      select new Employee
                      {
                          ID = Employee.Id,
                          FirstName = Employee.FirstName,
                          LastName = Employee.LastName
                      };
        return empData.Single();
    }