Saturday, September 1, 2007

The Endpoint Catalog Application block

Is a simple service that uses a dictionary to store
  1. Endpoints - An endpoint consists of a name and a series of network names appropriate for that endpoint.
  2. Associated credential for each endpoint - A credential consists of two or three String values: a user name, a password, and optinally a logon domain name.
The following XML shows a typical configuration section in an App.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration?>
...
<Endpoints?>
<EndpointItems?>

<add Name="my-host-name" Address="http://default-host.com/path"
UserName="user-name" Password="password"?>
<NetworkItems?>
<add Name="Internet" Address="http://internet-host.com/path"
UserName="user-name" Password="password" /?>
<add Name="Work" Address="http://work-host.com/path"
UserName="user-name" Password="password" Domain="domain-name" /?>
</NetworkItems?>
</add?>

</EndpointItems?>
</Endpoints?>

</configuration?>


Configuring the Endpoint Catalog Application Block

Define a configuration section for the application block in the application configuration file.
<configSections >
<section name="Endpoints" type="Microsoft.Practices.SmartClient.EndpointCatalog.Configuration.EndpointSection, Microsoft.Practices.SmartClient.EndpointCatalog" />
</configSections>

Creating an Endpoint Catelog Class Instance

Create a new EndpointCatalog and add it to a WorkItem

1. Add a refrence to the Endpoint Catalog Application block to your project and import the namespace
using Microsoft.Practices.SmartClient.EndpointCatalog;
2. If you intend to work with individual credentials, you must also import
using System.Net;
3. Create a new instance of the EndpointCatalogFactory class.

Save the new insatnce of the factory class as an IEndpointCatalogFactory interface type in case you decide to extend the application block later by creating a custom class that implements this interface.
// Specify the section name in the configuration file.
String configName = "Endpoints";
IEndpointCatalogFactory catalogFactory = new EndpointCatalogFactory(configName);
4. Use the CreateCatalog method of the EndpointCatalogFactory, that contains details of all the configured endpoints for the application.
IEndpointCatalog catalog = catalogFactory.CreateCatalog();

5. Add it to the root WorkItem of your application.
myWorkItem.Services.Add(catalog);


Getting an Endpoint Catalog Instance from a WorkItem

There are 3 methods

1. use a [ServiceDependency] attribute on a parameter of your class constructor to accept an injected reference and store it in a class-level variable.

public class MyNewClass
{
private EndpointCatalog catalog;

public MyNewClass([ServiceDependency] EndpointCatalog catalog)
{
this.catalog = catalog;
}
...
}
2. Expose a public property for the EndpointCatalog and have ObjectBuilder set it to the EndpointCatalog instance in the WorkItem.
public class MyNewClass
{
private EndpointCatalog catalog;

[ServiceDependency]
public EndpointCatalog EndpointCatalog
{
get { return this.catalog; }
set { this.catalog = value; }
}
...
}

3. Query the Services collection of the WorkItem directly to obtain a reference to the service you want
public class MyNewClass
{
private EndpointCatalog catalog;

public MyNewClass()
{
this.catalog = myWorkItem.Services.Get<endpointcatalog>();
}
...
}

Listing and Getting Information about Endpoints

Use EndpointExists method to Check whether an endpoint exists in the catalog
Use EndpointCatalog class to get information about an endpoint

// Get the number of endpoints in the catalog.
int endpointCount = catalog.Count;
// Get the address for an endpoint if it exists.
String epName = "MyWebServiceEndpoint";
String epNetworkName = "MyHost";
if (catalog.AddressExistsForEndpoint(epName, epNetworkName))
{
String epAddress = catalog.GetAddressForEndpoint(epName, epNetworkName);
// Get the credentials for this endpoint.
NetworkCredential epCredentials = catalog.GetCredentialForEndpoint(epName, epNetworkName);
String epUsername = epCredentials.UserName;
String epPassword = epCredentials.Password;
String epDomain = epCredentials.Domain;
}

No comments: