Polly.Extensions.Http not accessible for IHttpClientFactory - .Net6 - asp.net-core

I need to use Polly.Extensions.Http for same reason.
I install Polly and Polly.Extensions.Http packages, and have these using's
global using Polly;
global using Polly.Retry;
global using Polly.Timeout;
global using Polly.Extensions.Http;
but when need add to HttpClient, It's not available!
I use these refrence

To summarize the discussion in the comments:
Like Peter Csala said: "The Polly.Extensions.Http package is deprecated. It has not been modified more than 3 years."
The new way to use Polly with the HttpClientFactory is described in the wiki of the Polly github. It seems to use Microsoft.Extensions.Http.Polly.
E.g.
services.AddHttpClient("GitHub", client =>
{
client.BaseAddress = new Uri("https://api.github.com/");
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
})
.AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
}));

Related

What is the MassTransit pattern for retrieving RabbitMQ password from a dependency?

Using MassTransit.RabbitMq 8.0.9, in a .Net Core 3.1 project using AspNetCore 3.1.10 and IServiceContainer. The password for RabbitMq is stored in a secrets vault, accessible from a dependency-injected interface. All of the examples I've been able to find just get the password from configuration.
I'd like to do something like
var secrets = serviceProvider.GetRequiredService<ISecretRetrieval>();
var rabbitPassword = secrets.GetRabbitMqPassword();
and then hand that password to IRabbitHostConfigurator, but inside UseMassTransit...UseRabbitMq, there isn't an IServiceProvider instance that I've seen.
Alternatively, I could create a configuration object with a constructor-injected dependency on ISecreteRetrieval. I see examples for IConfiguration<MassTransitHostOptions> that show how to create and register my own class with its own constructor dependencies. Can I do that with IConfiguration<RabbitMqHostSettings> even though RabbitMqHostSettings is an interface, not a class like MassTransitHostOptions?
In the UsingRabbitMq block, the first parameter is a service provider.
x.UsingRabbitMq((context, cfg) =>
{
var secrets = context.GetRequiredService<ISecretRetrieval>();
cfg.Host("hostname", h =>
{
h.Password(secrets.GetRabbitMqPassword());
});
});

ServiceStack - IAuthRepository vs IUserAuthRepository

I’ve to configure my web application to use the ServiceStack built-in ApiKeyAuthProvider. I’ve registered in the container the OrmLiteAuthRepository with the IAuthRepository interface but it throws an exception saying that I’ve not registered the IUserAuthRepository.
Could someone explain me the difference?
Thanks in advance
EDIT:
Sorry, i've made confusion
The error is
System.NotSupportedException: 'ApiKeyAuthProvider requires a registered IAuthRepository'
Our AppHost's Configure method is
public override void Configure(Container container)
{
var dbFactory = new OrmLiteConnectionFactory("connString", SqlServerDialect.Provider);
container.Register<IDbConnectionFactory>(dbFactory);
container.Register<IUserAuthRepository>(_ => new OrmLiteAuthRepository(dbFactory));
container.Resolve<IUserAuthRepository>().InitSchema();
var authProvider = new ApiKeyAuthProvider()
{
RequireSecureConnection = false
};
Plugins.Add(new AuthFeature(
() => new AuthUserSession(),
new IAuthProvider[] {
authProvider
}
));
}
Could you explain me the difference between these two interfaces? we can't figure out (ServiceStack v.6.0.2)
Please refer to the Auth Repository docs for examples of correct usage, e.g:
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(connectionString, SqlServer2012Dialect.Provider));
container.Register<IAuthRepository>(c =>
new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>()));
container.Resolve<IAuthRepository>().InitSchema();
The IAuthRepository is the minimum interface all Auth Repositories have to implement whilst IUserAuthRepository is the extended interface to enable extended functionality to enabled additional features which all ServiceStack built-in Auth Repositories also implement. But you should never need to register or resolve a IUserAuthRepository, i.e. they should only be registered against the primary IAuthRepository interface.
Resolving Auth Repository
If you need to, the Auth Repository can be accessed from base.AuthRepository or base.AuthRepositoryAsync in your Service where you'll be able to use any IUserAuthRepository APIs since they're all available as extension methods on IAuthRepository, e.g. This example Service calls the IUserAuthRepository.GetUserAuth() method:
public class MyServices : Service
{
public object Get(MyRequest request) =>
AuthRepository.GetUserAuth(request.UserId);
}
Whilst here are the recommended APIs to access the Auth Repository outside of your Services:
var authRepo = HostContext.AppHost.GetAuthRepository();
var authRepoAsync = HostContext.AppHost.GetAuthRepositoryAsync();

