Default End Points in WCF 4.0

No.of Views1291
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  04 Dec 2010 09:12:28
Tag : WCF , General
Developer had to add endpoints to setup a WCF service. In WCF 4, Default End Point is associated with the service, if we don’t configure any WCF endpoint.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

For a normal developer like me, had a great challenge working with WCF 3.x was configuration of End Points in config file. Developer had to add endpoints to setup a WCF service.  In WCF 4, Default 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 ,
Step1

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.

Image Loading

Here, we are registering two base addresses with the servicehost.   One for http binding and other 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

Image Loading

The default protocol mapping is as below,

Image Loading

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,
 Image Loading

We added one service EndPoint and now the output we would get for below code ad below,

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 

Image Loading

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

Conclusion

In this article, you have learned EndPoint in WCF.thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</