开发者

Calling WCF Services without using WCF - good or bad practice?

开发者 https://www.devze.com 2023-02-28 13:19 出处:网络
I\'m developing a service oriented architecture for an application and I would like the services to be exposed both over WCF as well as usable through a simple library.Ideally I would like to reduce d

I'm developing a service oriented architecture for an application and I would like the services to be exposed both over WCF as well as usable through a simple library. Ideally I would like to reduce duplicated code.

Conceptually, this maps to:

Client => WCF Service => Service Library (actual implementation)

or

Client => Service Library (actual implementation)

based on where the client is located (local or remote).

Here's a simple example:

[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    int Add(int a, int b);
}

public class Calculator : ICalculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

public class CalculatorFactory
{
    public static ICalculator CreateCalculator()
    {
        return new Calculator();
    }
}

And my client application did the following

int result = CalculatorFactory.CreateCalculator().Add(1,2);

or

int result = IChannelFactory<ICalculator>().CreateChannel().Add(1,2);

depending on if it were local or remote.

Is it a bad practice to call into WCF annotated code directly (i.e., without using WCF)?

Additional comments:

  • I realize that I could use WCF in all cases and just host the service using NamedPipes for local connections. I would like to avoid this if I can for simplicity sa开发者_开发百科ke.
  • The alternative to the above is to essentially duplicate the ICalculator interface in the service library and change the WCF service implementation to contain CalculatorFactory.CreateCalculator().Add(1,2). This seems like a lot of overhead given that I want the interface to be the same.


You can create and consume WCF annotated classes localy without any problems unless you start to use some WCF related features like OperationContext etc.

Generally this is useually abstracted in different way:

Client => ServiceAgent => Business Service

or

Client => ServiceAgent => WCF Service => Business service

The client itself doesn't know if the service is local all remote. The service agent is client side component which based on its implementation either creates local service instance or callse remote WCF service which in turn creates business service instance. ServiceAgent can be injected as dependency into client which will make your application pretty good configurable. Also you can expose different interface on the service agent (the same as business service implements) and if you want WCF service and proxy can use different one.

If you decite to use WCF service all the time including local calls don't use NamedPipes. NamedPipes are for inter process communication on the same machince. If you want to use communication in the same process use NullTransport or Local Channel instead. It still has worse performance then direct call.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号