0

Sorry I don't have any example code. I'm simply trying to find an example of how to use Lamar with a new .Net 6 console application and not finding anything on google.

The application is basically just a task runner app. It takes in a command line argument of a class name. The class will implement an interface with a Run() method so that the console app can try to find the class registration in the container and then call the .Run() method of the class.

All I'm finding are examples of how to use Lamar in an ASP.NetCore app which isn't what I'm trying to do, and I'm not sure how I can translate it... if I even can.

geoff swartz
  • 5,437
  • 11
  • 51
  • 75

2 Answers2

0

So there are two approaches to achieve that.

  1. You can use the Generic Host Builder, which is kind of the same as ASP.NET Core, as ASP.NET Core is using a specialised implementation of the builder called WebHostBuilder. As an example, your console app will look sth like that ( example copied from this post: How to run .NET Core Console app using generic host builder)
using Microsoft.Extensions.Hosting; // Requires NuGet package    
var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services => { services.AddTransient<MyService>(); })
    .UseLamar() // now you can use this
    .Build();
var my = host.Services.GetRequiredService<MyService>();``
await my.ExecuteAsync();
class MyService
{
    private readonly ILogger<MyService> _logger;

    public MyService(ILogger<MyService> logger)
    {
        _logger = logger;
    }

    public async Task ExecuteAsync(CancellationToken stoppingToken = default)
    {
        _logger.LogInformation("Doing something");
    }
    // and use this just like ASP.NET Core
    public void ConfigureContainer(ServiceRegistry services)
    {
        // Supports ASP.Net Core DI abstractions
        services.AddMvc();
        services.AddLogging();
        
        // Also exposes Lamar specific registrations
        // and functionality
        services.Scan(s =>
        {
            s.TheCallingAssembly();
            s.WithDefaultConventions();
        });
    }
}
  1. Using a pure console app and Lamar container
using Lamar; 

var container = new Container(x => { x.AddTransient<IClock, Clock>();  }); 

container.GetInstance<IClock>();

In this scenario, you will have to share the container and use various methods to get objects from the container. I have not tried it but I believe that you can use BuildMethod to get the object with all dependencies.

Simply you can follow this documentation: https://jasperfx.github.io/lamar/guide/ioc/bootstrapping.html#bootstrapping-a-container on how to see tup but Scanner is similar for both scenarios.

Assassin
  • 1,296
  • 2
  • 14
  • 17
0

I found an answer in this article.

var builder = WebApplication.CreateBuilder(args);
 
// use Lamar as DI.
builder.Host.UseLamar((context, registry) =>
{
    // register services using Lamar
    registry.For<ITest>().Use<MyTest>();
     
    // Add your own Lamar ServiceRegistry collections
    // of registrations
    registry.IncludeRegistry<MyRegistry>();
 
    // discover MVC controllers -- this was problematic
    // inside of the UseLamar() method, but is "fixed" in
    // Lamar V8
    registry.AddControllers();
});
MirrorBoy
  • 618
  • 7
  • 11