C# – 设计问题建议:那么不能使用字典么?
发布时间:2020-12-16 01:42:56 所属栏目:百科 来源:网络整理
导读:我需要通过Web服务返回一个Dictionary(或一些List,我只是不知道),特别是通过WCF数据服务.看起来WCF数据服务不支持字典类型. 通过Web服务看起来像这样: ?xml version="1.0" encoding="utf-8" standalone="yes" ? - Employees xmlns="http://schemas.microsof
我需要通过Web服务返回一个Dictionary(或一些List,我只是不知道),特别是通过WCF数据服务.看起来WCF数据服务不支持字典类型.
通过Web服务看起来像这样: <?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> 我首先尝试了一个1维数组,这似乎工作,但当然只带来3个elemet数字的1维数组: [WebGet] public string[] Employees() { return new[] { "employee1","employee2","employee3" }; } 基本上,我需要一些List(?),每个都有两个参数,即EmployeeName和一个booleann值,IsActive. 任何建议将不胜感激. 更新:我在Web服务中添加了以下内容: 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; } 解决方法
为什么不创建一个用户定义的数据类型,其中包含两个属性:字符串和布尔值?
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"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |