Article
One Community, One World
Windows Commnication Foundation Addressing
by Ahmed Rafie  on Aug 6 2007 12:00AM
  PageViews 3836   |    Add to Fav   |     Send to Friend   |    no downloads available

Introduction
Windows communication foundation is Microsoft's new platform for developing distributed application using .Net framework. WCF simplifies development of connected applications through a new service-oriented programming model. Before we start explaining windows communication foundation addressing I'd like to start with service-oriented architecture.

Service-oriented Architecture
Service-oriented Architecture (SOA) is an idea that many developers and architects are thinking about today. Many times confusion is created because definitions are not stated up front. So what is a Service? A service is a business logic (the server) that is capable of being accessed from another process (the client). You can think of a service as a component that has an address and its own implementation that executes to perform a function. In the development of distributed applications you may have many services connected to each other to perform the required operations. And have many clients that consume services. This concept is called Service Orientation.

Service-Oriented Architecture as defined in W3C www.W3C.org as "A set of components which can be invoked and whose interface description can be discovered and published".

Service-Oriented applications are programs that know nothing about each other, thus are loosely coupled. Each application communicates with other applications via messages. And what makes this approach ideal is that these messages can be sent from one application to another regardless of the platform of each service.

So we are talking about services and components that communicate with each other and that know nothing about each other, how can they communicate without channel?, so we need a channel that will carry the messages between the service and the client. But before creating a channel the client need to know the address of the service.

Without address there is no channel, and without channel there is no connection, and if there is no connection, no messages transformation. So we can say that address is the first letter in this story.

Endpoint Addresses
WCF service exposes one or more endpoints where messages can be sent. You can think of a service as collection of endpoints. Each endpoint consists of an Address, a Binding, and a Contract. The address is the most important component of the endpoint, because without the address you wouldn't be able to find the endpoint to begin with. It specifies there the service's endpoint is.

Address parts
Endpoint address consists of four parts:

  • The transport protocol.
  • The name if the machine running the service.
  • Port number (optional) .
  • The path of the service Such as http://MyMachine:8080/MyService

This is an example of endpoint address, the endpoint uses HTTP as transport protocol and the service hosted on a computer named "My Machine" and using port 8080 for communication.

Address Types
WCF lets you use several types of addresses to associate with endpoint

Endpoint Address
This address is specific for service endpoint; the client can access the service via the endpoint address. Such as the following: http://MyMachine:8080/MyService You can define the endpoint address in one of the following ways Via a configuration file 

XML
<configuration>       
    <system.serviceModel>            
    <services>                  
        <service name="MyService">                        
            <endpoint address=http://localhost:8080/MyService contract="IMyService" binding="wsHttpBinding"/>                  
        </service>             
    </services>       
    </system.serviceModel> 
</configuration>



Via Code 

C#
ServiceHost host = new ServiceHost(typeof(MyService));
host.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "http://localhost:8080/MyService"); 



The configuration file method is preferable because it provides much more flexibility after the application is deployed. The configuration in the preceding example defines a single endpoint; the address is specified via an attribute on the endpoint element.

Base address
Base address provide a way to specify only one single primary address for a service and then assign relative addresses to each individual endpoint. Suppose you have a service with two endpoints, you assign only Base address for the service: http://MyMachine:8080/Myservice

And then assign the endpoints relative address. http://MyMachine:8080/MyService/Service1 http://MyMachine:8080/MyService/Service2 You can specify Base address via the configuration or via code. I will show you how to specify it in the configuration file. You do this by listing the base address within the element for each like:
 
XML
<configuration>     
    <system.serviceModel>             
        <services>                   
            <service name="MyService">                         
                <host>                               
                    <baseAddresses>                                     
                        <add baseAddress="http://localhost:8080/Ser"/>                               
                    </baseAddresses>                         
                </host>
             
                <endpoint  address="Test"                                  
                                   contract="IMyService"                                  
                                   binding="wsHttpBinding"/>                    
            </service>           
        </services>         
    </system.serviceModel> 
</configuration> 



In the last example I added only one base address which is http://localhost:8080/Ser and only one relative address for the endpoint named test. But you can use more than one relative addresses inside the service. The address of the endpoint will be resolved against the base HTTP address and becomes http://localhost:8080/Ser/Test.

MEX Address This address allows the client to gather information about the service. This information is provided through the service metadata, which describe the service. MEX address is HTTP endpoint address and written like this: http://MyMachine:8080/Myservice/MexAddress

Formats
When I discussed the address parts, I said that the endpoint address consists of four parts; first one is the transport protocol. Addresses formatted based on the selected transport protocol. So when you develop a service keep in mind the environment in which the service will be hosted. This environment may require the addresses to be formatted a certain way. WCF address has six formats I will discuss them right now.

HTTP Address
One of the most common address formats for a service is that of an HTTP address. It contains four parts just like the following example: http://www.hut.com:8080/MyService

HTTPS Address
When HTTP address secured by using SSL then specified as HTTPS. But it required obtaining a certificate from a valid certificate authority. HTTPS://www.hut.cin:8080/MyService

TCP Address
The format of a TCP address as the following Net.tcp://www.hut.com:8080/MyService

MSMQ Address
It differs from the others a bit, the format as the following Net.msmq://hostname/[private]/queue-name For example: Net.msmq://nemo/msmqshare/BookOrder The new part here is the private part, and the queue-name. The private part is optional part but when used, it contains the address of a target queue that is a private queue. And not specified when accessing a public queue. The queue-name used to specify the name of the msmq name. Private queue is a queue on local machine. While the public queue is a queue found on the network as one accessed via active directory.

Named pipe
AddressIt is used when the client the service on the same computer, and can't be used cross-machines. It is formatted as follows Net.pipe://localhost/MyService

IIS Address
In this address format you must specify a virtual directory name as well as a service .SVC file name. HTTP://domainname/machinename[:port]/virtualDirectory[.svc filename]

Multiple endpoints share the same contract
Sure the WCF service can contain multiple Endpoints, and you can expose the same contract using different Bindings. But each endpoint address must be unique. Consider the following example:  

XML
<configuration>       
    <system.serviceModel>            
        <services>                  
            <service name="TaxService">                      
                    <endpoint address="http://localhost:8080/Tax" 
                                        binding="basicHttpBinding" 
                                        contract="ITaxService" />
                      
                    <endpoint address="net.tcp://localhost:8081/Tax" 
                                        binding="netTcpBinding" 
                                        contract="ITaxService" />                  
            </service>            
        </services>        
    </system.serviceModel> 
</configuration> 



In the pervious example we have two endpoints, and each of them use the ITaxService contract. But each one use a different Binding, so each address must be unique because you are using different bindings.

Multiple Endpoints share the same address
Also you can use a fixed address with multiple endpoints and share the same binding. In this case, Endpoints share the same transport listener and channel stack. But each endpoint must expose different contract. Consider the following example. 

XML
<configuration>      
    <system.serviceModel>           
        <services>                
            <service name="TaxService">                   
                    <endpoint address="http://localhost:8080/Tax" 
                                        binding="WsHttpBinding" 
                                        contract="ITaxFirstService" />
                   
                    <endpoint address="http://localhost:8080/Tax" 
                                        binding="WsHttpBinding" 
                                        contract="ITaxSecondService" />                 
            </service>            
        </services>       
    </system.serviceModel> 
</configuration> 



In the pervious example we have two endpoints, each of them uses the same Binding and the same address but each one expose different contract. WCF will create and use single Binding instance for both endpoints to share. Since both of them specified WsHttpBinding for the Binding. If you are using self-hosting environment and you will create ServiceHost manually in code you will need to ensure that you use the same Binding instance when calling AddServiceEndpoint method.

As the following example: 

C#
ServiceHost host = new ServiceHost(typeof(TaxService));
WSHttpBinding bind = new WSHttpBinding();
host.AddServiceEndpoint(typeof(ITaxFirstContract),bind,"http://localhost:8080/Tax");
host.AddServiceEndpoint(typeof(ITaxSecondContract), bind,"http://localhost:8080/Tax"); 
host.Open();



Conclusion
This article covered the windows communication foundation addressing and gives you a brief description about Service-Oriented Architecture which is important topic to read about it. We also covered many types of addresses and its formats.

Comments