The chat application works OK, but you may have noticed an issue. When Wilma discovered other chat instances all you knew about them was the Uri of the service endpoint. Therefore, her chat window shows a chat with the host machine name. Fred, on the other hand, knew that the chat message came from Wilma because the chat message included her name. If you could add the name along with the discovery messages, it would be possible to display the names in the user box instead of the endpoints. In this exercise, you will learn how you can extend the metadata used in WS-Discovery to supply additional information (such as the username used for the chat session).
Task 0 – Opening the Solution
To begin this exercise you can use the solution you finished from Exercise 4. Alternatively, you can follow the following steps to begin with Exercise 5.
- Open the starting solution for Exercise 5 located under the Source\Ex5-MetadataExtensions\Begin (choosing the folder that matches the language of your preference.) Use it as the starting point for this exercise.
- Press CTRL+SHIFT+B to build the solution.
Task 1 – Adding an EndpointDiscoveryBehavior with Extensions
- Open the SimpleChat class in code view from the DiscoveryChat project. You can open the code view by selecting the file, and pressing F7.
You can add XML to the endpoint metadata when responding to a discovery probe. To do this, you will need to add namespaces directives.
(Code Snippet - What is new in WCF4 Lab – ServiceModel.Description CSharp)
using System.ServiceModel.Description;
(Code Snippet - What is new in WCF4 Lab – ServiceModel.Description VB)
Imports System.ServiceModel.Description
Locate the OpenServices method, and modify it as shown in the following code. Here you are creating an EndpointDiscoveryBehavior instance, and as part of that behavior you are a adding an XML element, in this case the user name to the extension collection. This extension is sent along with the responses to clients looking for that endpoint. Lastly, you are applying that behavior to your service endpoint.
(Code Snippet - What is new in WCF4 Lab – OpenServices Method CSharp)
private void OpenServices()
FakePre-0f67741420fb497da0903e6fab6bf01f-a6720482241241d7a1378f1865cb66b5FakePre-d90919ebe32e40c48f5c095f0980092b-654a5f1af59d469797985404c5b06bc3FakePre-85eb45f574574767ac05812a868ff198-9e9571cd38fa4a73b011eedd6db711e8FakePre-03eb7c12f29d4aaab8f64b45512ec99b-fb3c637438bf4820a69e9be5a32e9790FakePre-867f3f3acfd942ed8aeca9090f84002c-e19dc0c53dac459e9926def6f433cc7b // Create a discovery behavior EndpointDiscoveryBehavior endpointDiscoveryBehavior = new EndpointDiscoveryBehavior(); // Add an extension element with the username endpointDiscoveryBehavior.Extensions.Add( new XElement( "root", new XElement("Name", this.userName))); // Find the endpoint ServiceEndpoint simpleEndpoint = this.chatServiceHost.Description.Endpoints.Find(typeof(ISimpleChatService)); // Add your behavior to the endpoint before opening it simpleEndpoint.Behaviors.Add(endpointDiscoveryBehavior);FakePre-50b5402264e747f78b3195b079e7ce2f-f09904a241d84149a15f67c897dcc1c7FakePre-3f5a29f54d244f199ff74741c217e474-f2895f30eaf64bd6a0b76f8158b83c21FakePre-22f6b2ce93f34ae888bb938b0b29d6cb-7d7bb6b41a6f4ab5a2b6a95e7afcdd36FakePre-c668d9ba3b3a4f9f8b4119ca5e070bde-b3f04185c35f4365b7522cae4042ed22FakePre-05f65f1ab1394cdcb10b1522bf1c82a5-11d62949ad7a4a0e84d3c6fb8a9c88f7FakePre-65d917b3d9dd4ed8b1a086937e20d011-c3df27c7c5e641cd9b5e2f3bfcd2060fFakePre-be193bf78a5840ff98d92010f1eb3284-961acfd41f74451b960acc19938db14eFakePre-46a2b14b1d4349c69ac21e09e26e2f25-e7ebac74cb2442f99f7b5835653ad1ceFakePre-1f0b1e77cd8c4f9c9f79f411f20e727a-5b6ce469d7384a979abc1321afd8ee83FakePre-609920ed4ca34186841ba121a63d127a-0914a63a086b4c7b8ccd19e6c9b73647FakePre-c5bc770c20664545abc0eb04deeaf8f5-5d07c30743ad47e68b31358161a40042FakePre-3a6f03837f8e474484f5b13a752b1738-029f2bb7c56644deb9b1760262aae201FakePre-b49d7b5fd8914584a6b23208e32413ac-fca0c6b243c34bbcaa82a4ac8da1c790
(Code Snippet - What is new in WCF4 Lab – OpenServices Method VB)
Private Sub OpenServices()
FakePre-0eb4092d3e6647ed8c96cc5a1770e8a4-3c24774f9f3f4b9dbd7663762af86e72FakePre-f0388bb95175418a8cfc574e71d850f6-e45a84d8d30f48b98996cc3f2f99d640FakePre-5e90ab302fe949b1b17c1bff86b2d6f7-6ca71c69a875493ea5db084a3e560d9dFakePre-5e664e6da9ed47628e40685d7fa546cc-4b857de75c14477ea33bd41078b16b15 ' Create a discovery behavior Dim endpointDiscoveryBehavior = New EndpointDiscoveryBehavior() ' Add an extension element with the username endpointDiscoveryBehavior.Extensions.Add( New XElement( "root", New XElement("Name", Me._userName))) ' Find the endpoint Dim simpleEndpoint = Me.chatServiceHost.Description.Endpoints.Find(GetType(ISimpleChatService)) ' Add your behavior to the endpoint before opening it simpleEndpoint.Behaviors.Add(endpointDiscoveryBehavior)FakePre-0e0237ef30c546459edae87739e1a1b7-337c116c8b8c474c8c47aa5e7735927fFakePre-a30bbf2a42604bcbbf81ce2762cd45e0-6fc0d54fc87e4ec88867930cb024e6faFakePre-f8317db8fa074af199ee147f381d33cd-a05401e1902242e5b5cc86be821ca7dfFakePre-9c48aa28b2194802bdbcf138e959e8a7-e71c3f3b56104166bad97d69a739e818FakePre-a89797280a40476a8ff08706246d93ba-0df1d318fbe945d68dc13bf23fbe45ecFakePre-18740ced1b9141eea001e837590a25a0-5ea0dd66b21e4dda92669526d1be09cfFakePre-fbb1386d6f4a4cabac52d8bc946d98c4-ff27aa9a01f446c69ed74f1cc839be66FakePre-9f2e89e60554433ca57860227086ce6e-874052010b4248be820fc2b7a3225ed3FakePre-418526c57959472b91deb42470a076ad-59274f12e57d4c87b83f88e5f402aa69FakePre-77c9a1cf94f04aa58f5b1cde6a425343-fbe8b3da4d05414b829566f82510f95f
Now that the name is sent along with service endpoint information, you need to modify the PopulateUserList method to invoke the GetPeerName method. The GetPeerName method returns the first node named “Name” from the extension metadata. The PeerUser class will use it as the display name in the list box. To do this, replace the current call to the AddUser method with the following one.
private void PopulateUserList(
FakePre-9ca261a03b124a5ea56ac9e3330f2978-dfabf6c4feea42e0a6eb57fd7b8c4506FakePre-1fb61e3e0c864629b6f0a44cf4413748-d7302e99459a4939a7ed89019156c23cFakePre-3696afa8059a4d2ea370ad09290ae205-0a478dc3b9ad49dc889804a4db383982FakePre-097d9001304341efa3d57007e16614d8-e2934125a8f641f3b0669fcef9128486 this.AddUser(new PeerUser(GetPeerName(endpointDiscoveryMetadata), endpointDiscoveryMetadata.Address));FakePre-1c69ab253181464b88acdfcd35dbaead-eb56c81c56364980aa6d899ac7212fa2FakePre-471a8039389a4698ae49cd6ae4c562e8-cff62c9d5ba3431ab279dd719f4e015c
Private Sub PopulateUserList(ByVal endpointDiscoveryMetadata As EndpointDiscoveryMetadata)
FakePre-0ae745aaca6d4717beb0c28f83445edb-454ae22859f24b7f886aa4dda55a115e Me.AddUser(New PeerUser(GetPeerName(endpointDiscoveryMetadata), endpointDiscoveryMetadata.Address))FakePre-a2e344dfbbd94564bfa03d68ec722013-5e91b325d6eb4f88a169ba2be9ad5971FakePre-a567516093af49779c56f496cc02c3e3-9ca902ce6d194177bd049a3ba2e0f406
- Press CTRL+SHIFT+B to build the solution.
Next Step
Exercise 5: Verification