WCF在OperationContract中返回数据 [英] WCF Return Data in OperationContract

查看:64
本文介绍了WCF在OperationContract中返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一种在WCF OperationContract中返回一组数据的方法。我返回的数据可以是任何行和任意数量的列。



我的WCF(库)由Windows服务托管。到目前为止,我得到通常的错误服务无法启动.System.NotSupportedException:不支持多维数组。我已经尝试过锯齿状数组,字符串[],数据集,数据表......关于如何返回数据行和列的任何建议?



仅供参考 - 显然我是新的在WCF所以请温柔:-)



这是我的一些WCF库代码(其中一些):



I need to find a way to return a set of data in a WCF OperationContract. The data I am returning can be any rows and any number of columns.

My WCF (library) is hosted by a Windows Service. Thus far I get the usual error "Service cannot be started. System.NotSupportedException: Multi-dimensional arrays are not supported." I have tried jagged arrays, string[], DataSet, DataTable...any suggestions on how to return rows and columns of data?

FYI - Clearly I am new at WCF so please be gentle :-)

Here is some of my WCF Library code (some of it):

[ServiceContract]
public interface IOperationsDataService
{
  [OperationContract]
  ReturnResults ExecuteStoredProcedure(string connectionKey, string procedureName, SpParameter[] spParameter, bool pleaseReturnRecords);
}

[DataContract]
public class ReturnResults
{
  [DataMember]
  public String[][] ReturnedResultsArray { get; set; }
}







***以下是事件查看器的完整错误:



服务无法启动。 System.NotSupportedException:不支持多维数组。

在System.Runtime.Serialization.CollectionDataContract.CollectionDataContractCriticalHelper..ctor(类型类型)

在System.Runtime。 Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id,RuntimeTypeHandle typeHandle,Type type)

at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type)

at System.Runtime.Serialization.XsdDataContractExporter.GetSchemaTypeName(Type type)

at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.ValidateDataContractType(Type type)

at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreatePartInfo(MessagePartDescription部分,OperationFormatStyle样式,DataContractSerializerOperationBehavior serializerFactory)
System.ServiceModel上的
.Dispatcher.DataContractSerializerOperationFormatter.CreateMessageInfo(DataContractFormatAt ...

事件Xml:

<事件xmlns =http://schemas.microsoft.com/win/2004/ 08 / events / event>

< System>

< Provider Name =HostedService/>

< EventID限定符=0> 0< / EventID>

< Level> 2< / Level>

<任务> 0< /任务>

< Keywords> 0x80000000000000< / Keywords>

< TimeCreated SystemTime =2013-01-04T18:52:14.000000000Z/>

< ; EventRecordID> 4359< / EventRecordID>

<频道>应用< /频道>

<电脑> ntvdeva235.apsc.com< /电脑>

< Security />

< / System>

< EventData>

< Data>服务不能是开始。 System.NotSupportedException:不支持多维数组。

在System.Runtime.Serialization.CollectionDataContract.CollectionDataContractCriticalHelper..ctor(类型类型)

在System.Runtime。 Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id,RuntimeTypeHandle typeHandle,Type type)

at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id,RuntimeTypeHandle typeHandle,Type type)

at System.Runtime.Serialization.XsdDataContractExporter.GetSchemaTypeName(Type type)

at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.ValidateDataContractType(Type type)

