Passing ServiceClientCredentials as a paramater to a constructor in C#

Viewed 255

I'm trying to implement a REST API client in C# where the constructor takes a parameter off Microsoft.Rest.ServiceClientCredentials. Since it's an abstract class I can't instantiate it and pass it on.

1 Answers

The abstract class is a template. You can't instantiate a template since there is missing functionlity in it, rendering it incomplete. You can instantiate derived class though.

Check the documentation here for derived credential classes and pick the one that's fitting to your case.

If you are looking for a username/password case, the BasicAuthenticationCredentials is the concrete class for you.

You can instantiate it

var bauthCredentials = new BasicAuthenticationCredentials();
functionThatTakesServiceClientCredentials(bauthCredentials);

And the function is something like this:

public void functionThatTakesServiceClientCredentials(ServiceClientCredentials bauthCredentials) {
    // Do stuff
}
Related