WCF:不支持多维数组 [英] WCF : Multi-dimensional arrays are not supported

查看:48
本文介绍了WCF:不支持多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有多维数组的不同对象需要通过WCF netTcpBinding连接.尝试时WCF抛出不支持多维数组".

Different objects with multi-dimensional arrays need to go through a WCF netTcpBinding connection. WCF throws "Multi-dimensional arrays are not supported" when trying.

我无法更改此类的界面.我将创建一个包装器,该包装器将在通过wcf之前对对象进行二进制序列化(即使netTcpBinding应该是二进制传输).我说的对吗?

I can't change the interface of this class. I'm going to create a wrapper that will binary serialize the objects before going through wcf (even if netTcpBinding is supposed to be a binary transfer). Am I right?

我正在寻找一种优雅的通用设计来进行这种转移.在启动该开发人员之前(在C#点网4中),社区有什么建议吗?

I'm looking for an elegant generic design to do that transfer. Any advices from the community before I start that dev (in C# dot net 4)?

推荐答案

是否可以将WCF服务的接口更改为接受byte []而不是所讨论的类?您的类仍然可以具有多维数组,但是不必传递实例,而将其序列化为byte []并将其作为参数传递.我认为这就是@John Willemse所说的.

Can you change the interface of your WCF service to accept the byte[] instead of the class in question? Your classes can still have the multi-dimensional arrays, but instead of passing an instance, serialize it to a byte[] and pass that as the parameter. I think that's what @John Willemse is saying.

听起来确实可以接受更改WCF服务界面,因为您似乎没有100%充实它.没有您的原始代码,我会受到一定的限制,但是类似以下示例的内容应该可以帮助您:

It does sound like you can accept changing the WCF service interface since it appears you don't have it 100% fleshed out. Without your original code I am somewhat limited but something like this example should help guide you:

更改自:

[ServiceContract]
public interface IShipmentManagement
{
    [OperationContract]
    void Process(Shipment shipment);
}

收件人:

[ServiceContract]
public interface IShipmentManagement
{
    [OperationContract]
    void Process(byte[] serializedShipment);
}

然后在调用WCF服务的代码中,您需要对"Shipment"实例进行二进制序列化,如下所示:

And then in your code that calls the WCF service you need to binary serialize the "Shipment" instance like so:

var shipmentManagement = // however you get a reference to your WCF service
var shipment = new Shipment();

// populate your array or whatever

IFormatter formatter = new BinaryFormatter();
var ms = new MemoryStream();
formatter.Serialize(stream, shipment);
var serializedShipment = ms.ToArray();
shipmentManagement.Process(serializedShipment);

另一方面,您只需要反序列化byte []并将实例传递给您拥有的任何代码.让我知道是否需要进一步说明.

On the other side you just need to deserialize the byte[] and pass the instance on to whatever code you have. Let me know if you need more clarification.

这篇关于WCF:不支持多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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