Can WCF support link equality over a wire?

Say you have several classes defined as

[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);
}

Assuming I use WCF for WCF with a DLL to exchange data between 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();
    }

}

I get True, True, Falseas a result, which means that bars[0]and bars[1]do not point to the same object on the server.

Am I doing something wrong, or is there no way to have common links to WCF?

+3
source share
2 answers

You must explicitly specify WCF to maintain links by specifying

[DataContract(IsReference = true)]

otherwise, reference equality is lost when building the message.

+4
source

DataContact . , , . , .

, , , WCF , , . , WCF , , .

+2

All Articles