Echo Test Application
The main task, which is solved by NFX.Glue is simply exchange messages between contract-implementing/consuming endpoints. Below is step-by-step guide to create the simplest NFX.Glue server application which receives text message and returns echo.
1. Create blank C# solution.
2. Add new C# Class Library project DemoContracts
.
3. Add reference to NFX (e.g. via NuGet).
4. Add IEchoService.cs file with the following contents:
using NFX.Glue;
namespace DemoContracts
{
[Glued]
public interface IEchoService
{
string Echo(string text);
}
}
This is the contract for our simple server (note NFX.Glue.Glued
attibute). The server obviously accepts string message and returns some echo.
5. Create new C# Console Application project DemoServer
.
6. Add references to NFX and DemoContracts
project.
7. Add EchoService.cs file with the contents:
using DemoContracts;
namespace DemoServer
{
public class EchoService : IEchoService
{
public string Echo(string text)
{
return "Your message is: " + text;
}
}
}
The DemoServer.EchoService
class represents strong typed service and contains all the business logic.
8. We need properly setup server configuration i.e. service address, binding etc. Add configuration file DemoServer.laconf with the following contents
application
{
glue
{
bindings
{
binding
{
name="sync"
type="NFX.Glue.Native.SyncBinding, NFX"
}
}
servers
{
server
{
node="sync://localhost:8080"
contract-servers=$"DemoServer.EchoService, DemoServer"
}
}
}
}
Be sure to set Build Action : None and Copy to Output Directory: Always in the file properties.
Thus, we declare that we want NFX.Glue.Native.SyncBinding
- synchronous communication pattern between the client and the server,
and also expose our servise at the port 8080 (use another one if it is busy).
9. Add the following code to the Main
method of DemoServer.Program.cs file
using (var application = new ServiceBaseApplication(args, null))
{
Console.WriteLine("server is running...");
Console.WriteLine("Glue servers:");
foreach (var service in App.Glue.Servers)
Console.WriteLine(" " + service);
Console.ReadLine();
}
The only neccessary code above is start ServiceBaseApplication
and use Console.ReadLine()
to prevent the app from immediately shupdown.
10. Add the last one project DemoClient
- a client-side. It can be the project of any type (Console, WinForms, WPF, Web etc.)
but for the sake of simplicity we choose C# Console Application for demonstration purposes.
11. Add references to NFX and DemoContracts
project.
12. Add configuration file DemoClient.laconf:
application
{
glue
{
bindings
{
binding
{
name="sync"
type="NFX.Glue.Native.SyncBinding, NFX"
}
}
}
}
(the binding must coincide with the ones on server-side).
Again, be sure to set Build Action : None and Copy to Output Directory: Always in the file properties.
13. The last step is to create client proxy class that will operate with the service above.
One can create this client class directly but it is more suitable to use out-of-the-box utility called gluec to generate service contracts automatically.
The utility accepts the path to DemoContracts.dll, output path and (optionally) autogenerated class suffix.
It extracts all contracts marked with NFX.Glue.Glued
attribute and generates handy client classes one for each service contract.
It is preferable to automate client classes generation instead of run gluec utility every time the contracts were chenged/added.
For to moment we simpty add th following script to Post-build event of DemoContracts
project:
"$(SolutionDir)packages\NFX.3.1.0.1\tools\gluec" "$(SolutionDir)DemoContracts/bin/Debug/DemoContracts.dll" /o out="$(SolutionDir)DemoClient" cl-suffix="AutoClient"
Rebuild DemoContracts
project and add the file EchoServiceAutoClient.cs to the DemoClient
project (the file will appear in the root of the project).
14. Add the following code to the Main
method of DemoClient.Program.cs:
using (var application = new ServiceBaseApplication(args, null))
{
var message = Console.ReadLine();
using (var client = new EchoServiceAutoClient("sync://localhost:8080"))
{
var responce = client.Echo(message);
Console.WriteLine(responce);
}
}
15. Run the DemoServer
server host and start the DemoClient
client application.