WCF Simple Example in Visual Studio 2010 – part 2
This topic is covered in multiple posts
- Creating simplest solution with default endpoints
- Manually adding and configuring the endpoints (you’re reading it right now)
- More to come…
The previous post was the most simple example of creating a WCF service and calling it from a client application. In this post you can read how you can achieve the exact same behavior, but now manually adding every bit of configuration. So we’ll basically override the default endpoints, configure our won, resulting in exactly the same behavior.
The idea behind this is that you understand what WCF does and that you can do it yourself. Besides that, the previous .NET Framework versions don’t support the default endpoints, meaning that the solution at the end of this article also works in Visual Studio 2008.
What will we do?
- Add BasicHttpBinding endpoint
- Add MEX endpoint
- Add Metadata behavior
We’ll start with bullet three, simply because almost everything is already in place. The solution from the previous weblog post ended with this in the application configuration from 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>
The service behavior has no name, making it the default for all services in WCF4. All we have to do is give it a name. Change line 6 into the following.
Adding BasicHttpBinding endpoint
We’ll now add the first endpoint using the BasicHttpBinding. Right under <system.serviceModel> we’ll add the <services /> tag and add our service. Completely empty this will result in the following.
<services> <service name=""> <endpoint address="" binding="" contract="" /> </service> </services>
This is a bit misleading, as you can’t just enter any name, it’s actually the type of the service.
This must be the fully qualified name of the implementation of our service. This means not the interface, but the class. Its name is EmailValidator but its full name includes the namespace, resulting in EmailService.EmailValidator.
The address we don’t have to fill in, because the base address in our code already defines it. You can also define this in configuration as you can read in the post Address.
The binding is simply basicHttpBinding. Note the camelCasing.
Here we should enter the contract, which is our interface. As with the name attribute, we need to enter the fully qualified name, resulting in EmailService.IEmailValidator.
You can view the end result at the end of this article and in the download, also at the end of this article.
Adding MEX endpoint
The MEX endpoint needs the metadata behavior configured, but we’ll get back to that. First we need to add the endpoint. Again the WCF ABC, an address, binding and contract.
Because we cannot have this endpoint at the same address as the BasicHttpBinding endpoint, we need to enter “mex” here.
This is simple, it’s just mexHttpBinding
This is simple as well, although a bit weird, it HAS to be IMetadataExchange. Not the fully qualified name or anything, it has to be just this.
Enabling the metadata behavior
Now all we have to do is add the metadata behavior. This is done at the root of the service, where we first entered the name. There’s another attribute there called behaviorConfiguration. We have to enter the name of our behavior configuration there, which is MyBehavior.
This results in the following configuration.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="EmailService.EmailValidator" behaviorConfiguration="MyBehavior"> <endpoint address="" binding="basicHttpBinding" contract="EmailService.IEmailValidator" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="MyBehavior"> <serviceMetadata httpGetEnabled="True"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>