Wcf4.0-discovery

Source: Internet
Author: User

In the previous WCF, when the client calls a service, the address of the service must be known first.

What happens when the client cannot determine the address? An inappropriate example: When you do not know a person's domicile but want to find him?

The Discovery Function released by wcf4.0 may meet this requirement.

Wcf4.0-discovery provides the ad-hoc & managed mode.

Details:

Http://msdn.microsoft.com/en-us/library/dd456791.aspx

Here is just a small demo to help you find the code. ^_^.

Running Effect: (two running service ends)

2 consolesProgram.

1. Service end

Program. CS

 Program. CS  Namespace Wcfdiscoveryserver { Class Program { Static  Void Main ( String [] ARGs) {URI baseaddress = New Uri (" Http: // localhost: 8000/ "+ Guid. newguid (). tostring ()); Using (Servicehost = New Servicehost ( Typeof (Loginservice), baseaddress) {servicehost. addserviceendpoint ( Typeof (Ilogin ), New Wshttpbinding (), String. Empty );// Discoverable @ UDP multicast Servicehost. description. behaviors. Add ( New Servicediscoverybehavior (); servicehost. addserviceendpoint ( New Udpdiscoveryendpoint (); servicehost. open (); console. writeline (" Service started at {0} ", Baseaddress); console. writeline (); console. writeline (" Press any key to terminate the service. "); Console. writeline (); console. readkey ();}}}}

Ilogin. CS

Ilogin. CSNamespaceWcfdiscoveryserver {[servicecontract]Public InterfaceIlogin {[operationcontract]StringEcho (StringInput );}}

Loginservice. CS

Loginservice. CSNamespaceWcfdiscoveryserver {Public ClassLoginservice: ilogin {Public StringEcho (StringInput ){StringResult =String. Format ("You said: {0 }@{ 1}", Input, datetime. Now); console. writeline (result );ReturnResult ;}}}

2. Client Side

Ilogin. CS

Ilogin. CSNamespaceWcfdiscoveryclient {[servicecontract]Public InterfaceIlogin {[operationcontract]StringEcho (StringInput );}}

Program. CS

 Program. CS  Namespace Wcfdiscoveryclient { Class Program { Static   Void Main (String [] ARGs) {discoveryclient = New Discoveryclient ( New Udpdiscoveryendpoint (); console. writeline (" Finding ilogin endpoints... "); Console. writeline (); // Find ilogin service endpoints Findresponse = discoveryclient. Find ( New Findcriteria ( Typeof (Ilogin); console. writeline (" Found {0} ilogin endpoint. ", Findresponse. endpoints. Count); console. writeline (); endpointaddress address; If (Findresponse. endpoints. Count> 0) {address = findresponse. endpoints [0]. address; Foreach (VAR item In Findresponse. endpoints) {console. writeline (item. Address. Uri. tostring ();} ilogin loginservice = channelfactory <ilogin>. createchannel ( New Wshttpbinding (), findresponse. endpoints [0]. Address); console. writeline (loginservice. Echo (" Test ");} Console. writeline ("Press any key to exit. "); Console. writeline (); console. readkey ();}}}

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.