问题
I have three programs in a distributed system that rely on WCF services for communication. If I have a data type used in the WCF service library, how do I access it from the client that has referenced the service?
I am able to instantiate objects defined in the WCF service from the client console application, although there's not much else I can do with it. I can't access any of the object's member methods or fields.
For example, I have this Transaction object class that is used in the service library and client:
[DataContract]
public class Transaction
{
public int checkoutID;
public DateTime time;
public List<object> products;
public double totalPrice;
public bool complete;
[OperationBehavior]
public void Start(int ID)
{
checkoutID = ID;
products = new List<object>();
complete = false;
}
[OperationBehavior]
public void Complete()
{
time = DateTime.Now;
complete = true;
}
}
This is taken from the interface file for the service (IService) where another class is defined, called CompositeType. What am I missing? I thought WCF services allow remoting of methods and data types?
Thanks to anyone who can help, I've been having problems with WCF for a while.
回答1:
WCF is a message passing system - all the client and server share is the service contract (e.g. the service methods and the structure of the data types used).
What goes over the wire between client and server is basically an XML-serialized representation of your data contract objects. But WCF is by default not sharing code - only contracts (e.g. not data types / classes, only their XML representation).
So by default, when a client creates a client proxy for a given service, it will be able to determine the service methods and the XML shape of the data being passed back and forth - and based on that information, it will create client-side data classes that have the exact same XML representation on the wire - but they're different .NET classes (in a client-side namespace).
If you control both ends of the communication - the server and the client - and both are using .NET, you can do this:
put your service and data contracts into a separate assembly (
YourService.Contracts
or something like that)let the server-side code reference that assembly and use its interfaces and types
before creating the client-side proxy, add a reference to that shared contracts assembly
in this case, when you create the client-side proxy (using
Add Service Reference
or thesvcutil
command-line tool), the WCF runtime will detect that it knows about the types needed already (since those are defined in the shared assembly), and it will reuse those shared types instead of creating new client-side only data classes.
回答2:
I am not able to understand your question completely but from the code pasted above, you are sharing the methods with the WCF client and not the fields/properties. You need to apply [DataMember] attribute in the fields as well.
回答3:
Add the [DataMember] tag to the fields - they're serialized then.
Also, [OperationContract] should be on the methods, not [OperationBehavior]
来源:https://stackoverflow.com/questions/10384681/how-do-i-share-an-object-between-a-wcf-service-and-a-client