Data contract of wcfwcf Data Protocol

Source: Internet
Author: User
What is contractWhat is data protocol?
In the abstract, all the WCF provides for is the ability to host and expose native CLR types as servies, and the ability to consume services as native CLR interfaces and classes. WCF Service Operations accept and return CLR types such as integers and string, and the wcfclients pass in and process returned CLR types. as a result, any client, regardless of its own technology, can ineract with the service. this obviusly mean that WCF cannot allow you to expose the CLR data types should ss the service boundary. what u need is a way of converting CLR types to and from a standard neutral represention. that reprepresentation is a simpel XML-based schema or an inset. in adddtion, the service needs a formal way for declarging how such conversion is to take place, this formal way is calledData contract
Theoretically, WCF can start and publish local CLR types as services, and call services as local CLR interfaces and classes. All WCF Service operations can accept and process the returned data as defined by CLR, such as integer and character creation. Therefore, no matter what technology the client uses, it can interact with the service. However, there is a limit that the data operated cannot surpass the service boundary of the CLR type ). in this case, we need to be able to use a platform-independent neutral way of expressing data, which can be converted to CLR types or converted to each other. Therefore, this expression is based on the XML mode or information set. In addition, the Service requires a formal representation to indicate how to perform such a conversion. This is Data Protocol
Datacontract attribute
Public sealed class datacontractattribute: attribute

{

Public string name {Get; set ;}

Public String namespace {Get; set ;}

} // Datacontract mainly contains two properties: Name and namesapce
Public sealed class datamemberattribute: attribute

{

Public bool isrequired {Get; set ;}

Public string name {Get; set ;}
Public int order {Get; set ;}

} // Datamember mainly includes name and order

We can apply the datacontract and datamember attribute like this:
// We use the following syntax to define a Data Protocol
[Datacontract]
Struct mydata

{
[Datamember]

Public String firstname;

[Datamember]
Public String lastname;

}

// Comments: Data contracts are case-sensitive. Note: data protocols are case sensitive.

Importing a data contract on Client
When a data contract is used ina contract operation, it is published in the Servce metadata. when the client imports the definiton of the Data contract, the client will end up with an equivalent definition, but not an identical one. the imported definition will maintain the original type designation of a class or a structure. in addtion, unlike a service contract, by default the imported definition will maintain the original type namespace.
When a data protocol is used in protocol operations, the data protocol is published using service metadata. At the same time, when the client imports our data protocol, the client will also generate peer-to-peer data definitions, but not necessarily the same definition format.
For example:
E. G1

Service-side Definition

NamespaceMynamespace
{

[Datacontract (namespace = "myothernamespace")]
Struct contract

{...}

}

The imported definiton on client-side will be

NamespaceMythernamespace
{

[Datacontract]

Struct contract

{...}

}

The imported definiton will always have propertiyes decorated with the datamember attribute, even if the original type on the Service side did not define any properties.If the original service-side definition applied the datamemer attribute on filelds directly, then the imported type definition will have properties accessing fields whose names will be the name of the datamember suffixed with field.
Even if the server does not define any property, a property modified with [datamember] is generated for the data definition imported on the client. If the definition on the server side is defined directly through field, the client will also generate property to access its field, and the field generated on the client will be added.
E. g 2

Service-side

[Datacontract]

Struct contract

{

[Datamember]

Public String firstname ;

[Datamember]

Public String lastname ;

}

Then the imported client-side definition will be:

[Datacontract]

Public partial struct contract

{

String firstnamefield ; // Suffixed with Field Along Original service-side firstname;

String lastnamefield ; // Suffixed with Field Along Original service-side lastname;


[Datamember]

Public String firstname

{

Get

{

Return firstnamefield;

}

Set

{

Firstnamefield = value;

}

}


[Datamember]

Public String lastname

{

Get

{

Return lastnamefield;

}

Set

{

Lastnamefield = value;

}

}

}

// The client can of course manually rework any imported definition to be just like a service-side denifiton.
// Of course, we can also manually modify the definition on the client to be the same as that on the server.

Composite data contracts combined data protocol
Wen we define a data contract, we can apply the datamember attribute on members that are themselves data contracts.
As shown the following
When defining a Data Protocol, its datamember attribute can also be applied to a data protocol, that is, its datamember is also a data protocol.
[Datacontract]
Struct address
{
[Datamember]
Public String Street;
[Datamember]
Public String city;
[Datamember]
Public String state;
}

[datacontract]
struct contact
{< br> [datamember]
Public String firstname;
[datamember]
Public String lastname;
[datamember]
Public Address;
}< br>

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.