at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreatePartInfo(MessagePartDescription部分,OperationFormatStyle样式,DataContractSerializerOperationBehavior serializerFactory)
System.ServiceModel上的
.Dispatcher.DataContractSerializerOperationFormatter.CreateMessageInfo(DataContractFormatAt ...< / Data>

< / EventData>

< / Event>




*** Here is the entire error from the Event Viewer:

Service cannot be started. System.NotSupportedException: Multi-dimensional arrays are not supported.
at System.Runtime.Serialization.CollectionDataContract.CollectionDataContractCriticalHelper..ctor(Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.XsdDataContractExporter.GetSchemaTypeName(Type type)
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.ValidateDataContractType(Type type)
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreatePartInfo(MessagePartDescription part, OperationFormatStyle style, DataContractSerializerOperationBehavior serializerFactory)
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreateMessageInfo(DataContractFormatAt...
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="HostedService" />
<EventID Qualifiers="0">0</EventID>
<Level>2</Level>
<Task>0</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2013-01-04T18:52:14.000000000Z" />
<EventRecordID>4359</EventRecordID>
<Channel>Application</Channel>
<Computer>ntvdeva235.apsc.com</Computer>
<Security />
</System>
<EventData>
<Data>Service cannot be started. System.NotSupportedException: Multi-dimensional arrays are not supported.
at System.Runtime.Serialization.CollectionDataContract.CollectionDataContractCriticalHelper..ctor(Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.XsdDataContractExporter.GetSchemaTypeName(Type type)
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.ValidateDataContractType(Type type)
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreatePartInfo(MessagePartDescription part, OperationFormatStyle style, DataContractSerializerOperationBehavior serializerFactory)
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreateMessageInfo(DataContractFormatAt...</Data>
</EventData>
</Event>

推荐答案

1。将服务starp帐户更改为具有管理权限的用户帐户。



2.您可以使用Stream返回大数据/图像。





3.由于您无法连接,可能是访问限制,但此消息可以在您''时显示因其他问题被踢回来,所以这可能是一个缺少引用的二进制文件,或使用无效的DataContract或类似的东西。



首先,打开一些诊断:



< system.diagnostics>

< sources>

< source name =System。 ServiceModelswitchValue =Warning,ActivityTracingpropagateActivity =true>

< listeners>

< add type =System.Diagnostics.DefaultTraceListenername = 默认>

< filter type =/>

< / add>

< add name = ServiceModelTraceListener>

< filter type =/>

< / add>

< / listeners>

< / source>

< / sources>

< sharedListeners>

< add initializeData =path \to\trace.svclog

type =System.Diagnostics.XmlWriterTraceListener,System,Version = 4.0 .0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089

name =ServiceModelTraceListenertraceOutputOptions =Timestamp>

< filter type =/>

< / add>

< / sharedListeners>



现在运行服务,得到错误并且使用SvcTraceTool(只需转到Program Files \Microsoft SDK并搜索svc)打开输出日志。这将为您提供更多关于该过程的概述,此时它会出错并指向异常的更多细节 - 有时只是告诉您查看事件日志中的特定异常。











4.你需要定义的东西是序列化。 System.Drawing.Image是,但默认情况下不在WCF的cotnext中(使用DataContractSerializer)。这可能包括将原始字节作为数组返回,或将序列化返回到字符串(base64,JSON)或实现可序列化的DataContract,并且可以随身携带数据。



正如其他人所说,WCF支持流媒体,但这并不是问题的症结所在。根据您可能希望执行此操作的数据大小,这样做会减少问题本身,因为您将流式传输字节(从明显的顶级视图)。
1. Change the service starp account to that of a User with administrative privileges.

2.You can use Stream to return large data/Images.


3. Since you can''t connect, it could be an access restriction, but this message can show when you''ve been kicked back for other issues, so this could be a missing referenced binary, or use of an invalid DataContract or something similar.

Firstly, turn on some diagnostics:

<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning, ActivityTracing" propagateActivity="true">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="path\to\trace.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>

Now run the service, get the error and using SvcTraceTool (just go to Program Files\Microsoft SDKs and do a search for svc) open up the output log. This will give you more of an overview of the process, at which point it goes wrong and point to more details of the exception - sometimes just telling you to look in the Event Log for the specific exception.





4.You will need to define something that is serializable. System.Drawing.Image is, but not in the cotnext of WCF (using DataContractSerializer) by default. That could range from returning the raw bytes as an array, or serialization to a string (base64, JSON) or implementing a DataContract that is serializable and can carry the data with it.

As others have said, WCF supports streaming, but that''s not the crux of the issue here. Depending on the size of the data you might wish to do this, and doing so will reduce the problem in itself since you would be streaming bytes (from an evident, top-level view).


这篇关于WCF在OperationContract中返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