Configuring Context
You may want to provide a Polly Context, thanks to WithContext builder option, available at both register and request time.
Configuring a context at register time allows you to get a pre-defined one while requesting.
WithContext builder option is available with or without using registry.
It means that you can share a context globally by setting it at registry level and/or set some specific one at api level.
As it's not recomended to share the same context instance between requests, WithContext registration option comes with a factory registration only.
Here is a quite simple scenario:
var reqResUserManager = ApizrBuilder.Current.CreateManagerFor<IReqResUserService>(options => options
.WithContext(() => new Context { { "testKey1", "testValue1" } }));
And here is a pretty complexe scenario:
private Context FirstContextFactory() => new() { { "testKey1", "testValue1" } };
private Context SecondContextFactory() => new() { { "testKey2", "testValue2" } };
private Context ThirdContextFactory() => new() { { "testKey3", "testValue3" } };
var apizrRegistry = ApizrBuilder.Current.CreateRegistry(registry => registry
.AddGroup(group => group
.AddManagerFor<IReqResUserService>(options => options
.WithContext(ThirdContextFactory))
.AddManagerFor<IReqResResourceService>(),
options => options.WithContext(SecondContextFactory))
.AddManagerFor<IHttpBinService>()
.AddCrudManagerFor<User, int, PagedResult<User>, IDictionary<string, object>>(),
options => options.WithContext(FirstContextFactory));
Here I'm telling Apizr to:
- Merge all 3 context together and pass it while requesting with
IReqResUserServiceapi - Merge first and second context and pass it while requesting with
IReqResResourceServiceapi - Pass the first context while requesting with
IHttpBinServiceapi orUserCRUD api
Feel free to configure your context at the level of your choice, depending on your needs. You definitly can mix it all with request option context providing. Keep in mind that the closest key/value to the request will be the one used by Apizr.
You may notice that:
strategyparameter let you adjust the behavior in case of mixing (default:Merge):Ignore: if there's another context yet configured, ignore this oneAdd: add or merge this context with any yet configured onesReplace: replace all yet configured context by this oneMerge: add or merge this context with any yet configured ones