Last week, I created an API, which has been able to find and invoke a method in a service. The API was getting all service classes by dependency injection supported by an Autofac container. The next step was to connect the dots and implement a way to invoke a remote service method by using the MiSeCo class.
Just to remind you, how it’s going to work, I will give an example. I have created a simple service called Service1, which implements an interface IService1:
As you have probably already correctly guessed, the only method here is adding two integers (without worrying about adding two big integers and overflowing the int type ;)). This interface is implemented in a class library project called Service1, which is referencing MiSeCo. MiSeCo contains a WebApi controller inside and everything that’s needed to host a web service.
On the other end, we have a simple console application, which is referencing Service1.Contract (containing just the IService1 interface) and MiSeCo. The difference is, that here MiSeCo will just provide a method to find and invoke Service1 methods over an HTTP API.
- The application doesn’t know about the Service1 implementation - it only references its interface
- Service1 has no idea about the Application
To communicate, I had to create an API model:
It contains everything that’s needed to find and invoke a method.
When the application needs to call a service it asks the MiSeCo class for an interface implementation:
Then it can just call the method as if it was a real service implementation.
What’s happening behind the scenes is that MiSeCo is creating a dynamic proxy object with a method interceptor:
When a call comes in, it’s getting the name of the service, its method and its parameters, and creates a model. This model is then sent to the API (service URL is hard-coded for now, support for many services is coming later). The API should return the result, which is converted to a return type and set as a return value of the method.
The MiSeCo WebAPI finds a service implementation and the requested method, then it simply sets all the parameters, invokes it and returns the result as a JSON response.
I have started the service in one console, application in an other and it all worked!
Next, I need to find a way to autodiscover running services. It will be a hard nut to crack.
All the code for MiSeCo is available on Github: https://github.com/mdymel/miseco