Do not inherit interfaces from other interfaces.
Interfaces are contracts. Contracts must be explicit, otherwise the flexibility and maintainability of your design would run into issues. An interface is a pure contract definition and should remain explicit and pure.
Consider the following example:
[ServiceContract]
public interface IMyService1
{
[OperationContract]
void Test1();
}
[ServiceContract]
public interface IMyService2 : IMyService1
{
[OperationContract]
void Test2();
}
[ServiceBehavior(…)]
class MyService : IMyService2, IMyService1
{
#region IMyService2 Members
public void Test2(){
}
#endregion
#region IMyService1 Members
public void Test1()
{
}
#endregion
}
What happens when you Publish your Service and open its communication channels. If you do the following:
ServiceHost serviceHost = new ServiceHost(typeof(MyService, …)ServiceEndpoint ep = serviceHost.AddServiceEndpoint(typeof(IMyService1), typeof(IMyService2));
You will get a nasty ambiguous message from WCF since you are trying to register the same contract twice; once implicitly with IMyService2 wince it inherits from IService1 and another time explicitly using IService1
In this case, you need to either do #1 (Still the wrong way) or #2(the right way)
#1: remove the IMyService1 from both the Service inheritance and implicitly implement IMyService1’s operation contracts and remove the IMyService1 from your service endpoints registration with WCF.
[ServiceBehavior(…)]
class MyService : IMyService2
{
#region IMyService2 Members
public void Test2()
{
}
#endregion
#region IMyService1 Members
void IMyService1.Test1()
{
}
#endregion
}
ServiceHost serviceHost = new ServiceHost(typeof(MyService, …)
ServiceEndpoint ep = serviceHost.AddServiceEndpoint(typeof(IMyService2));
In this case it is very ambiguous to anyone reading the code to know at a glance that your service publishes an IMyService1 as a service contract
#2 (the right way)
Just remove the inheritance of IMyService1 from iMyService2’s interface. That’s it.[ServiceContract]
public interface IMyService2
{
[OperationContract]
void Test2();
}
Interfaces are contracts. Contracts must be explicit, otherwise the flexibility and maintainability of your design would run into issues. An interface is a pure contract definition and should remain explicit and pure.
Consider the following example:
[ServiceContract]
public interface IMyService1
{
[OperationContract]
void Test1();
}
[ServiceContract]
public interface IMyService2 : IMyService1
{
[OperationContract]
void Test2();
}
[ServiceBehavior(…)]
class MyService : IMyService2, IMyService1
{
#region IMyService2 Members
public void Test2(){
}
#endregion
#region IMyService1 Members
public void Test1()
{
}
#endregion
}
What happens when you Publish your Service and open its communication channels. If you do the following:
ServiceHost serviceHost = new ServiceHost(typeof(MyService, …)ServiceEndpoint ep = serviceHost.AddServiceEndpoint(typeof(IMyService1), typeof(IMyService2));
You will get a nasty ambiguous message from WCF since you are trying to register the same contract twice; once implicitly with IMyService2 wince it inherits from IService1 and another time explicitly using IService1
In this case, you need to either do #1 (Still the wrong way) or #2(the right way)
#1: remove the IMyService1 from both the Service inheritance and implicitly implement IMyService1’s operation contracts and remove the IMyService1 from your service endpoints registration with WCF.
[ServiceBehavior(…)]
class MyService : IMyService2
{
#region IMyService2 Members
public void Test2()
{
}
#endregion
#region IMyService1 Members
void IMyService1.Test1()
{
}
#endregion
}
ServiceHost serviceHost = new ServiceHost(typeof(MyService, …)
ServiceEndpoint ep = serviceHost.AddServiceEndpoint(typeof(IMyService2));
In this case it is very ambiguous to anyone reading the code to know at a glance that your service publishes an IMyService1 as a service contract
#2 (the right way)
Just remove the inheritance of IMyService1 from iMyService2’s interface. That’s it.[ServiceContract]
public interface IMyService2
{
[OperationContract]
void Test2();
}
Comments
Post a Comment