user2463514
user2463514

Reputation: 273

Single WCF Service for all functionality

I have created wcf service and I want to create single service for all the functionalities.

so I have used partial class and try to achieve it like this.

FileName:-IMainService.cs

[ServiceContract]
public interface IMainService
{       
    [OperationContract]
    void DoWork();
}

FileName:-MainService.svc

 public partial class MainService : IMainService
{
    public void DoWork()
    {
        throw new NotImplementedException();
    }
}

FileName:-IProductService.cs

[ServiceContract]
public interface IProductService
{       
    [OperationContract]
    void GetProducts();
}

FileName:-ProductService.svc

 public partial class MainService : IProductService
    {
        public void GetProducts()
        {
            throw new NotImplementedException();
        }
    }

When I referenced the service in the application I can see two clients, MainServiceClient & ProductServiceClient. These names I guess based on file names and both contains the methods belong to there files for example: MainServiceClient has Dowork & ProductServiceClient has GetProducts.

I beleive I am in wrong track, Can somebody suggest me how do I achieve single service concept.

EDIT:

Upvotes: 2

Views: 137

Answers (2)

Serve Laurijssen
Serve Laurijssen

Reputation: 9763

why not something like this:

public class MainService : IProductService, IMainService
{
  MainService mMain = new MainService();
  ProductService mProduct = new ProductService();

  public void ImplementedMainServiceFunction()
  {
    mMain.DoSomething();
  }

  public void ImplementedProductServiceFunction(string s)
  {
    if (!string.IsNullOrEmpty(s))
      mProduct.DoSomething();
  }
}

Like this there's one implementing class which takes care of parameter checking and the actual logic can assume that the parameters are checked already giving you clearer/shorter code. And the logic is divided over multiple files too.

Upvotes: 0

Martin Moser
Martin Moser

Reputation: 6267

I would just create a new service interface which includes both other service interfaces. And the service class then implements the joined interface.

something like this:

public interface IJoinedService : IProductService, IMainService
{
}

public class JoinedService : IJoinedService
{
   private ProductService _productService;
   public void GetProducts()
   {
     _productService.GetProducts();
   }
}

Upvotes: 4

Related Questions