WCF Service Configuration Using Configuration Editor- Day 12

In this article, you will learn about WCF Service Configuration using the Configuration Editor.

This is the Day 12 article. If you have not read the previous articles, please go through the following articles:

Introduction

 
Let us configure a WCF Service using the WCF Service Configuration Editor.
 
Open the Visual Studio editor and select "File" -> "New" -> "Project...". Select the WCF Service Library from the template and provide an appropriate name for it.
 
In the web.config file you can see under the system.serviceModel element, there are two endpoints specified. One is for wsHttpBinding and another is to expose metadata using the IMetadataExchange interface. The base address is also specified under the host element. In service behavior with the serviceMetadata element set httpGetEnabled attribute to true. This is the default configuration in the WCF Service Library.
 
WCF1.jpg 
 
Right-click on App.config and select Edit WCF Configuration to open the WCF Configuration Editor.
 
WCF2.jpg 
 
The WCF Service configuration information is contained under the system.servicemodel element. So now check how this configuration is shown in the configuration editor.
 

Endpoints

 
App.config
 
WCF3.jpg 
 
Configuration Editor
 
WCF4.jpg 
 

Base Address

 
App.config
 
WCF5.jpg 
 
Configuration Editor
 
WCF6.jpg 
 

Service Behavior

 
App.config
 
WCF7.jpg 
 
Configuration Editor
 
WCF8.jpg 
 

How to add new endpoint?

 
There are two approaches to add a new endpoint using this configuration editor. Use the following to create a new endpoint.
 
Approach 1
 
Right-click on the Endpoints folder, select "New Service Endpoint".
 
WCF9.jpg 
 
In the general tab, insert the address as "basic" and select "basicHttpBinding" for the binding. For contract click on the button which is available on the right side.
 
WCF10.jpg 
 
Now navigate to the path "\bin\Debug\EditConfigurationServiceLib.dll" and select the contract.
 
WCF11.jpg 
 
And click on the "Open" button.
 
This will add a contract to your binding. Now you will see an address, binding and contract for basicHttpBinding.
 
WCF12.jpg 
 
Now click on services and it will show endpoints for the service, as in:
 
WCF13.jpg 
 
Here you will see that one more endpoint is added, as the one covered in an orange rectangle in the image.
 
Approach 2
 
On the preceding image, there is a link "Create New Service Endpoint…" to create a new endpoint. So click on this link. The Contract is selected by default. Now click on the "Next" button.
 
WCF14.jpg 
 
Select TCP and click on "Next" button, as in:
 
WCF15.jpg 
 
Specify the address in the address text box.
 
WCF16.jpg 
 
Click on the "Finish" button.
 
WCF17.jpg 
 
It will show an address, binding and contract for netTcpBinding, which we just created.
 
WCF18.jpg 
 
Click on services, which now shows one more binding i.e. netTcpBinding; see:
 
WCF19.jpg 
 
We have exposed two more endpoints i.e. for basicHttpBinding and netTcpBinding. Now go to the file menu and save these changes. Now open the app.config file. It will reflect all the changes done through the configuration editor.
 
WCF20.jpg 
 

How to specify base address?

 
Select host under the services and in the bottom-right side there are buttons to add new base addresses and to update base addresses. You can select any according to your requirement. After creating or changing the base address, save these changes and check in the app.config file for reflection of these changes.
 
WCF21.jpg 
 

How to set service behavior?

 
Go to the Advanced section and select Service Behavior. On the right panel click on the link "New Service Behavior Configuration".
 
WCF22.jpg 
 
Give the configuration name as "Metadata" and click on the "Add" button.
 
WCF23.jpg 
 
Select "serviceMetadata" from different behaviors.
 
WCF24.jpg 
 
Now select "serviceMetadata" from the left panel and set the "HttpGetEnabled" attribute to true.
 
WCF25.jpg 
 
The "Metadata" behavior is created now. The next step is to assign this behavior to a service. For that select the service "EditConfigurationServiceLib.EditConfigurationService" and select a newly created behavior for the BehaviorConfiguration attribute.
 
WCF26.jpg 
 
Finally, save the changes and check the app.config file for these changes.
 

Conclusion

 
In my previous article, we have seen WCF service configurations using web.config and in this article, we covered the same thing except using the configuration editor.