Search code examples
c#.netwcfapplication-design

Is it ok to call a service method in an other service method ? (WCF)


Sample code:

public class Service1
{
    public int Service1()
    {
        ....
    }
}

public class Service2
{
    public int Service2()
    {
        ...
        var service1 = new Service1();
        var count = service1.Service1();
        ...
    }
}

Both classes and methods are exposed thru WCF.


Solution

  • This should work fine, as inside Service2 you're calling Service1 in-process rather than going out over WCF (even if you were using WCF inside Service2 it should still work).

    But in terms of design, this isn't very good. Service operations should be called via a service (e.g. over HTTP or TCP). If Service1 and Service2 both need access to common functionality, the solution is to refactor the common code out into a shared class or something, rather than just having it in Service1.