private async Task<Session> Connect(Guid clsid)
{
// load the endpoint information.
ConfiguredEndpoint endpoint = m_endpoint = LoadConfiguredEndpoint(clsid);
if (endpoint == null)
{
throw new ServiceResultException(StatusCodes.BadConfigurationError);
}
// update security information.
if (endpoint.UpdateBeforeConnect)
{
endpoint.UpdateFromServer();
// check if halted while waiting for a response.
if (!m_running)
{
throw new ServiceResultException(StatusCodes.BadServerHalted);
}
}
// look up the client certificate.
X509Certificate2 clientCertificate = await m_configuration.SecurityConfiguration.ApplicationCertificate.Find(true);
// create a message context to use with the channel.
ServiceMessageContext messageContext = m_configuration.CreateMessageContext();
// create the channel.
ITransportChannel channel = SessionChannel.Create(
m_configuration,
endpoint.Description,
endpoint.Configuration,
clientCertificate,
messageContext);
// create the session.
Session session = new Session(channel, m_configuration, endpoint, clientCertificate);
// create a session name that is useful for debugging.
string sessionName = Utils.Format("COM Client (Host={0}, CLSID={1})", System.Net.Dns.GetHostName(), clsid);
// open the session.
session.Open(sessionName, null);
// return the new session.
return session;
}