Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

i am new here and thats my first entry in this forum, i hope somebody can help me with this hopefully easy question? I am trying to implement a base WCF Service from which my sub services should inherit.

My Base Service looks like this:

C#
Class: BaseService.cs
  [ServiceContract]
  public class BaseService
  {
      [OperationContract]
      public DateTime GetDate()
      {
          return DateTime.Now;
      }
  }

Here is my sub Service whicht should inherit all Methods from BaseService.
C#
[ServiceContract]
public class TestService:BaseService
{
    [OperationContract]
    public string SayHello()
    {
        return "Hello";
    }
}


When i try to host my TestService with ServiceHost like this:
C#
ServiceHost _host;
_host = new ServiceHost(typeof(TestService.TestService));
_host.Open();


i get an exception that says something like: the service inheritance can only
be between interface types.
So here is my question: Is there a possiblity to inherit from a Base Service Class
to provide base functionality like getdate() or something like this, or is this absolutly impossible.

Best Regards
Manuel
Posted
Updated 19-Dec-11 6:10am
v2

Similar question has been discussed in MSDN forum. Have a look at it.WCF Service Inheritance[^]
 
Share this answer
 
Comments
thatraja 19-Dec-11 12:55pm    
This should help OP, 5!
m.bleimuth 20-Dec-11 9:57am    
I have already read this article, but from my point of view this cant be the only solution, hopefully,... For example, I want to write 10 different services, each service handles a different topic, but I want in each service two Methods, Register() und Unregister(). If i do it like described in the microsoft article i always need to implement the Register() and Unregister() Method in each service, thats not really beautifull in my eyes. I want to implement the register and unregister method only one time and then inherit it to the services.
You should implement multiple interfaces and put the [OperationContract] methods in them.

so your class might look like

MyWCFClass : IBaseService, IOtherServiceContract, IYetAnotherContract
{

}
 
Share this answer
 
Comments
m.bleimuth 20-Dec-11 8:54am    
Yes, but if i do it like this i need to implement the IBaseService Interface in every class. For example, i want to write some Services, each Service should have a Register() and Unregister() Method. If i do it like described above, i need to implement the register und unregister Method in each service, so i produce the same code for each service. That cant be the right solution for my problem,...
killabyte 21-Dec-11 19:48pm    
i that case make sure your base class has no contract attributes on it, i dont think they bubble up through correctly.

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