Heavy rain, I have a strong programming flavor;
The algorithm is exquisite, and female colors are hard to work;
It is a single shadow, and tea without pearls.
You can't use your talents, but you can't use them.
Download the code in this article: minmin.zip
You are preferred to create two WCF services, which use different contract and create a console project as the client:
Specifically, the wcfservicewithdatacontract project uses datacontract to mark the object class as [datacontract]:
[DataContract]public class BookOrder{ [DataMember] public string ISBN { get; set; } [DataMember] public string BookTitle { get; set; } [DataMember] public string BookAddress { get; set; }}
Note: Even if datacontract is not added, datacontract is used by default.
The wcfservicewithmessagecontract project uses messagecontract to mark the object class as [datacontract]:
[MessageContract]public class BookOrder{ [MessageHeader] public string ISBN { get; set; } [MessageBodyMember] public string BookTitle { get; set; } [MessageBodyMember] public string BookAddress { get; set; }}
However, the operation of the two services is the same:
public BookOrder PlaceOrder(BookOrder bookOrder){ return new BookOrder() { ISBN = "2", BookTitle = "WPF", BookAddress = "China" };}
To observe more clues, we set the parameters and return value types to bookorder. The logic of the method is simply to slightly modify the parameters and return them.
Depending on the service contract (2 types) and the method of adding a service to the client (2 types), there are 2*2 = 4 cases. The analysis is as follows:
Scenario 1:Usedcrefdc (use datacontract on the server and generate datacontract on the client)
We directly add service reference on the client to point to the service: http: // localhost: 1111/service1.svc generated by the wcfservicewithdatacontract project.
At the same time, datacontract is generated by default on the client:
On the client (that is, the console project), you can call the Service as follows:
public static void TestUseDCRefDC(){ var client = new UseDCRefDC.Service1Client(); var item = new UseDCRefDC.BookOrder() { ISBN = "1", BookAddress = "USA", BookTitle = "WCF" }; var result = client.PlaceOrder(item);}
Scenario 2:Usemcrefdc (use datacontract on the server and generate messagecontract on the client)
Still add reference to the wcfservicewithdatacontract WCF Service, and click the Advanced button in the add service reference panel. The service reference settings panel is displayed. Select the "always generate message contracts" option:
Messagecontract is generated on the client.
On the client (that is, the console project), you can call the Service as follows:
public static void TestUseMCRefDC(){ var client = new UseMCRefDC.Service1Client(); var item = new UseMCRefDC.BookOrder() { ISBN = "1", BookAddress = "USA", BookTitle = "WCF" }; PlaceOrderRequest request = new PlaceOrderRequest(item); PlaceOrderResponse response = client.PlaceOrder(request); var result = response.PlaceOrderResult;}
Case 3:Usedcrefmc (use messagecontract on the server and generate datacontract on the client)
public static void TestUseDCRefMC(){ var client = new UseDCRefMC.Service2Client(); var item = new UseDCRefMC.BookOrder() { ISBN = "1", BookAddress = "USA", BookTitle = "WCF" }; client.PlaceOrder(ref item.ISBN, ref item.BookAddress, ref item.BookTitle);}
Scenario 4:Usemcrefmc (use messagecontract on the server and generate messagecontract on the client)
public static void TestUseMCRefMC(){ var client = new UseMCRefMC.Service2Client(); var item = new UseMCRefMC.BookOrder() { ISBN = "1", BookAddress = "USA", BookTitle = "WCF" }; var result = client.PlaceOrder(item);}