When a client references a WCF Service, a unique namespace must be defined for the service. By default, the classes used in this service are imported into this namespace. In this way, when you reference the same data contract in different services, you will encounter a problem: On the client, the same data contract is imported into different namespaces and cannot be converted.
In the following example, the PersonService and ContractService both include references to the Person data contract. However, the Person object obtained through the PersonService cannot be used as a parameter in the ContractService, because on the client, they are referenced in different namespaces and are considered as two different classes.
1: [ServiceContract]
2: public interface IPersonService
3: {
4: [OperationContract]
5: Person GetPerson();
6: }
7:
8: public class PersonService : IPersonService
9: {
10: public Person GetPerson()
11: {
12: Person person = new Person();
13: person.ID = 0;
14: person.Name = "Leslie";
15: person.Age = 34;
16: return person;
17: }
18: }
19:
20: [ServiceContract]
21: public interface IContractService
22: {
23: [OperationContract]
24: Contract GetContractByPerson(Person person);
25: }
26:
27: public class ContractService : IContractService
28: {
29: public Contract GetContractByPerson(Person person)
30: {
31: Contract contract=ContractManager.GetContractByPerson(person);
32: .....
33: return contract;
34: }
35: }
36:
From Visual Studio 2008, the system provides the "Shared data contract" function for customers. First, include the data contract to be shared in a set and generate a Model. dll file. When the client references this Assembly, when adding a service reference, select "advanced"-> "re-use the type (R) in the referenced assembly) "->" reuse the type (S) in the referenced specified assembly ", and then select Model. In this way, the client will use the Person object in Model. dll at the same time.
Related Articles
Simple WCF development example
Use AJAX + WCF for page development
Shared data contract
Reliability session Function
Custom binding
If you are interested in JAVA and. NET development, join the QQ group: 162338858
Cnblogs blog Park: http://www.cnblogs.com/leslies2/
Csdn blog: http://blog.csdn.net/leslies2
Original Works. Indicate the author and source when reprinting.