I am trying to to write a generic class CoreServiceSession to get the different type of client like CoreService or SessionAware. Below is my code:
public class CoreServiceSession<T> : IDisposable
where T : CoreServiceClient,SessionAwareCoreServiceClient
{
private T _client;
public T CoreServiceClient
{
get { return _client; }
}
public CoreServiceSession(string endPoint, NetworkCredential credentials, ClientType clientType)
{
if (_client == null)
{
InitializeClient(endPoint, credentials, clientType);
}
}
private void InitializeClient(string endPoint, NetworkCredential credentials, ClientType clientType)
{
try
{
switch (clientType)
{
case ClientType.CoreServiceClient:
_client = (T)new CoreServiceClient(endPoint);
break;
case ClientType.SessionAwareCoreServiceClient:
_client = (T)new SessionAwareCoreServiceClient(endPoint);
break;
default:
break;
}
_client.ChannelFactory.Credentials.Windows.ClientCredential = credentials;
}
catch (EndpointNotFoundException e) { }
catch (Exception e) { }
}
}
However I am getting Compile Time error saying "Cannnot convert to SessionAwareCoreServiceClient to T. Can I get any help here? And also If I reverse the position of client Type in constraint "where T : CoreServiceClient,SessionAwareCoreServiceClient " then it start giving the same error for CoreServiceClient.