What is the proper way to set up API endpoints for usage with Keystone?

It's not clear in the docs how one would use existing Keystone models to expose API endpoints that return json within a Keystone.js app. I would simply like to be able expose REST API endpoints with Keystone and be able to use the Keystone CMS capabilities to manage content via interacting with those endpoints. Thanks!
Now that they've standardized the admin API I found that it's pretty trivial to use the same methods. For my read only APIs that are powering my react app I've done put something like this in my routes/index.js
router.get('/api/:list/:format(export.csv|export.json)',middleware.initList,require('keystone/admin/server/api/list/download'));
And I've made my own version of the admin initList middleware:
exports.initList = function(req, res, next) {
console.log('req.keystone', req.keystone);
req.keystone = keystone;
req.list = keystone.list(req.params.list);
if (!req.list) {
if (req.headers.accept === 'application/json') {
return res.status(404).json({ error: 'invalid list path' });
}
req.flash('error', 'List ' + req.params.list + ' could not be found.');
}
next();
};
You may consider using:
restful-keystone by #creynders, or
keystone-rest by #danielpquinn
I've never actually used either of these because I have my own implementation, which I will open source once Keystone implements it plugin architecture (see Keystone Issue #912: Proposed Keystone Package Architecture).
I suspect many other similar modules will start surfacing once Keystone is more "plugin friendly".

Why is MSpec's ShouldBeOfType<T> assertion extension method missing?

I'm trying to use Mspec's ShouldBeOfType<T>() assertion extension method, but intellisense says that it can't find it. I'm using MSpec v0.7.0. I tried reinstalling using Nuget but didn't work.
[Subject("Prop Manager")]
public class When_Replying_To_Prop_Which_Already_Had_Emailed_And_No_Overwrite
{
Because of = () => _exception = Catch.Exception(() => _PropManager.ReplyToProp());
It should_result_in_an_error = () => _exception.ShouldBeOfType<InvalidOperationException>();
private static Exception _exception;
}
As of version 0.7.0, Machine.Specifications does not include assertions any more (see Daniel Marbach's blog). You have to install Machine.Specifications.Should (or another assertions library).
Additionally, ShouldBeOfType() has been replaced with ShouldBeOfExactType() (or ShouldBeAssignableTo() respectively), so you should also change that in your code (see github issue.

PooledRedisClientManager throws with Appharbor Redis URL

I am trying out Redis on Appharbor in an MVC4 application. I am using the ServiceStack C# client for Redis. Everything was working when using the RedisClient from ServiceStack.Redis. However, because I only plan to use Redis for caching, I attempted to wire up the ICacheClient that ServiceStack provides as a wrapper. Here is my StructureMap configuration (https://github.com/ServiceStack/ServiceStack/wiki/Caching):
x.For<IRedisClientsManager>().Use(() => new PooledRedisClientManager(redisUrl));
x.For<ICacheClient>().Use(c => c.GetInstance<IRedisClientsManager>().GetCacheClient());
My problem is that the PooledRedisClientManager is throwing error, "input string was not in a correct format" when I use the Redis-to-Go URL provided by Appharbor. Here is what that looks like:
redis://redistogo-appharbor:abunchofrandomcharacters#drum.redistogo.com:9081/
If I replace the Redis-to-Go URL with localhost:5051 everything works.
What am I missing?
Prefixing a redis:// is not any known redis convention - it must be a RedisToGo or AppHarbor convention.
ServiceStack's C# RedisClient supports standard "password#host:port" convention, e.g:
container.Register(c => new PooledRedisClientManager(
"redistogo-appharbor:abunchofrandomcharacters#drum.redistogo.com:9081"
));
Something like this worked for me:
container.Register(c => new PooledRedisClientManager(
#"abunchofrandomcharacters#drum.redistogo.com:9081"
));
After some trial and error this works.
string redisUrl = ConfigurationManager.AppSettings["REDISTOGO_URL"].Replace("redis://", "").Replace("redistogo:","").Replace("/", "");
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
SetConfig(new HostConfig{HandlerFactoryPath = "api"});
container.Register<IRedisClientsManager>(c => new PooledRedisClientManager(redisUrl));
container.Register<IDbRepository>(r => new DbRepository(r.Resolve<IRedisClientsManager>()));