How to build a good service layer in ASP.NET

asp.net-mvcwcfweb services

I have looked through some questions, technologies for building a good service layer but I have some questions regarding this that I need help with.

First some information of what I have for requirements. We currently have a number of web applications that talk to each other in a spiderweb looking way (all talking to each other in a confusing way via webservices and database data).

  • We want to change this so that all applications go through a service layer where we can work more with cache and encapsulate common functionality and more.
  • We want this layer to also have a Web API so that 3rd party clients can consume information from the service.

The problem I see is that if we build the service layer with say MVC4 Web API don't we need to communicate between the application using the webAPI meaning we have to construct URLs and consume JSON/Xml. That does not sound too effective. I assume a better method would be working with entities and WCF to communicate between the application but then we might loose the Web API magic?

So the question is if there is a way to consume a service layer as both a Web API (JSON/XML) and as a more backend service layer with entities. If we are forced to use 2 different service layers we might have to duplicate some functionality and other bad things.

Hope the question is clear enough and please ask if you need any more information.

Best Answer

For a set of applications that are hosted on intranet and possibly in one LAN, the best can be a TCP connection for services.

In that sense, I shall suggest, creating a service application that expose a set of endpoints and service interfaces that can work on TCP. These can be consumed by the home-grown or trusted applications.

The service application whould have another set of endpoints and service interfaces that are exposed for consuming over web (web API).

So the service application can be hosted using WCF and endpoints/interfaces shoukd be added in accordance to the need.