Browse DevX
Sign up for e-mail newsletters from DevX


WCF Essentials—A Developer's Primer : Page 4

WCF is an SDK for building service-oriented applications on Windows, letting you use classic CLR programming constructs, such as classes and interfaces, to deploy and consume services. The programming model is declarative and largely attribute-driven.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Every service is associated with an address that defines where the service is, a binding that defines how to communicate with the service, and a contract that defines what the service does. This triumvirate governing the service is easy to remember as the ABC of the service.

In fact, WCF formalizes this relationship in the form on an endpoint. The endpoint is the fusion of the address, contract, and binding (see Figure 5). Every endpoint must have all three, and the service exposes the endpoint. Logically, the endpoint is the service's interface, and is analogous to a CLR or COM interface.

Figure 5. An Endpoint: The endpoint is the fusion of the address, contract, and binding
Every service must expose at least one business endpoint and each endpoint has exactly one contract. All endpoints on a service have unique addresses, and a single service can expose multiple endpoints. These endpoints can use the same or different bindings and can expose the same or different contracts. It is important to point out that nothing in the service code pertains to its endpoints and they are always external to the service code. You can configure endpoints either administratively using a config file or programmatically.

Administrative Endpoint Configuration
Consider the following service definition:

namespace MyNamespace { [ServiceContract] interface IMyContract {...} Class MyService : IMyContract {...} }

Listing 2 shows the required entries in the host process config file. Administrative configuration is the option of choice in the majority of cases because it provides the flexibility to change the service address, binding, and even exposed contracts without rebuilding and redeploying the service.

Listing 3 shows a config file defining a single service that exposes multiple endpoints. Note that the endpoint must provide a base address that is consistent with the binding, such as HTTP with WSHttpBinding. A mismatch causes an exception at the service load time. You can configure multiple endpoints with the same base address as long as the URI is different:

<service name="MyNamespace.MyService"> <endpoint Address = "net.tcp://localhost:8001/Service1/" ... /> <endpoint address="net.tcp://localhost:8001/Service2/" ... /> </service>

You can also omit the address, in which case, the service uses the base address registered with the host (and the host must provide a matching base address):

<endpoint binding="wsHttpBinding" contract="MyNamespace.IMyContract" />

You can provide only a URI, in which case, the address is that relative address under the base address (and the host must provide a matching base address):

<endpoint address="SubAddress" ... />

When providing a base address, the endpoint overrides any base address provided by the host:

<endpoint address="http://localhost:8000/MyService/" ... />

Note that when hosting with IIS, the service must use the IIS base address (computer name + virtual directory over HTTP).

Programmatic Endpoint Configuration
Programmatic endpoint configuration is completely equivalent to administrative configuration yet it does not resort to a config file and instead, you make programmatic calls to add endpoints to the ServiceHost instance. Again, these calls are always outside the scope of the service code. ServiceHost provides overloaded versions of the AddServiceEndpoint() method:

public class ServiceHost : ServiceHostBase { public ServiceEndpoint AddServiceEndpoint( Type implementedContract, Binding binding, String address); //Additional members }

Listing 4 demonstrates programmatic configuration of the same endpoints as in Listing 3. To rely on the host base address, provide just the URI as the address:

Uri tcpBaseAddress = new Uri("http://localhost:8000/"); ServiceHost serviceHost = new ServiceHost(typeof(MyService), tcpBaseAddress); Binding tcpBinding = new NetTcpBinding(); //Use base address as address serviceHost.AddServiceEndpoint(typeof( IMyContract) , tcpBinding,""); //Add relative address serviceHost.AddServiceEndpoint( typeof(IMyContract), tcpBinding,"MyService"); //Ignore base address serviceHost.AddServiceEndpoint( typeof(IMyContract), tcpBinding, "net.tcp://localhost:8001/MyService"); serviceHost.Open();

Thanks for your registration, follow us on our social networks to keep up-to-date