Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi guys,

I have a WCF Service and I would like to implement some base class logic such as authentication and other basic checks which applies to all my services. The default implementation creates an interface to implement. I can't obviously then add concrete base class functions to this.

So the question is.. is there any downside to not using an interface when implementing a WCF Service?

thanks
Posted
Comments
Sergey Alexandrovich Kryukov 30-Jun-14 18:44pm    
Why without interface? Why do you think avoiding interface would limit your extensibility?
—SA

1 solution

Hi Ashman786,

In General it is a bad idea to implement Services without Interfaces, cause the Interface represents the Service contract. Anyway I don't see any reason why you can't do "both".

Look at this example code "structure".

You can work with inheritance - also with Interfaces.

So you have:

a Interface for common Service operations like this:

C#
[ServiceContract]
public interface ICommonServiceMethods
{
    [OperationContract]
    string WriteLog(string strMessage);
}


a specific Service Interface inherited from the common operations Interface like this:

C#
[ServiceContract]
public interface IService1 : ICommonServiceMethods
{
    [OperationContract]
    string GetData(int value);
}


You can implement your common Service operations in a base class (could be abstract)

C#
public class BaseService : ICommonServiceMethods
 {
     public string WriteLog(string strMessage)
     {
         return strMessage; //dummy
     }
 }


And then just derive your specific Service instance from the base class while implementing the specific Service.

C#
public class Service1 : BaseService, IService1
{
    public string GetData(int value) { return value.ToString(); /* dummy */}
}



So no need to think about omitting "Interfaces" - although technically you could...

I hope this helps?

Kind regards Johannes
 
Share this answer
 
Comments
Ashman786 30-Jun-14 13:10pm    
Thanks Johannes
that is exactly what I want to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900