SIGN UP MEMBER LOGIN:    
ARTICLE

Default Protocol Mapping in WCF 4.0

Posted by Dhananjay Kumar Articles | WCF with C# December 06, 2010
In my previous article, I talked about Default End Point in WCF 4 . If we closely look at the output generated by default, WCF maps protocol to binding as described here.
Reader Level:


In my previous post, I talked about Default End Point in WCF 4. If we closely look at the output generated by default, WCF maps protocol to binding as below.

wcf1.gif

So if we are not configuring any EndPoint, then for http type base address in default EndPoint created by WCF will have basicHttpBinding.

If we see the above mapping, by default http type is mapped to basicHttpBinding, net.tcp type is mapped to netTcpBinding and so on.

If we want to change this, we can change this default mapping

wcf2.gif


Now when we do the above change in the Web.Config file then when we do not define any EndPoint then for HTTP type base address, WCF will create the EndPoint with wsHttpBinding.

So to understand more, what I am talking about

1. Create a WCF Service application

2. Create a Service contract as below

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData();

    }


3. Implement the Service as below

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
{
  
    public class Service1 : IService1
    {
        public string GetData()
        {
            return "Hello From Iservice1 ";

        }

    }
}

4. Now we will host this service in a console application. Create a new console application project. Add a reference to a WCF service application project and also add reference to a System.serviceModel in the console application project.

5. Right click on the console project and add app.config file to console project. Write the configuration below in App.Config.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <
system.serviceModel>
    <
protocolMapping >
      <
add  scheme ="http" binding ="wsHttpBinding" bindingConfiguration ="" />
    </protocolMapping>
  </
system.serviceModel>
</
configuration>

6. Now write the code below to fetch the default Endpoint created by WCF

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using WcfService1;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            ServiceHost host = new ServiceHost(typeof(WcfService1.Service1),
                new Uri("http://localhost:8080/service"));        

            host.Open();
            foreach (ServiceEndpoint se in host.Description.Endpoints)
            {
                Console.WriteLine("Address: " +  se.Address.ToString() +
                    "  Binding: " + se.Binding.Name +
                    " Contract :"+ se.Contract.Name);
            }
 
            Console.ReadKey(true);
 
 
        }
    }
}


And output we would get is

wcf3.gif

So we can see how we modified the default mapping using Protocol mapping in System.ServiceModel

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor