Search code examples
c#unit-testingwcfdependency-injection.net-core

Consuming WCF service from Business Logic Layer


I have a solution with the following structure:

  1. Domain (.NET 4.6.1)
  2. DAL (.NET 4.6.1)
  3. BLL (.NET Core 2.0)
  4. API (.NET Core 2.0)

I have a requirement to submit data to an external WCF service. I personally do not have a lot of experience with WCF as I have been working with REST APIs. So to try out consuming a WCF service, I have set up a .NET core console application in and added a connected service reference to it. This resulted in a generated Reference.cs with the following classes:

  • IWebService
  • IWebServiceChannel
  • SendResponse
  • GetResponse
  • WebServiceClient (implementing IWebService)

I was thinking about creating a SubmissionService class in the BLL layer to submit data to the WCF service, so I moved IWebService from the console app to the BLL layer to inject into the class and perform unit testing. The attributes defined on the IWebService requires a reference to System.ServiceModel which comes with a lot of baggage and it does not make sense to reference it in my BLL layer.

I am not sure where to put the WCF client in my project structure. I have been investigating this issue and the only viable solution I can think of is to create a class library project purely for the WCF client and reference that in my BLL layer to have access to IWebService and mock it for testing. Has anyone been in the same situation? Any help is greatly appreciated.


Solution

  • You are missing a 'layer', namely the Composition Root. (for a more detailed explanation, see section 4.1 of this book).

    You can define your own application-specific abstraction that allows the BLL to talk with the WCF service (through that abstraction). That abstraction can be located in the BLL.

    Within your Composition Root, you can create an adapter on top of that application-specific abstraction that calls into the WCF service, using the WCF client, which can be generated inside the Composition Root as well.