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 ();}}}