StoredProcedure返回复杂类型,数据服务,实体FrameWork和WCF

时间:2013-11-26 07:07:24

标签: odata

我有实体框架TaskTracker.edmx 存在StoredProcedure GetEmployees并使用Complex类型返回Data

在TaskTrackerDataService.cs

public static void InitializeService(DataServiceConfiguration config)
{
  // Grant only the rights needed to support the client application.
  config.SetEntitySetAccessRule("*", EntitySetRights.All);
  config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
  config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}

[WebGet, WebInvoke(ResponseFormat = WebMessageFormat.Xml)]
public IQueryable<TaskTracker_EDM.EmployeeView> GetEmployees()
{
  TaskTracker_EDM.TaskTrackerEntities ctx = new TaskTracker_EDM.TaskTrackerEntities();
  return  ctx.GetEmployees(string.Empty).AsQueryable();
}

在客户端站点[控制台应用程序]

      var emps = ctxDSvc.Execute<EmployeeView>(new   Uri("http://localhost:2402/TaskTrackerDataService.svc/GetEmployees", UriKind.RelativeOrAbsolute));


  foreach (EmployeeView e in emps)
  {
    Console.WriteLine(string.Format("ID: {0} - {1} ", e.EmployeeID, e.Name));
  }

foreach无效,emps中没有数据。

总是拥有

+       emps    {System.Data.Services.Client.QueryOperationResponse<TaskTrackerConsoleTest.TaskTrackerDataService.EmployeeView>}    
System.Collections.Generic.IEnumerable<TaskTrackerConsoleTest.TaskTrackerDataService.EmployeeView> {System.Data.Services.Client.QueryOperationResponse<TaskTrackerConsoleTest.TaskTrackerDataService.EmployeeView>}

我读到OData不支持复杂类型。[他们是否已修复]或是否有其他解决方案。

有人说,使用Xml到Linq的方法来获取复杂的类型。 [任何帮助]

任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

我将服务器上的结果[ObjectToString]序列化,然后再将其发送到客户端应用程序

然后反序列化。

如果还有其他想法,请不要犹豫告诉我。

1- at TaskTracker_WCF1 -> TaskTrackerData.cs 
   //When StoredProcedure return Complex Type , Then you need to serialise it [Object To String] before send it to Client application

   //OData doesn't support Complex Type

[WebGet]
public String GetEmployees_SPreturnComplexType(String nameSearch)
{
  TaskTracker_EDM.TaskTrackerEntities ctx = new TaskTracker_EDM.TaskTrackerEntities();
     List<TaskTracker_EDM.EmployeeView> hiEmployeeView = (List<TaskTracker_EDM.EmployeeView>)ctx.GetEmployees("Keko88").ToList();

    //Serialize object to String
    XmlSerializer serializer = new XmlSerializer(hiEmployeeView.GetType());

    using (StringWriter writer = new StringWriter())
    {
      serializer.Serialize(writer,hiEmployeeView);

      return writer.ToString();
    }      
}

2-在program.cs的控制台应用程序

  TaskTrackerDataService.TaskTrackerEntities ctxDSvc = 
  new TaskTrackerDataService.TaskTrackerEntities(new Uri("http://localhost:2402/TaskTrackerDataService.svc"));
  //=======================================
  //2-1Call StoredProcedure return Complex Type
  String emps = ctxDSvc.Execute<String>(new Uri(ctxDSvc.BaseUri.ToString() + 
                                  "/GetEmployees_SPreturnComplexType", UriKind.RelativeOrAbsolute)).Single();

  //2-2We need to Deserialize it before use it
  var reader = new StringReader(emps);
  var serializer = new XmlSerializer(typeof(List<EmployeeView>));
  List<EmployeeView> instance = (List<EmployeeView>)serializer.Deserialize(reader);
  //=======================================

  foreach (EmployeeView e in instance)
  {
    Console.WriteLine(string.Format("ID: {0} - {1} ", e.EmployeeID, e.Name));
  }
  Console.WriteLine();
  //=======================================