通过 WCF 发送 Tuple 对象?

发布于 2024-08-30 20:30:01 字数 309 浏览 7 评论 0原文

WCF 的数据契约序列化程序是否支持 System.Tuple 类(即,我可以将 Tuple 对象传递给 WCF 调用和/或将它们作为结果的一部分或全部接收吗) ?

我找到了此页面,但没有明确、明确的“您可以发送并通过 WCF 接收元组”我所希望的答案。

我猜你可以,只要 Tuple 本身中的所有类型都受数据契约序列化器支持 - 任何人都可以为我提供更明确的信息回答?谢谢。

Is the System.Tuple class supported by WCF's Data Contract Serializer (i.e., can I pass Tuple objects to WCF calls and/or receive them as part or all of the result)?

I found this page, but not the clear, definitive "you can send and receive Tuples with WCF" answer I was hoping for.

I'm guessing that you can, as long as all of the types within the Tuple itself are supported by the Data Contract Serializer -- can anyone provide me with a more definitive answer? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烂柯人 2024-09-06 20:30:01

Tuple 类型用 SerializedAttribute 标记,因此,如果您存储的类型是可序列化的,那么它们也应该能够由 WCF 序列化。

查看:Tuple'1Tuple'2 等。它说:

[SerializableAttribute]
public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple

请注意,您链接的文档包含以下行:

部分信任环境不支持 [Serialized] / ISerialized 编程模型。

所以,这可能并不像看起来那么容易。

(顺便说一句,Tuple 静态类也值得一看。 )

The Tuple types are marked with SerializableAttribute, therefore, if the types you store within are serializable, they should be able to be serialized by WCF as well.

Check out: links of Tuple'1, Tuple'2, etc. It says:

[SerializableAttribute]
public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple

Note that the document you linked contains the following line:

The [Serializable] / ISerializable programming model is not supported in a partial trust environment.

So, it may not be as easy as it would seem.

(BTW, the Tuple static class is also worth checking out.)

丢了幸福的猪 2024-09-06 20:30:01

我自己只是在深入研究这一点,似乎有一个问题可能是如果您通过 Silverlight 使用 WCF 服务:请参阅 Davy Brion 的博客了解更多信息。

Silverlight 版本的 Tuple 没有可序列化属性,这目前造成了一个问题。

I was just digging into this myself, and it seems that one issue might be if you're consuming the WCF service through Silverlight: see Davy Brion's blog for more.

The Silverlight version of Tuple doesn't have the Serializable attribute, which poses an issue at present.

把时间冻结 2024-09-06 20:30:01

我的元组可以很好地与 .NET 4.0 和 WCF 配合使用(提醒:您需要 .NET 4.0 来支持元组)。

这是单元测试方法(通过 WCF 层调用该方法):

/// <summary>
/// Test Tuples
/// </summary>
[TestMethod()]
public void WcfTestTupleUnit()
{
  Tuple<double, double> x;
  x=CallViaWCF.testTuple();
  Assert.AreEqual(x.Item1, 42);
  Assert.AreEqual(x.Item2, 43);
}
#endregion

这是接口:

[OperationContract]
Tuple<double, double> testTuple();

这是实现:

public Tuple<double, double> testTuple()
{
  return new Tuple<double, double>(42, 43);
}

我刚刚通过使用“WCF 服务应用程序”(请参阅​​ New..Project)进行调试来测试它,该应用程序提供服务WCF 服务。我使用这种方法进行调试,因为我可以使用调试器无缝地从 WCF 客户端进入 WCF 服务,然后再返回,这有时非常有用。

我还刚刚通过将其部署到控制台应用程序和服务应用程序来测试此方法,因此它绝对适合我。

I have Tuples working nicely with .NET 4.0 and WCF (reminder: you need .NET 4.0 for Tuple support).

Here is the unit test method (which calls the method via the WCF layer):

/// <summary>
/// Test Tuples
/// </summary>
[TestMethod()]
public void WcfTestTupleUnit()
{
  Tuple<double, double> x;
  x=CallViaWCF.testTuple();
  Assert.AreEqual(x.Item1, 42);
  Assert.AreEqual(x.Item2, 43);
}
#endregion

Here is the interface:

[OperationContract]
Tuple<double, double> testTuple();

Here is the implementation:

public Tuple<double, double> testTuple()
{
  return new Tuple<double, double>(42, 43);
}

I just tested it by debugging using a "WCF Service Application" (see New..Project), which serves the WCF service. I use this method for debugging, as I can use the debugger to step seamlessly from the WCF client into the WCF service , and back again, which is quite useful at times.

I've also just tested this method by deploying it to both a console app, and a service app, so its definitely working for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文