SIGN UP MEMBER LOGIN:    
ARTICLE

Default End Points in WCF 4.0

Posted by Dhananjay Kumar Articles | WCF with C# December 05, 2010
For a normal developer such as I, a great challenge was working with WCF 3.x configuration of End Points in config file. A developer had to add endpoints to setup a WCF service. In WCF 4, Defualt End Point is associated with the service, if we don't configure any WCF endpoint.
Reader Level:


For a normal developer such as I, a great challenge was working with WCF 3.x configuration of End Points in config file. A developer had to add endpoints to setup a WCF service. In WCF 4, Defualt End Point is associated with the service, if we don't configure any WCF endpoint.

To see how Default EndPoint works follow the steps below,

Step 1 : Create a WCF service application

Let us create two service contracts

IService1.cs

[ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData();

    }


IService2.cs

[ServiceContract]
    public interface IService2
    {
        [OperationContract]
        string GetMessage();
    }


Now let us implement the service as below,

public class Service1 : IService1, IService2
    {
        public string GetData()
        {
            return "Hello From Iservice1 ";

        }

        public string GetMessage()
        {
            return " Hello From Iservice2";
        }


Step 2

Now we will host this service in a console application; create a new console application project.

Add reference of WCF service application project and also add reference of System.serviceModel in the console application project.

Note: There is no App.Config associated with console application.

image1.gif

Here, we are registering two base addresses with the servicehost. One for http binding and another for nettcp binding.

Now we don't have any configuration for the service EndPoint. ServiceHost will create default EndPoints.

Now ServiceHost will configure EndPoints for two base addresses

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"),
                new Uri("net.tcp://localhost:8081/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);

        }
    }
}


Output would be

image2.gif

The default protocol mapping is as below,

image3.gif

Since HTTP is mapped with basicHttpBinding, so we got the default EndPoint with basicHttpBinding.

Default EndPoint will only get created if there is not a single Endpoint configured. If we add any single EndPoint then all then there won't be any default EndPoint get configured.

If we add one EndPoint as below,

image4.gif

We added one service EndPoint. Now to the output we get add the following code:

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"),
                new Uri("net.tcp://localhost:8081/service"));

            host.AddServiceEndpoint(typeof(IService1),
                new WSHttpBinding(),
                "myBinding");
 
            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);
 
 
        }
    }
}


Output

image5.gif

Now we see that if we configure a EndPoint then WCF does not support default Endpoints.
  

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
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF 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.
Nevron Gauge for SharePoint
Become a Sponsor