WCF Service Configuration Using Configuration Editor

WCF Service Configuration Using 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.

image.png

Right-click on App.config and select Edit WCF Configuration to open the WCF Configuration Editor.

image.png

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

image.png

Configuration Editor

image.png

Base Address

App.config

image.png

Configuration Editor

image.png

Service Behavior

App.config

image.png

Configuration Editor

image.png

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".

image.png

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.

image.png

Now navigate to the path "\bin\Debug\EditConfigurationServiceLib.dll" and select the contract.

image.png

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.

image.png

Now click on services and it will show endpoints for the service, as in:

image.png

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.

image.png

Select TCP and click on "Next" button, as in:

image.png

Specify the address in the address text box.

image.png

Click on the "Finish" button.

image.png

It will show an address, binding and contract for netTcpBinding, which we just created.

image.png

Click on services, which now shows one more binding i.e. netTcpBinding; see:

image.png

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.

image.png

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.

image.png

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".

image.png

Give the configuration name as "Metadata" and click on the "Add" button.

image.png

Select "serviceMetadata" from different behaviors.

image.png

Now select "serviceMetadata" from the left panel and set the "HttpGetEnabled" attribute to true.

image.png

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.

image.png

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.