WCF secrets-shared data contract

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.