C# - 设计问题建议 : Can't Use Dictionary then what?

标签 c# linq wcf-data-services

我需要通过 Web 服务,特别是通过 WCF 数据服务返回一个字典(或一些列表,我只是不知道)。看起来 WCF 数据服务不支持字典类型。

通过网络服务看起来像这样:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
- <Employees xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
    <element>employee1, True</element> 
    <element>employee2, False</element>
    <element>employee3, True</element>
  </Employees>

我首先尝试了一个一维数组,这似乎可行,但当然只带来了具有 3 个元素的一维数组:

[WebGet]
public string[] Employees()
{
   return new[]
   {
        "employee1",
        "employee2",
        "employee3"
   };
}

基本上,我需要一些列表(?),每个列表都有两个参数,即 EmployeeName 和一个 bool 值 IsActive。

任何建议将不胜感激。

更新:我将以下内容添加到我的网络服务中:

public class Employee
    {
        public string Name{ get; set; }
        public bool IsActive{ get; set; }

        public Employee(string name, bool isActive)
        {
            Name = name;
            IsActive = isActive;
        }
    }

[WebGet]
        public List<Employee> Employees()
        {
            var emp1 = new Employee("Test1", true);
            var emp2 = new Employee("Test2", true);
            var list = new List<Employee> { emp1, emp2 };
            return list;
        }

当通过我的网络浏览器点赞 .svc 文件时,我会加载它:

Request Error

The server encountered an error processing the request. The exception message is 'Unable to load metadata for return type 'System.Collections.Generic.List`1[Web.Employee]' of method 'System.Collections.Generic.List`1[.Web.Employee] Employees()'.'. See server logs for more details. The exception stack trace is: 

at System.Data.Services.Providers.BaseServiceProvider.AddServiceOperation(MethodInfo method, String protocolMethod) at System.Data.Services.Providers.BaseServiceProvider.AddOperationsFromType(Type type) at System.Data.Services.DataService`1.CreateProvider() at System.Data.Services.DataService`1.HandleRequest() at System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody) at SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

有什么想法吗?

更新 2:

这里是关于我的 DataService.svc 类的更多信息。我正在使用带有 .NET 4.0 框架的 WCF 数据服务 V2:

public class WebDataService : DataService<MyModelEntities>
    {
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.UseVerboseErrors = true;
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead | EntitySetRights.AllWrite);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

最佳答案

你为什么不创建一个用户定义的数据类型,它有两个属性,一个字符串和一个 bool 值?

public class MySillyWCFObject
{ 
 boolean b;
 string name;

 public MySillyWCFObject(boolean b, string s)
  {
   this.b = b;
   this.name = s;
  }
}

然后你可以说:

MySillyWCFObject m = new MySillyWCFObject(true, "Hi");

关于C# - 设计问题建议 : Can't Use Dictionary then what?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5570173/

相关文章:

c# - 在 Razor 中使用一个提交按钮提交两个 HTML 表单

c# - 在 C# 中创建 Notepad++ 插件

c# - 从 WPF System.Windows.Shapes.Path 创建图像

c# - 流利的查询表达式——两者之间有什么优势吗?

linq - 使用类的最大值 LINQ

asp.net - SQL Azure 的 OData 服务是只读的吗?

c# - 以编程方式选择 ListBox 中的多个项目?

c# - 将 Lambda 表达式树转换预编译为常量?

mysql - MySQL 的 ADO.NET 数据服务

c# - 可以使用相关实体(而不是相关 ID)使用 OData 创建实体吗?