Partilhar via


Object References

Download sample

This sample demonstrates how to pass objects by references between server and client. The sample uses simulated social networks. A social network consists of a Person class that contains a list of friends in which each friend is an instance of the Person class, with its own list of friends. This creates a graph of objects. The service exposes operations on these social networks.

In this sample, the service is hosted by Internet Information Services (IIS) and the client is a console application (.exe).

Note

The WCF samples may already be installed on your machine. Check for the following (default) directory before continuing.

<InstallDrive>:\Samples\WCFWFCardspace

If this directory does not exist, click the download sample link at the top of this page. Note that this link downloads and installs all of the WF, WCF, and CardSpace samples. The sample is located in the following directory.

<InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Contract\Data\ObjectReferences

Note

The set-up procedure and build instructions for this sample are located at the end of this topic.

Service

The Person class has the DataContractAttribute attribute applied, with the IsReference field set to true to declare it as a reference type. All properties have the DataMemberAttribute attribute applied.

[DataContract(IsReference=true)]
public class Person
{
    string name;
    string location;
    string gender;
    int age;
    List<Person> friends;
    [DataMember()]
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    [DataMember()]
    public string Location
    {
        get { return location; }
        set { location = value; }
    }
    [DataMember()]
    public string Gender
    {
        get { return gender; }
        set { gender = value; }
    }
…
}

The GetPeopleInNetwork operation takes a parameter of type Person and returns all the people in the network; that is, all the people in the friends list, the friend's friends, and so on, without duplicates.

public List<Person> GetPeopleInNetwork(Person p)
{
    List<Person> people = new List<Person>();
    ListPeopleInNetwork(p, people);
    return people;
    
}

The GetMutualFriends operation takes a parameter of type Person and returns all the friends in the list who also have this person in their friends list.

public List<Person> GetMutualFriends(Person p)
{
    List<Person> mutual = new List<Person>();
    foreach (Person friend in p.Friends)
    {
        if (friend.Friends.Contains(p))
            mutual.Add(friend);
    }
    return mutual;
}

The GetCommonFriends operation takes a list of type Person. The list is expected to have two Person objects in it. The operation returns a list of Person objects that are in the friends lists of both Person objects in the input list.

public List<Person> GetCommonFriends(List<Person> people)
{
    List<Person> common = new List<Person>();
    foreach (Person friend in people[0].Friends)
        if (people[1].Friends.Contains(friend))
            common.Add(friend);
    return common;
}

Client

The client proxy is created using the Add Service Reference feature of Visual Studio.

A social network that consists of five Person objects is created. The client calls each of the three methods in the service.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

See Also

Reference

IsReference

Other Resources

Interoperable Object References

© 2007 Microsoft Corporation. All rights reserved.