For the Connecting to LemonEdge project to funtion you need to following reference:
using LemonEdge.API.Client.CommonUI;
#if LemonEdgeDB
using LemonEdge.ClientDB.Host;
#else
using LemonEdge.ClientService.Host;
#endif
using LemonEdge.Examples.ConnectingToLemonEdge;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
To connect to lemonedge you will need to build a host and configure services. This is a minimal demonstrations of how to connect to lemonedge
This project is the most basic starter example, we:
- Reference the LemonEdge NuGet to connect directly to a database (LemonEdge.ClientDB.Host) or via a web service (LemonEdge.ClientService.Host)
- Registering an IConnector then gives you access to the system.
- We can inject that interface into our background "Client" service and interact with the system.
try
{
// The host reads our application settings and spins everything up: https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices(ConfigureServices)
.Build();
host.Services.InitialiseLemonEdge();
await host.RunAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
The services are registered with the ConfigureServices function. AuthenticationService is a example service that is being registered in this example.
void ConfigureServices(HostBuilderContext hostContext, IServiceCollection services)
{
// LemonEdge services required by an end-client are registered with a call to ConfigureApplication
services.ConfigureLemonEdge(hostContext.Configuration);
// Your own services can be appended here, along with the connector to LemonEdge
services.AddScoped<IConnector, Connector>();
services.AddSingleton<IAuthenticationService, AuthenticationService>();
// Finally, we need to define the application host service for start-up
services.AddHostedService<Worker>();
}
The worker class performs a simple operation getting the service version. The class needs to inherit BackgroundService
public class Worker : BackgroundService
Constructor receiving dependencies through injection, it needs to take the following. lifetime: The IHostApplicationLifetime needed to manage application lifetime. authenticator: Example service to authenticate to LemonEdge.
private readonly IHostApplicationLifetime _lifetime;
private readonly IAuthenticationService _authenticator;
/// <summary>
/// Constructor receiving dependencies through injection.
/// </summary>
/// <param name="lifetime">The <see cref="IHostApplicationLifetime" /> needed to manage application lifetime.</param>
/// <param name="authenticator">Example service to authenticate to LemonEdge</param>
public Worker(IHostApplicationLifetime lifetime, IAuthenticationService authenticator)
{
_lifetime = lifetime;
_authenticator = authenticator;
}
ExecuteAsync gets the app service version then logs into the service as a example of how to interact with the service.
protected override async Task ExecuteAsync(CancellationToken cancelToken)
{
await _authenticator.GetAppServiceVersion();
await _authenticator.Login("root@lemonedge.com", "!ltroot");
_lifetime.StopApplication();
}
The IAuthenticationService service and its implementation that were used in the worker
IAuthenticationService
/// <summary>
/// Helper Service for authenticating to LemonEdge
/// </summary>
public interface IAuthenticationService
{
/// <summary>
/// Unauthenticated request to return the AppServiceVersion of the application that last upgraded the database.
/// </summary>
Task<Version?> GetAppServiceVersion();
/// <summary>
/// Authenticates to LemonEdge returning a <see cref="UserInfo"/> object.
/// </summary>
/// <param name="username">The username of the user to authenticate with</param>
/// <param name="password">The password of the user to authenticate with</param>
/// <param name="reloadDynamicEntities">Flag for reloading add-ins and custom objects/lists</param>
/// <returns><see cref="UserInfo"/> Object holding information about the currently logged in user.</returns>
Task<UserInfo> Login(string username, string password, bool reloadDynamicEntities = false);
/// <summary>
/// Authenticates to LemonEdge returning a <see cref="UserInfo"/> object. Uses configuration to populate the username
/// and password.
/// </summary>
/// <returns><see cref="UserInfo"/> Object holding information about the currently logged in user.</returns>
Task<UserInfo> Login();
}
AuthenticationService is the implementation of IAuthenticationService it has basic methods that utilise the connector.
public class AuthenticationService : IAuthenticationService
{
private readonly IConfiguration _configuration;
private readonly ILogger<AuthenticationService> _logger;
private readonly IConnector _connector;
public AuthenticationService(IConnector connector, ILogger<AuthenticationService> logger, IConfiguration configuration)
{
_connector = connector;
_logger = logger;
_configuration = configuration;
#if !LemonEdgeDB
_connector.UpdateServiceConnection(_configuration.GetValue<string>("LemonEdge:ConnectionSettings:ServiceURI")).GetAwaiter().GetResult();
#endif
}
public async Task<Version?> GetAppServiceVersion()
{
// the connector gives you application information, as well as login and poco entity access.
var version = await _connector.CreateAuthenticator().GetAppServiceVersion();
_logger.LogInformation($"LemonEdge version: {version}");
return version;
}
public async Task<UserInfo> Login(string username, string password, bool reloadDynamicEntities = false)
{
var user = await _connector.Login("root@lemonedge.com", "!ltroot", reloadDynamicEntities);
_logger.LogInformation($"Logged in user: {user.Name}");
return user;
}
public async Task<UserInfo> Login()
{
var username = _configuration.GetSection("LemonEdge:Authentication:Username").Value;
var password = _configuration.GetSection("LemonEdge:Authentication:Password").Value;
var reloadEntities = _configuration.GetSection("LemonEdge:Authentication").GetValue<bool>("ReloadDynamicEntities");
return await Login(username, password, reloadEntities);
}
}