WCF能够保持在导线参考平等? [英] Can WCF keep reference equality over the wire?
本文介绍了WCF能够保持在导线参考平等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设你有定义为
[DataContract]
public class Foo
{
[DataMember]
public List<Bar> Bars {get; set;}
}
[DataContract]
public class Bar
{
[DataMember]
public string Baz { get; set; }
}
public class Service1 : IService1
{
public bool Send(Foo foo)
{
var bars = foo.Bars;
bars[0].Baz = "test2";
return bars[0].Baz == bars[1].Baz;
}
}
[ServiceContract]
public interface IService1
{
[OperationContract]
bool Send(Foo composite);
}
假设我使用WCF与客户端之间共享数据合同的DLL WCF和服务器,如果我做类似如下
Assuming I am using WCF to WCF with a shared data contract DLL between the client and server, if I do something like the following
static void Main(string[] args)
{
using (var client = new ServiceReference.Service1Client())
{
var bar = new Bar();
bar.Baz = "Start";
List<Bar> bars = new List<Bar>();
bars.Add(bar);
bars.Add(bar);
var foo = new Foo();
foo.Bars = bars;
Console.WriteLine(bars[0].Baz == bars[1].Baz);
bars[0].Baz = "test1";
Console.WriteLine(bars[0].Baz == bars[1].Baz);
Console.WriteLine(client.Send(foo));
Console.ReadLine();
}
}
我得到真
,真
,假
我的结果,这意味着酒吧[0]
和吧[1]
并没有指向服务器上的同一个对象。
I get True
, True
, False
as my result which means that bars[0]
and bars[1]
did not point to the same object on the server.
我做得不对,或者是它不可能在WCF已经共享引用?
Am I doing something wrong, or is it impossible to have shared references over WCF?
推荐答案
您有来明确地告诉WCF保存引用通过指定
[DataContract(IsReference = true)]
否则引用消息施工过程中的平等会丢失。
otherwise reference equality is lost during message construction.
这篇关于WCF能够保持在导线参考平等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文