This is the Day 12 article. If you have not read the previous articles, please go through the following articles:
- Day 1 - WCF Introduction and Contracts
- Day 2 - WCF Fault Contracts
- Day 3 - WCF Message Exchange Patterns
- Day 4 - WCF DataContract
- Day 5 - WCF Difference between service application and service library
- Day 6 - WCF Serialization Part 1
- Day 7 - WCF Serialization Part 2
- Day 8 - WCF Opt-In VS Opt-Out
- Day 9 - WCF Message Contract
- Day 10 - WCF Address Binding & Contract
- Day 11 - WCF Service Configuration Using Web.Config
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.
Right-click on App.config and select Edit WCF Configuration to open the WCF Configuration Editor.
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
Configuration Editor
Base Address
App.config
Configuration Editor
Service Behavior
App.config
Configuration Editor
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".
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.
Now navigate to the path "\bin\Debug\EditConfigurationServiceLib.dll" and select the contract.
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.
Now click on services and it will show endpoints for the service, as in:
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.
Select TCP and click on "Next" button, as in:
Specify the address in the address text box.
Click on the "Finish" button.
It will show an address, binding and contract for netTcpBinding, which we just created.
Click on services, which now shows one more binding i.e. netTcpBinding; see:
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.
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.
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".
Give the configuration name as "Metadata" and click on the "Add" button.
Select "serviceMetadata" from different behaviors.
Now select "serviceMetadata" from the left panel and set the "HttpGetEnabled" attribute to true.
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.
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.