|
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace SvcHost
{
/// <summary>
/// Client class provides proxy for server connection.
/// </summary>
public class Client
{
/// <summary>
/// Method creates proxy by its endpoint URI resolving server metadata.
/// </summary>
public static IT ConnectTo<IT>(string endpointAddressUri) where IT : class
{
IT proxy = null;
string err = null;
ServiceEndpointCollection endpoints = null;
string realAddress = ExtractAddress(endpointAddressUri); //Igor
try
{
//Igor
endpoints = MetadataResolver.Resolve(typeof(IT),
new EndpointAddress(GetProperMexEndpointAddress(endpointAddressUri)));
}
catch (Exception e)
{
err = e.Message;
}
if (null == err)
foreach (ServiceEndpoint endpoint in endpoints)
{
try
{
proxy = new ChannelFactory<IT>(endpoint.Binding,
new EndpointAddress(endpoint.Address.Uri.AbsoluteUri.Replace("localhost", realAddress)))
.CreateChannel(); //Igor
break;
}
catch
{
}
}
return proxy;
}
//Igor
private static string GetProperMexEndpointAddress(string uri)
{
return string.Format("{0}{1}Mex", uri, uri[uri.Length - 1] == '/' ? string.Empty : "/");
}
//Igor
private static string ExtractAddress(string uri)
{
return uri.Split(new char[] { ':' })[1].Replace("//", string.Empty);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.