Angad
Angad

Reputation: 1174

Extending WCF services

I have multiple WCF services in my project and would like to add a new method to all of them without changing any services. Is it possible to do that in c#?

Let say the services are

  1. MyService1.svc
  2. MyService2.svc
  3. Myservice3.svc

Now I want to expose a method called "HealthyStatus()" to all of them so that clients can consume this method. This method is implemented in another class called "HeathCheckerService.cs"

I DON'T want to change the web services i.e. no base class technique.

Thanks in advance.

Upvotes: 0

Views: 1200

Answers (1)

tom redfern
tom redfern

Reputation: 31750

As commenters have stated, this is not possible. You cannot extend a service contract unless you add operations to it, which will mean recompiling and redeploying your services.

One way to acheive what you want is to host a new service endpoint with an operation called HealthyStatus(), which then calls the other services (in some non-intrusive manner) and returns a status. But this would require your other services to expose an operation which could be called without consequence, and your definition of what a Healthy status actually means.

Some load balancers can be configured to provide health monitoring of http enpoints, but again, the definition of healthy here is fluid and may not be specific enough for your needs.

If you're happy to update your service, probably the least intrusive thing to do would be to create a new service contract like:

[ServiceContract] 
public interface IHealthCheckServiceContract
{    
    [OperationContract]    
    int CheckStatus(); 
}

and then have all your other service contracts implement it:

[ServiceContract] 
public interface IMyService1 : IHealthCheckServiceContract
{ 
    ...    
}

[ServiceContract] 
public interface IMyService2 : IHealthCheckServiceContract
{ 
    ...    
}

Obviously with this approach you could then subclass your service implmentations

public abstract class HealthChecker
{
    public abstract int CheckStatus();
}

public class MyService1 : HealthChecker, IMyService1
{
    public override int CheckStatus()
    {
        // MyService1 implementation of CheckStatus()
    }

    // Implementation of IMyService1 operations
    .....
}

public class MyService2 : HealthChecker, IMyService2
{
    public override int CheckStatus()
    {
        // MyService2 implementation of CheckStatus()
    }

    // Implementation of IMyService2 operations
    .....
}

Upvotes: 1

Related Questions