2

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.

1 Answer 1

2

As per c# specs here T : CoreServiceClient,SessionAwareCoreServiceClient is incorrect, You can have only one class at start or/and followed by multiple interfaces Multiple classes are not allowed here. so you can change the code accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.