I've been trying 2.0.0 BulletTrain Client SDK on a .NetCore 3.1 WebApi project and on a Console application. For Api calls, my initial plan was to run an on-prem version on Docker. But also I tried "https://app.bullet-train.io/" with a free account.
-
Console Application works properly. But WebApi project fails when a second call is made to the BulletTrain Api.
-
So here's my sample code for Console App,
BulletTrainConfiguration configuration = new BulletTrainConfiguration()
{
ApiUrl = "http://localhost:8000/api/v1/",
EnvironmentKey = "my-local-env-key"
};
BulletTrainClient bulletClient = new BulletTrainClient(configuration);
bool featureEnabled = await bulletClient.HasFeatureFlag("is_it_weekend");
if (featureEnabled) {
// Run the code to execute enabled feature
Console.WriteLine("Hello World!");
} else {
// Run the code if feature switched off
Console.WriteLine("GoodBye World!");
}
- Here is the sample code from the WebApi project, they're almost identical (I used BulletTrain docs for implementation)
BulletTrainConfiguration configuration = new BulletTrainConfiguration()
{
ApiUrl = "http://localhost:8000/api/v1/",
EnvironmentKey = "my-local-env-key"
};
BulletTrainClient bulletClient = new BulletTrainClient(configuration);
bool featureEnabled = await bulletClient.HasFeatureFlag("is_it_weekend");
if (featureEnabled) {
// Run the code to execute enabled feature
return Ok(myWeekendItem);
} else {
// Run the code if feature switched off
return NotFound();
}
- When I run it for the first time, it works properly. Without even changing anything, when I run on the web api the second time, it throws the below exception:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request.
System.NullReferenceException: Object reference not set to an instance of an object.
at BulletTrain.BulletTrainClient.GetFeatureFlags(String identity)
at BulletTrain.BulletTrainClient.HasFeatureFlag(String featureId, String identity)
at Glossary.Controllers.GlossaryController.Get(String term) in /Users/sevikarakose/Projects/Glossary/Controllers/GlossaryController.cs:line 52
at lambda_method(Closure , Object )
at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)