Wednesday, July 16, 2014

Points to remember: WCF Service

Points to remember: WCF Service

In this article I will explain some points about WCF service believe we should understand those points and their implementation also.

The main intent to write this article for those people who doesn’t understand the exact utilization of keywords and settings during WCF service creation and hosting

clip_image002

.

I have created a sample WCF service library and implement an interface having method Get info. Kindly refer an image depicted below for basic structure of web site:

clip_image004

If you notice an image shown above, I have also hosted this WCF service using console application.

Main intent to write this article starts from here.When I host this application and edit app.config file using ‘Edit WCF configuration”. Kindly see an image shown below:

clip_image005

This is a layout of app.config file as shown in image below.

clip_image007

Note:

Thus in order to generate proxies, you need metadata information. When you remove the serviceMetadata-line you say you are not providing Meta data in WSDL format.

But the line before, publishing your metadata in MEX, is still active, thus you can generate a proxy from those metadata. When you provide neither WSDL nor MEX formatted information, you cannot generate a proxy

Now let’s execute shown above steps more practically.

Point1:clip_image009

WCF service is up and running as shown in image below:

clip_image011

Initially service metadata is enabled as shown below:

clip_image012

I just paste a baseAddress url in browser, it provides us metadata in wsdl format as shown in image below:

clip_image014

But if I change it to “false” than it doesn’t provide you metadata in wsdl format. Kindly refer both images as shown below:

clip_image015

clip_image017

 

Point2:clip_image019

Now lets’ understand why mexHttpBinding is important from WCF perspective.

I’ve disabled “mexHttpBinding” code segment and host our service. It prompts us an error at time of adding Service reference to Client application.

clip_image021

‘IMetadataExchange’ contract is for enabling metadata for the service. This is required for adding service reference at WCF client side or it is a binding that returns metadata so you can build a proxy at the client side.

MEX endpoints are special endpoints that allow clients to receive the service’s metadata by using SOAP messages instead of http get requests. You can create MEX endpoint that can be accessed through http, https, tcp, and even named pipes.

Point 3:clip_image023

Third point is about to enable logging/tracing process in client application so that I could see what happen at time of calling an operation of WCF Service.

I’ve created a client application and added a reference of WCF Service, kindly see an image show below

clip_image025

To enable logging and tracing in client application edit app.config file. Kindly see below depicted image.

clip_image027

Go to Diagnostics and enables Log Auto Flush, MessageLogging and Tracing. Kindly see an image shown below:

clip_image029

Now go to Diagnostics-> Message Logging and set properties to true as shown in image below to log the exact information.

clip_image031

After setting properties to true it asks you to update your app.config file, press yes to all. And go through once with updated config file. Now we are done with all requirements and ready to consume our service via client. Press F5.

It will be first appearance of you client application as shown below in image:

clip_image033

I put some value in text box and press on GetInfo button, See the result below.

clip_image035

Now look at the .svclog files exist in root folder of client application as shown in image below:

clip_image037

Note: Don’t forget to refresh this folder once.

Double click on the app_messages file and see the result.

clip_image039

Response:

clip_image041

GreatJ .

I belief we learnt remarkable points about wcf service, which we may face in daily routine work.

You can also have a look at MVC-related articles here:

clip_image043

Keep coding and Smile Smile

1 comment :