⬆️ ⬇️

A simple example of using WCF in Visual Studio 2010. Part 2

You are reading the second part:



image



The previous part described the simplest example of creating a WCF service and calling it from a client application. In this part, you can learn how to achieve exactly the same result, but now we will customize everything manually. Thus, we will override the standard endpoints, configure our own, and end up with exactly the same behavior of our application.



The idea is that you understand what WCF is doing and that you can do it yourself. In addition, previous versions of the .NET framework do not support standard endpoints, and this means that the solution we get at the end of this article will also work in Visual Studio 2008.



What do we do?

  1. Add end points to BasicHttpBinding
  2. Add MEX endpoints
  3. Add Metadata Behavior


Behavior of metadata



We will start immediately from the third point simply because here we are almost ready to go. The solution from the previous post contained the following application configuration for the ConsoleHost application.

<? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  1. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  2. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  3. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  4. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  5. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  6. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  7. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  8. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  9. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  10. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  11. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
  12. <? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? > < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior > < serviceMetadata httpGetEnabled ="True" /> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > * This source code was highlighted with Source Code Highlighter .


The behavior of the service has no name, which makes it standard for all services in WCF4. All we have to do is give him a name. Modify line 6 as follows:

  1. < behavior name = "MyBehavior" >
* This source code was highlighted with Source Code Highlighter .


Adding a BasicHttpBinding endpoint



Now we will add the first endpoint using BasicHttpBinding. Right under <system.serviceModel> we add the <services /> tag and add our service. Leaving all values ​​empty, we get the following:

  1. < services >
  2. < service name = "" >
  3. < endpoint
  4. address = ""
  5. binding = ""
  6. contract = "" />
  7. </ service >
  8. </ services >
* This source code was highlighted with Source Code Highlighter .


Now we need to fill in the blanks. More information can be found in the WCF ABC articles and hosting the service .

You will be able to see the final result at the end of this article, and also download the solution, the link to which is also available at the end of the article.

')

Adding MEX End Point



The MEX endpoint requires customized metadata behavior, but we will come back to this later. First we need to add an end point. WCF ABC again, address, link and contract.



Enabling (enabling) metadata behavior



Now we just have to add the behavior of the metadata. This is done in the service ad tag, next to her name. Add the behaviorConfiguration attribute and specify the name of our behavior configuration: MyBehavior .



Final result



As a result, we got the following configuration:

  1. <? xml version = "1.0" encoding = "utf-8" ? >
  2. < configuration >
  3. < system.serviceModel >
  4. < services >
  5. < service name = "EmailService.EmailValidator" behaviorConfiguration = "MyBehavior" >
  6. < endpoint
  7. address = ""
  8. binding = "basicHttpBinding"
  9. contract = "EmailService.IEmailValidator" />
  10. < endpoint
  11. address = "mex"
  12. binding = "mexHttpBinding"
  13. contract = "IMetadataExchange" />
  14. </ service >
  15. </ services >
  16. < behaviors >
  17. < serviceBehaviors >
  18. < behavior name = "MyBehavior" >
  19. < serviceMetadata httpGetEnabled = "True" />
  20. </ behavior >
  21. </ serviceBehaviors >
  22. </ behaviors >
  23. </ system.serviceModel >
  24. </ configuration >
* This source code was highlighted with Source Code Highlighter .


You can download the solution for Visual Studio 2010 and Visual Studio 2008 right here .

Source: https://habr.com/ru/post/116872/



All Articles