Sending a Tuple object over WCF?

天大地大妈咪最大 提交于 2019-12-10 12:30:49

问题


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.


回答1:


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.)




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2732840/sending-a-tuple-object-over-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!