当对象被反序列化时从 WCF 流中读取?
我目前有一个启用了流式传输的 WCF 服务,这大大减少了客户端应用程序的内存使用量,但我希望能够在客户端上反序列化流中的对象时读取它们并填充 DataTable,而不是等待接收所有对象,因为获取所有数据仍然需要很长时间。
我读过 这篇文章似乎与我的目标相关,但似乎已经过时了。我想知道是否有更现代的方法使用 WCF 4.0 来实现此目的。
更新:
因此,我使用上面链接的文章中描述的方法在我的服务中实现了该操作,但我在运行时遇到了此异常:
无法加载操作“GetMyDtos”,因为它具有 System.ServiceModel.Channels.Message 类型的参数或返回类型,或者具有 MessageContractAttribute 和其他不同类型参数的类型。使用 System.ServiceModel.Channels.Message 或带有 MessageContractAttribute 的类型时,该方法不得使用任何其他类型的参数。
我的操作具有以下签名:
Message GetMyDtos( bool param1, int? param2, bool param3 );
所以我将其更改为:
Message GetMyDtos( GetMyDtosParameters getMyDtosParameters );
GetMyDtosParameters 看起来像这样:
[MessageContract]
public class GetMyDtosParameters
{
[MessageHeader]
public bool Param1 { get; set; }
[MessageHeader]
public int? Param2 { get; set; }
[MessageHeader]
public bool Param3 { get; set; }
}
现在服务端一切正常,但在向客户端添加或更新服务引用时遇到问题。方法 GetMyDtos
是在客户端中生成的,没有任何方法参数(也没有重载),因此我无法将任何参数传递给服务操作。
更新 2:
即使不传递任何参数,我也能够从服务中获取结果。我猜测正在使用默认值,因为它们是基元和/或可为空值。但是,我仍然希望能够使用不同的参数值调用服务,但我无法这样做。
I currently have a WCF service which has streaming enabled which has greatly decreased the memory usage of the client application, but I want to be able to read the objects in the stream as they are deserialized on the client and populate a DataTable instead of waiting for all the objects to be received as it still takes a long time to get all the data.
I had a read through this article which seems to be relevant to my goal, but it seems rather out of date. I was wondering if there is a more modern way of achieving this using WCF 4.0.
Update:
So I implemented the operation in my service using the method described in the article linked above, but I came across this exception at runtime:
The operation 'GetMyDtos' could not be loaded because it has a parameter or return type of type System.ServiceModel.Channels.Message or a type that has MessageContractAttribute and other parameters of different types. When using System.ServiceModel.Channels.Message or types with MessageContractAttribute, the method must not use any other types of parameters.
My operation had the following signature:
Message GetMyDtos( bool param1, int? param2, bool param3 );
So I changed it to this:
Message GetMyDtos( GetMyDtosParameters getMyDtosParameters );
and GetMyDtosParameters looks like this:
[MessageContract]
public class GetMyDtosParameters
{
[MessageHeader]
public bool Param1 { get; set; }
[MessageHeader]
public int? Param2 { get; set; }
[MessageHeader]
public bool Param3 { get; set; }
}
Everything's working fine on the service end now, but I have an issue when adding or updating a service reference to the client. The method GetMyDtos
is generated in the client without any method parameters (and no overloads) so I cannot pass any parameters to the service operation.
Update 2:
I'm able to get back results from the service even without passing any parameters in. I'm guessing default values are being used since they're primitives and/or Nullables. However, I'd still like to be able to make calls to the service with different values for the parameters, but I'm unable to do so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那篇文章背后的想法仍然有效。为什么说它已经过时了呢?流式传输 WCF 消息主要涉及处理 WCF 消息本身,并且此处描述的技术应该可行。您有任何问题吗?
The idea behind that article is still valid. Why do you say it's out of date? Streaming a WCF message involves mostly dealing with a WCF message itself, and the technique described there should work. Are you having any problems with it?