tags:

views:

25

answers:

1

I would like to set up a WCF service so that any changes a client makes to an object I send them are also reflected on the server side. For example, if Assembly A has the following...

namespace AssemblyA
{

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    [ServiceContract]
    public interface IServer
    {
        [OperationContract]
        Person GetPerson();
    }

}

And Assembly B references Assembly A...

using AssemblyA;

namespace AssemblyB
{
    class Program
    {
        static void Main(string[] args)
        {
            <snip>
            IServer server = factory.CreateChannel();
            Person person = server.GetPerson();
            person.FirstName = "Kilroy";
            person.LastName = "WuzHere";
        }
    }
}

What is the easiest/best way to make it so that the service's copy of the Person object also reflects the changes that the client makes? Is this even possible?

+2  A: 

Create a method on the server which take a Person object as parameter.

[ServiceContract]
public interface IServer
{
    [OperationContract]
    Person GetPerson();

    [OperationContract]
    void UpdatePerson( Person person )
}

and call that from the client after you have set the FirstName and LastName properties.

server.UpdatePerson( person );
Mikael Svenson
I'm not sure if this will work for what I'm trying to do... the example I gave above is very simple, but the objects I actually want to pass through are much more complex - the properties have multiple events and cause UI changes, hardware outputs, etc. I will give it a shot though.
Chris Vig
Could you say a bit more about your scenario? Do you want client events to trigger server actions, or the other way around? Are the server and client on a local network or on internet?If your Person object has an event you could implement the client code to run the Update method on the server.
Mikael Svenson
Sorry for taking so long to respond - after doing a little more research I think I was misunderstanding how WCF works and what it would be capable of doing. I have been able to implement something like you suggested that works for what I need to do. Thanks!
Chris Vig