I would like to set up a WCF service so that any changes a client makes to an object I send them are also 开发者_C百科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?
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 );
If you are in need for handling events between the client & server, you ought to look for Duplex Contracts with WCF. This is a very good detailed example for you to start with: http://www.codeproject.com/Articles/491844/A-Beginners-Guide-to-Duplex-WCF
精彩评论