WCF Part 2: defining contract

Source: Internet
Author: User

As seen in the previous article, we need an address, binding and contract to complete the wcf abc. We'll start at the contract.

A contract is definedExplicitly, Via a class. you add a [servicecontract] attribute to the class. all methods you want to expose in your service, you mark as [operationcontract], as methods are called operations in services.

[Servicecontract]

Public Class Hello

{

[Operationcontract]

StringHelloworld ()

{

Return "Hello World";

}

 

StringHellocomputer ()

{

Return "Hello computer";

}

}

The operations are definedExplicitly, A service orientation tenet. hellocomputer won't be visible by consumers of our service; it isn' t marked with an attribute. the helloworld operation however is, even though it's private inside. net World.

Interfaces
We prefer however, to have an interface for our contract and have our actual service implement the interface. That's because

    • Interfaces can extend/inherit other interfaces
    • A single class can implement multiple interfaces
    • You can modify the implementation of a service without breaking the contract
    • You can version your service via old and new interfaces

It's always best to have an interface and implement it. The attributes also must be specified in the interface.

[Servicecontract]

Public Interface Ihello

{

[Operationcontract]

StringHelloworld ();

 

StringHellocomputer ();

}

 

Public Class Hello:Ihello

{

String Ihello. Helloworld ()

{

Return "Hello World";

}

 

String Ihello. Hellocomputer ()

{

Return "Hello computer";

}

}

in the next article I'll show you how you can host this service. in the future we'll all also consume the service and I will try to tell more about contracts and versioning.

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.