pavanarya – Pavan Kumar Aryasomayajulu

Contact Email : pavan.aryasomayajulu@gmail.com

ABC’s in WCF—Part II

with 2 comments

In my previous post Getting started with WCF, we saw how to create a wcf service in visual studio and the history of wcf services.
Now in this post let us see something related to configuration in wcf services.

Backbone of WCF

What makes wcf different from web services?

There are number of factors that wcf from web services.Among them the most important are
1.Different types of protocols that wcf supports. Webservice supports only http and https protocols where as WCF
supports number protocols like tcp,http,https,msmq.

2. webservices exposes its services only through single address. WCF can expose it services using different
addresses.

So a wcf service has 3 most important things associated with it. They are.

1.Address(Where the service is present)
2.Binding(How the service communicates)
3.Contracts(What are the services our wcf exposes).

Simple WCF Service

Now let us create a simple wcf service in visual studio and let us analyze ABC of that service

1.Create a new project of type WCFService application.
2. by default it will create a file called IService1.cs. This is an interface. Open this file and modify the code as shown below.

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code 

and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        int Add(int a,int b);
    }
}

So in the above code we are creating a function called Add in our interface and we are planning to expose that to external world.

So in order to expose a method to outer world it should be decorated with OperationContract Attribute. And the
service should be decorated with the attribute ServiceContract.

4.Now let us open our Service1.svc.cs file and modify its content as follow.

Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and 

config file together.
    public class Service1 : IService1
    {
        public int Add(int a,int b)
        {
            return a + b;
        }

    }
}

So in the above class we are trying to implement the interface IService1 in our Service1.cs class. So we are implementing the Add method in this class.

Configuring our WCF

When we create a wcf service application by default it will create a web.config file that contains all the configuration
related data.

web.config

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

Endpoints in WCF
An Endpoint is nothing but it is just like a portal for communicating with outer world.

A wcf exposes its services through Endpoints. An Endpoint contains various attributes like Address, Binding,

Contract and some option attributes. These 3 values places an important role the wcf configuration.

A WCF service should have atleast one endpoint or else we will get an exception when we try to access the service stating

“Atleast one endpoint should be there”.

Where is My Endpoint section in the web.config file written above

In one of the above lines i am mentioning that EndPoints are key and there should be atleast one endpoint defined for the service.
But in the web.config file written above , we cannot find any endpoint defined. So you might be thinking that i am wrong
and feel that we can have a wcf service without endpoints.

Yes you are correct to some extent and wrong to some extent.

We cannot have a wcf service without an endpoint. But from wcf4.0 there was an option provided by the framework.

It is not mandatory to have endpoinds defined but a default end point will be created at the runtime.

The default endpoint uses basichttpBinding.

Browse the svc file in the browser and we will get a url.

Now create a console application in the same solution. Add the wcf reference using the above url.

Now a default app.config file will be created.

If we see the app.config file we can see something like this.

<client>
            <endpoint address="http://localhost:3190/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="Test.IService1"
                name="BasicHttpBinding_IService1" />
 </client>

So even though we dont have an endpoint at the server side web.config file we can see endoint section in the client side
app.config file.

If we can see above end all the three mandatory items address,binding,contract are present.

Address
When we define an endpoint we can specify address in number of formats.

1. We can give the ip address or host name or ip address with port number or hostname with port number.

It is the basic url that specifies where the wcf is hosted. Clients make use of this url to connect to the web service.

http://localhost:3190/Service1.svc

We can specify the address in two way.
1. By providing absolute url
2. By providing relative url

We will discuss these methods in my next post. For now we will work by providing only absolute url in the address when defining endpoints.

Binding

A Binding defines how a wcf service should interact with clients.

Wcf supports wide number of protocols apart from http and https supported by web services.
Different binding supported by wcf services.

Binding Description Web.config Name Protocol support
BasicHttpBinding

This is same as the protocol used by web service.This is the default binding basicHttpBinding Http
WSHttpBinding Web services with WS-* support. It Supports transactions. it is equivalent to web services with web security wsHttpBinding Http
WSDualHttpBinding it supports security and transaction and it is duplex wsDualHttpBinding Http
NetTcpBinding It supports security and it provides support for tcp protocol and it is reliable netTcpBinding Tcp
NetMsmqBinding It is used for communication between wcf server and client using MSMQ. Supports transactions netMsmqBinding MSMQ
NetNamedPipeBinding It is used for communication between wcf server and client using NamedPipes. Supports duplex contracts and transactions netNamedPipeBinding NamedPipes

If we want to create a simple wcf service that requires no security and requires http protocol then we will be using basicHttpBinding

If we want to create a simple wcf service that requires security and requires http protocol then we will be using wsHttpBinding

If we want to create a simple wcf service that requires security and requires tcp protocol then we will be using netTcpBinding

Contract

A contract is nothing but collection of operations that are exposed by wcf service using Endpoint.

When defining an end point we will specify the name of the interface that was decorated with Servicecontrat as Contract.

There are different types of contracts supported by the WCf service.They are

1. Service Contract
2.DataContract
3.MessageContract
4.FaultContract.
5.Operation Contract.

[ServiceContract] attribute in wcf is equivalent to [WebService] attribute in web services. An interface should be declared with this attribute inorder to expose all the operation defined inside the interface.

[operationContract] attribute in wcf is equivalent to [Webmethod] attribute in web services. All the methods in the interface with operation contract were exposed to external world through wcf service

We will go through these contracts in my upcoming article.

An Example web.config with Endpoints

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfService1.Service1"  behaviorConfiguration="ServiceBehaviour1" >
        <endpoint name="test" address="http://localhost:3190/Service1.svc"  binding="basicHttpBinding" contract="WcfService1.IService1"></endpoint>
      </service>
    </services>
    
    
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour1">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

<endpoint name="test" address="http://localhost:3190/Service1.svc"  binding="basicHttpBinding" contract="WcfService1.IService1"></endpoint>

Here we are specifying the address of the endpoint with absolute address, Binding as “basicHttpBinding” and specifying the contract with the name of the interface.

We can have as many Enpoints as we want in the wcf service.

In my next post i am planning to write an article on how to configure the web.config with different configurations

Thanks,
Pavan

Written by pavanarya

August 31, 2012 at 2:42 pm

Posted in WCF

2 Responses

Subscribe to comments with RSS.

  1. Thanks , I’ve recently been searching for information approximately this topic for a while and yours is the greatest I’ve came upon so far. However, what about the conclusion? Are you certain about the source?

    Amedar Consulting Group

    September 2, 2012 at 6:31 am

  2. if you want to expose the WSDL to clients. Visual Studio (or wcfutil.exe) needs the WSDL description to create the webservice client classes.

    silver account

    September 2, 2012 at 9:34 pm


Leave a comment