Thursday, May 31, 2007

Endpoint Catalog Application Block

add a <section> node to the <configSections>

<configSections>
<section name="Endpoints" type="Microsoft.Practices.SmartClient.EndpointCatalog.Configuration.EndpointSection, Microsoft.Practices.SmartClient.EndpointCatalog" />
</configSections>



Create the application block configuration section

<Endpoints>
<EndpointItems>
<add Name="WebService1" Address="http://default-host.com/WebService1.asmx" UserName="default-user-name" Password="default-password" Domain="default-domain">
</add>
</EndpointItems>
</Endpoints>


create a new EndpointCatalog and add it to a WorkItem
using Microsoft.Practices.SmartClient.EndpointCatalog;
using System.Net;


// Specify the section name in the configuration file.

IEndpointCatalogFactory catalogFactory = new EndpointCatalogFactory("Endpoints");

IEndpointCatalog catalog = catalogFactory.CreateCatalog();

myWorkItem.Services.Add<IEndpointCatalog>(catalog);
Getting an Endpoint Catalog Instance from a WorkItem


Method 1:
public class MyNewClass
{
private EndpointCatalog catalog;

public MyNewClass([ServiceDependency] EndpointCatalog catalog)
{
this.catalog = catalog;
}
...
}
Method 2:
public class MyNewClass
{
private EndpointCatalog catalog;

[ServiceDependency]
public EndpointCatalog EndpointCatalog
{
get { return this.catalog; }
set { this.catalog = value; }
}
...
}
Method 3:
public class MyNewClass
{
private EndpointCatalog catalog;

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

Listing and Getting Information about Endpoints

To check whether an endpoint exists
String epName = "MyWebServiceEndpoint";
if (catalog.EndpointExists(epName))
{
// Endpoint named MyWebServiceEndpoint does exist in the catalog.
}

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: