Reputation: 464
I have two services TestService1 and TestService2. Can I inherit both these services from a plain class without ServiceBehaviorAttribute attribute? Like this:
public class ServiceBase
{
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class TestService1 : ServiceBase, ITestService1
{
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class TestService2 : ServiceBase, ITestService2
{
}
Will the behaviuor from ServiceBehaviorAttribute attribute be applied to inherited members of inherited plain class?
Have I to add some service specific attributtes to base plain class?
If I wrong in realization, how to properly realize such an inheritance - service inherits some behaviour from another plain class?
UPDATE. It is not about ServiceBehaviorAttribute attribute inheritance. I ask more common question - can I inherit some functionality from another class (that is in outside DLL for example, that doesn't even associated with WCF) in WCF service class? And how? Is my code valid and have no concealed nuances?
Upvotes: 1
Views: 1183
Reputation:
[ServiceBehavior]
can be applied to base classes such that derived service classes will inherit the behavior.
Change this:
public class ServiceBase
{
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class TestService1 : ServiceBase, ITestService1
{
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class TestService2 : ServiceBase, ITestService2
{
}
...to this:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class ServiceBase
{
}
public class TestService1 : ServiceBase, ITestService1
{
}
public class TestService2 : ServiceBase, ITestService2
{
}
can I inherit some functionality from another class (that is in outside DLL for example, that doesn't even associated with WCF) in WCF service class?
Sure, ServiceBase
could have methods that TestService1
and TestService2
may utilise. However, WCF clients will only see the methods defined on your interfaces ITestService1
and ITestService2
even if ServiceBase.SomeMethod()
were public.
e.g.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class ServiceBase
{
// TestService1 and TestService2 may call me
// but not WCF clients. I'm invisible
public void SomeMethod () {}
}
public class TestService1 : ServiceBase, ITestService1
{
public void SomeServiceMethod1() { SomeMethod(); }
}
public class TestService2 : ServiceBase, ITestService2
{
public void SomeServiceMethod2() { SomeMethod(); }
}
Regarding your update where the base class is nothing to do with WCF, "yes you can". It's not different to calling any other base class.
Upvotes: 1