6

I am writing a dll that is referencing to some WCF service. The dll is functioning as a Gateway of the service and all calls are going through it. Probably there can be concurrent calls .

I have referenced the service but now I cannot decide how to write the wrapper functions correctly.

Is there some example or best practice for this functionality.

Night Walker
  • 20,638
  • 52
  • 151
  • 228
  • adding to the below discussion (best practices) i found the following (by the same asker) quite useful : http://stackoverflow.com/questions/10575724/moving-wcf-contracts-to-a-separate-dll – hello_earth May 15 '12 at 10:21

2 Answers2

5

I would make wrapper that matches the web service interface. It would also be a good idea to wrap up all of the objects exposed. Basically create a proxy. What I find really useful for this type of thing is to create an interface that matches the API and implement that. That way, you can create a dummy version of the DLL for testing without the overhead (or potential costs) associated with the WCF call. It would also make it much simpler if you need to replace the WCF call with an alternate provider in the future.

As an example, lets assume that we have an WCF service to an external provider for processing a payment. Something like this:

void ProcessPayment(float amount);

We could easily hook this into our code. The problem is that a simple change to the interface would result in us having to make changes everywhere the code is referenced. The same would be necessary if we changed providers to someone else, even if the interface was almost identical. Adding something like a simple interface:

interface IPaymentProvider
{
    void ProcessPayment(float amount);
}

Would completely decouple our code from the WCF service. We could easily build a class like this:

class PaymentProviderAWrapper : IPaymentProvider
{
    void ProcessPayment()
    {
        // Call the WCF service
    }
}

That we could load dynamically with a factory or dependency injection framework like Spring.NET. Changing to a provider B would be as simple as creating a new wrapper:

class PaymentProviderBWrapper : IPaymentProvider
{
    void ProcessPayment()
    {
        // Call provider B's Native DLL
    }
}

Switching your code from provider A to B would be as simple as changing a configuration setting.

Even if we compiled the library directly into our code, all we would need to do is change the construction logic to use the new library. The rest of our application would not change at all. Just a simple recompile.

Graymatter
  • 6,529
  • 2
  • 30
  • 50
  • The example is too easy. What about wrapping event handlers (with specific EventArgs) or async methods? Seems to have too much maintanence overhead. – J Pollack Oct 24 '18 at 09:56
2

In response to Graymatter's answer I don't see what the difference is between calling a service wrapper which exposes the same calls and then forwards the calls to the real service, and just calling the service, assuming a one-to-one mapping on individual calls and no change in transport binding.

The only reason you would want to create a wrapper in the first place is that the interface exposed in some way does not meet your requirement on it's own. There are several reasons you may want to do this but a few common ones:

  1. Protocol translation - the service is not exposed across the correct transport binding for your needs
  2. Service Composition - the interface operations are too granular and don't represent business-level operations.
  3. Authentication - perhaps you require an authentication layer on top of the endpoint you are consuming.

So how to wrap the service endpoint depends on why you want to wrap the service...

tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • 1
    The way I read the question was that it was an external WCF service. Hooking such a service directly into your code is reckless and risky. What if the external provider changes their interface on a regular basis, or closes down? You would then need to update all of the areas where interaction with the external system occurs with the updated interface or new provider. Including all of the wrapping logic in one place makes sense in this regard. I may have completely misunderstood the original question though. – Graymatter Apr 30 '12 at 13:13
  • @Graymatter , you understood correctly. and the reasons you mentioned are the reasons why I want to have this dll – Night Walker Apr 30 '12 at 13:20
  • 1
    Thanks for your response. I concede that this could be a reason to wrap your service in a like-for-like proxy but in reality how often is a public endpoint going to change it's contract? And if it does, wouldn't you want to update all your consumers to use the latest one? I think it's overkill to have an extra moving part in your solution just for this one purpose. – tom redfern Apr 30 '12 at 14:35
  • It's a bit weird but I am happier with more moving parts. I prefer to decouple the solution as much as possible. In my experience, interfaces, even public ones have a habit of changing. Quite often it's not even technical reasons for making a change. Often things like licensing and cost come into effect. The last thing you want is a supplier having you by the short and curly's. That's why I proposed the interface approach, adding an interface to the mix even decouples the solution further. I will update my answer with an example in a short while. – Graymatter Apr 30 '12 at 15:28
  • Valid points. Encapsulating repetative web service setups and simplifying web service calls makes sence. – J Pollack Oct 24 '18 at 10:02