立即注册 登录
汉山 返回首页

翰山的个人空间 https://hanshan.info/?2 [收藏] [复制] [分享] [RSS]

日志

WCF (2), Contracts in Various Types

已有 5574 次阅读2021-4-11 17:31 |个人分类:Tech|系统分类:原创博文 | 宽屏 请点击显示宽屏,再点击恢复窄屏 | 动漫全图 如只见部分动漫,请点击显示全图,再点击恢复窄图

In this series of articles, we will discuss the major features of WCF:
 
 
ABC of an EndPoint in WCF:
 
Sample Image - maximum width is 600 pixels
 
Contracts are in the core position of WCF endpoints. This article will discuss more details of WCF Contracts.
 
 

Contract

 
A Contract is an agreement between the two parties, WCF contract is between a Service and a Client. In WCF, Contracts are categorized as behavioral or structural.
  • Behavioral Contract

    • ServiceContract attribute is used to mark a type as Service Contract that contains operations.
    • OperationContract attribute is used to mark the operations that will be exposed.
    • FaultContract defines what errors are raised by the service being exposed.
     
  • Structual Contract

    • DataContract attribute defines types that will be moved between the parties.
    • MessageContract attribute define the structure of the SOAP message.


Behavioral Contract


A service contract defines the operations which are exposed by the service to the outside world. A service contract is typically an interface of the WCF service and it tells the outside world what the service can do. 
  1. [ServiceContract]    
  2. public interface IService1    
  3. {    
  4.    
  5.     // TODO: Add your service operations here    

 
An operation contract is defined within a service contract. It defines the parameters and return type of an operation. An operation contract can also defines operation-level settings, like as the transaction flow of the operation, the directions of the operation (one-way, two-way, or both ways), and fault contract of the operation.
  1. [ServiceContract]
  2. public interface IService1    
  3. {    
  4.     [OperationContract]    
  5.     string GetData(int value);    
  6.     
  7.     [OperationContract]    
  8.     CompositeType GetDataUsingDataContract(CompositeType composite);    
  9.       
  10. }   
 
A Fault Contract is a way to handle an error/exception in WCF. In C# we can handle the error using try and catch blocks at the client side. The purpose of a Fault Contract is to handle an error by the service class and display in the client side.

  1. [ServiceContract]    
  2. public interface IGetDetailsService    
  3. {    
  4.      [OperationContract]    
  5.      [FaultContract(typeof(Student))]    
  6.      Student GetDetails(string Name);    
  7. }  
 

Structual Contract


The DataContract is a totally different beast --- it define the data type for variables that are the same as get and set properties but the difference is that a Data Contract in WCF is used to serialize and deserialize the complex data. It defines how data types are serialized and deserialized. Using serialization, you can convert an object into a sequence of bytes that can be transmitted over a network. Using de-serialization, you reassemble an object from a sequence of bytes that you receive from a calling application.

The DataMemberAttribute must then be applied to each member of the data contract type to indicate that it is a data member, that is, it should be serialized.

  1. [DataContract]    
  2. public class Student    
  3. {    
  4.      private string _Name;      
  5.      private string _City;    
  6.     
  7.      [DataMember]    
  8.      public string Name    
  9.      {    
  10.          get { return _Name; }    
  11.          set { _Name = value; }    
  12.      }    
  13.     
  14.      [DataMember]    
  15.      public string City    
  16.      {    
  17.          get { return _City; }    
  18.          set { _City = value; }    
  19.      }    

 
A MessageContract is an abstraction over a SOAP message that allows you to explicitly dictate the structure of the underlying message. 
 
The default SOAP message format is provided by the WCF runtime for communication between the client and the service. If it does not meet your requirements then we can create our own message format. This can bedone using the Message Contract attribute.
  1. [MessageContract]    
  2. public class Person    
  3. {    
  4.    [MessageHeader] public Operation Name;    
  5.    [MessageHeader] public string city;    
  6.    [MessageBodyMember] private Home Address;    
  7.    [MessageBodyMember] private Home Streat;    
  8.    [MessageBodyMember] public int age;    
  9. }  
The differences between DataContract and MessageContract:
  • UsingDataContract, the service can expose the types that it interchanges. But the XML (SOAP) that is interchanged is not controlled (though impacted) by it.
  • While MessageContract can be used to explicitly define how the XML (SOAP message) will be structured.
 

Summary

 
This article gave a brief discussion of WCF Contracts.
 
 

References


鲜花

真棒

玩闹

同情

看看

困惑

震惊

bad

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

Archiver|手机版|小黑屋|汉山网    

GMT-5, 2024-3-28 15:52 , Processed in 0.067893 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回顶部