Reputation: 1695
I'm trying to add a feature to an existing code in C# and call an api, say MyAPI(arg1,arg2). I have an interface,Lets say IProvider, which has a set of client side API definitions. I have two client Proxy classes- Provider and ProviderNew that has some wrapper implementations for the API's - within each of those wrapper functions (for API's) we call eventually call the service API hosted at a remote server. Only Under the ProviderNew(Not Provider) Project class - under Service references - I see that MyAPI being defined ( or auto-generated, as I'm not sure how these files are created) in WSDL,xsd files. Adding to these, I see some references.cs file for the ProviderNew alone that has some definitions for MyAPI. These things clearly suggest that only ProviderNew implements the client side Proxy code for MyAPI.
My Problem now is that since IProvider interfaces MyAPI signature , the system expects both the implementing classes (Provider and ProviderNew) to implement client code for MyAPI, which is not the case as only ProviderNew has definitions added for MyAPI. When I try to implement MyAPI client code in Provider.cs alone, I see an error saying -
Provider.ProviderServiceReference.ProviderServiceClient does not contain a definition for 'MyAPI' and no extension method 'MyAPI' accepting a first argument of type 'Provider.ProviderServiceReference.ProviderServiceClient' could be found (are you missing a using directive or an assembly reference?) MyPath..\Provider.cs
But, when I leave without implementing them in Provider.cs, I see an error (This is a classic case that every class(if there are multiple implementations) that implements an interface, should implement all its functions individually by itself and not an subset of functions.)
'Provider' does not implement interface member 'IProvider.MyAPI(int , int)' MyPath ...Provider.cs
I have tried my best to make this clear as possible, let me know for more clarity.
How do I proceed ? Any suggestions ?
Upvotes: 2
Views: 500
Reputation: 28530
An implementing class cannot pick and choose which portions of an interface to implement - it's all or nothing. From a WCF perspective, if you want to add operations (methods) for one client but not another, you are looking at two services, not one.
For example, say you have the following contract:
[ServiceContract]
public interface IProvider
{
[OperationContract]
int MyApiCall_1(string param);
[OperationContract]
string MyApiCall_2(int number);
}
Which is implemented (initially) by Provider. All clients are using this service just fine.
Now let's say that one of your clients needs access to MyApiCall_3, but the rest don't. As you've discovered, this becomes a breaking change for all the clients that don't need MyApiCall_3.
You could simply update the service - but then all of the clients would have access to MyApiCall_3, and perhaps you don't want that for one reason or another.
Another option would be to create another contract (interface) that derives from the original one, like this:
[ServiceContract]
public interface INewProvider : IProvider
{
[OperationContract]
double MyApiCall_3();
}
You would then implement the contract in a new service (for simplicity, let's call it NewProvider
). The client that needs the additional API call would use NewProvider
, and the other clients would use the older Provider
.
In short, if you are adding functionality to a contract, you can either:
1. Make that additional functionality available in the original contract and thereby all clients will have access to it, or 2. Create a new contract that inherits from the original contract and expose it via a new service, and then only the clients that need the additional functionality would use the new service.
Upvotes: 1