Missing dependency : Program does not run (and freezes)
Here's an example of code where a dependency is missing. The program displays no error and freezes.
using Cocona;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
var builder = CoconaApp.CreateBuilder();
builder.Services.AddSingleton<Class1>();
builder.Logging.AddConsole()
.AddFilter(f => f >= LogLevel.Debug);
var app = builder.Build();
app.AddCommands<MyCommands>();
app.Run();
public class MyCommands
{
public MyCommands(Class1 c1){}
public void DoStuff()
{
Console.WriteLine("Hello, World!");
}
}
public class Class1
{
public Class1(Class2 c2){}
}
public class Class2{}
the output is as follows, nothing else happens:
dbug: Microsoft.Extensions.Hosting.Internal.Host[1]
Hosting starting
dbug: Microsoft.Extensions.Hosting.Internal.Host[2]
Hosting started
I also experienced this. Took me a while to realize it was caused by missing dependencies.
Experiencing the same issue; any advice on how to capture dependency resolution errors?
I'm also experiencing this.
Any update on this?
I have used the same thing but with minimal api command style, and I got the proper exception.
using Cocona;
using Microsoft.Extensions.DependencyInjection;
using static System.Console;
var builder = CoconaApp.CreateBuilder();
builder.Services.AddSingleton<Class1>();
//builder.Services.AddSingleton<Class2>();
var app = builder.Build();
app.AddCommand((Class1 c1) =>
{
var msg = c1.GetMsg();
WriteLine(msg);
});
app.Run();
class Class1(Class2 c2)
{
private Class2 _c2 = c2;
public string GetMsg() => _c2.GetMsg();
}
class Class2
{
public string GetMsg() => "Hello";
}
Exception:
Unhandled Exception: System.InvalidOperationException: Unable to resolve service for type 'Class2'
while attempting to activate 'Class1'.
But when I use separate command class then my application freezes.
So, this bug is only for those cases when you are using separate command class, but if you are using minimal api style commands then you will get proper exception.
Any update on this? still having this issue
There is no fix has been provided for this problem, but I am talking about a workaround.
For example, I have created a console app with cocona NuGet package, and in that I have forgot to register a type in the DI container. The build will be successful but when I deploy the app in my customer's machine and the customer will run the app then it will fail/freeze. There is no way I can be notified about the missing type registration at the time of building the app.
One workaround can be to add an integration test which will verify if all the types are properly registered in the DI container or not. Then I can run this test in the GitHub action workflow before the actual deployment of the app to the customer's machine. This test can notify me about the missing type registration problem before the customer can be able to find it.
flowchart TD
start([Start])
stop([Stop])
commit[Commit code in GitHub]
build[Trigger build in GitHub action]
test[Trigger integration test in GitHub action]
deploy[Deploy to customer's machine]
isBuildSuccess{Is build success?}
isTestSuccess{Is test success?}
start --> commit
commit --> build
build --> isBuildSuccess
isBuildSuccess -- Yes --> test
isBuildSuccess -- No --> stop
test --> isTestSuccess
isTestSuccess -- Yes --> deploy
isTestSuccess -- No --> stop
deploy --> stop