[WCF] Contract

Source: Internet
Author: User

In WCF, all services are called externally through contracts. A contract is a platform-independent and standard method for describing services. Currently, four types of contracts are defined in WCF.

L
Service Contract)

Describes the operations that a client can call a service.

L
Data contract)

Describes the data types that can be transmitted between the client and the service. CLR data types are added with data contracts by default. Of course, you can easily add data contracts for custom types.

L
Fault contract)

Describes possible exceptions in the service and how the service handles and propagates exceptions.

L
Message contract)

Messages with this contract can directly interact with the service.

 

Service Contract

Using System;
Using System. servicemodel;

namespace anrs. service
{< br> [servicecontract ()]
Public interface ianrsservicecontract
{< br> [operationcontract ()]
string getfullname ( string author );

[Operationcontract ()]
StringGetfamilyname (StringAuthor );
}

Public   Class Anrsservice: ianrsservicecontract
{
Public   String Getfullname ( String Author)
{
Return Author. Replace ( ' . ' , '   ' );
}

Public StringGetfamilyname (StringAuthor)
{
ReturnAuthor. Split ('.')[2];
}
}
}

The service contract is mapped to the CLR interface type. In terms of the contract, accessibility is irrelevant, because "accessibility" is a CLR concept, this concept does not exist in WCF. Interfaces that do not implement the servicecontract feature are invisible to the client, which implies the "boundary clearness" principle of SO. In addition, the methods in WCF must be clearly told to be part of servicecontract (using the operationcontract feature, methods that do not apply the operationcontract feature will not be considered part of the service contract ), that is to say, these methods can be called by the client. However, the operationcontract feature can only be applied to methods. It is invalid for properties/indexers/events.

A single method can also implement multiple interfaces that apply the servicecontract feature.

Using System;
Using System. servicemodel;

namespace anrs. service
{< br> [servicecontract ()]
Public interface ianrsservicecontract1
{< br> [operationcontract ()]
string getfullname ( string author );

[operationcontract ()]
string getfamilyname ( string author );
}

[Servicecontract ()]
Public InterfaceIanrsservicecontract2
{
[Operationcontract ()]
VoidSaveauthor (StringAuthor );
}

Public   Class Anrsservice: ianrsservicecontract1, ianrsservicecontract2
{
Public   String Getfullname ( String Author)
{
Return Author. Replace ( ' . ' , '   ' );
}

Public string getfamilyname ( string author)
{< br> return author. split ( ' . ' ) [ 2 ];
}

Public VoidSaveauthor (StringAuthor)
{
Console. writeline ("Save {0} to database", Author );
}
}
}

 

Name1And namespace

You can also define a namespace for servicecontract (the benefits of the namespace will not be mentioned), as shown below:

[Servicecontract (namespace = " Anrs. Service " )]
Public   Interface Ianrsservicecontract1

If the namespace is not explicitly specified
Servicecontract specifies a default namespace http://tempuri.org
. The servicecontract name must be the same as the interface name.

[Servicecontract (namespace = " Anrs. Service " , Name = " Ianrsservicecontract1 " )]
Public   Interface Ianrsservicecontract1

You can also specify the name for operationcontract.

 

[Servicecontract (namespace = " Anrs. Service " , Name = " Ianrsservicecontract1 " )]
Public   Interface Ianrsservicecontract1
{
[Operationcontract (name = " Getfullname " )]
String Getfullname ( String Author );

 

 

 

1. Specifying aliases for servicecontract/operationcontract different from the actual names of interfaces/methods will affect the metadata (metadata) sent to the client ).

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.