how to use ValidateScopes in autofac? - autofac

if i use default ioc in asp.net core,i can use this option :
WebHost.CreateDefaultBuilder(args)
.UseDefaultServiceProvider((context, options) => {
options.ValidateScopes = true;
})
but how to use this config in autofac? I don't see any relevant documentation on the github homepage
When ValidateScopes is set to true, the default service provider performs checks to verify that:
Scoped services aren't directly or indirectly resolved from the root service provider.
Scoped services aren't directly or indirectly injected into singletons.

ValidateScopes - as with UseDefaultServiceProvider - are both not Autofac. There is no equivalent in Autofac. There is documentation about caprice dependencies but nothing to prevent you from doing it.

Related

How to use UseLoggerFactory in conjunction with UseInternalServiceProvider

We have an asp.net core Web API and registering our EF DB context like so
services
.AddDbContext<SiteDbContext>((serviceProvider, opt) =>
{
opt
.UseNpgsql(options.Site)
.UseInternalServiceProvider(serviceProvider)
;
})
The DB Context calls DbContextOptionsBuilder.UseLoggerFactory which fails.
Looking at this doco https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.useloggerfactory?view=efcore-5.0
It is mentioned that you can't do what I just did above and suggested to "In this case, the ILoggerFactory should be configured directly in that service provider."
My question is how is that specifically done? to configure directly in that service provider.
From the docs (emphasis mine):
Sets the IServiceProvider that the context should resolve all of its services from. EF will create and manage a service provider if none is specified.
The service provider must contain all the services required by Entity Framework (and the database being used). The Entity Framework services can be registered using an extension method on IServiceCollection. For example, the Microsoft SQL Server provider includes an AddEntityFrameworkSqlServer() method to add the required services.
You shouldn't try to replace the internal service provider with the app-level service provider unless you register every single internal service EF core needs.
If you actually need to set the logger factory, there's an easier way to configure it. Use an overload that gives you an IServiceProvider:
services.AddDbContext<AppDbContext>((provider, options) =>
{
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
options
.UseNpgsql(/* ... */)
.UseLoggerFactory(loggerFactory);
});

Accessing config instance from a Lagom service descriptor

Is there a recommended way for injecting / accessing the application Typesafe config instance from within a Lagom service interface (ie, the trait, not Impl)?
The use case I am thinking of is for the creation of a request header filter that depends on a configurable value (like, an env specific secret) and injecting it via a constructor argument.
final override def descriptor: Descriptor = {
import Service._
named("some-service")
.withCalls(
pathCall("/health", healthCheck),
)
.withHeaderFilter(new CustomerHeaderFilter(config))
}
Unfortunately, from within the descriptor function, there is no readily exposed reference to the config. I have tried including as an abstract field on the service but this seems to cause Lagom to bomb out and complain that the field does not generate a service.
Is there a recommended way to do this or do I essentially have to call ConfigFactory.load()?
Currently using Lagom 1.4.5 + Scala - thanks!

Multiple v2 Service Remoting Endpoints in Service Fabric

I'm using Service Fabric v6.1.472. We're trying to switch to using Service Fabric Remoting (https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-remoting), specifically to use the v2 listeners.
The problem I'm running in to is that the documentation only says how to do it using a single listener via the extension method:
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return this.CreateServiceRemotingInstanceListeners();
}
This assumes that
The service class implements the remoting interface
There is only one remoting v2 endpoint in the host (I need multiple endpoints).
When digging into the decompiled code, it looks like this extension method uses a hard-coded endpoint name, which would make it impossible to use this for multiple remoting endpoints. Further digging revealed that many of the methods used by the extension method are marked internal.
Short of creating my own library, has anyone else found a workaround to this bit of bad design?
Edit
Microsoft updated their documentation. Under the header "Using explicit V2 classes to use the V2 stack," it is clear how to create listeners without the extension method.

Spring Boot Starter Data Rest change URL of repository from the root URI

Following the spring.io example here: http://spring.io/guides/gs/accessing-data-rest/ for exposing a repository as a rest web service works just fine, but I cannot see how to change the URL of the exposed service. The API documentation is a little vague as to what the annotation parameters mean, perhaps some prior knowledge is assumed.
What I want - A HATEOAS service accessed at http://localhost:8080/api/people for a People repository. I want to achieve this URL using annotations only, not messing with the context root or similar. I tried the following repository annotations:
#RepositoryRestResource(collectionResourceRel = "api/people", path = "people")
#RepositoryRestResource(collectionResourceRel = "people", path = "api/people")
#RepositoryRestResource(collectionResourceRel = "api/people", path = "api/people")
None of these work.
I know I have probably missed the obvious, much appreciate anyone who can point it out.
As of Spring Boot 1.2 you are able to set this property:
spring.data.rest.baseUri=api
Alternatively:
spring.data.rest.base-uri=api
(Spring Boot uses a relaxed binding system)
NOTE: I have found that if you have extended RepositoryRestMvcConfiguration with custom configuration, the property does not take effect. For more information see:
https://github.com/spring-projects/spring-boot/issues/2392
Once the next version of Spring Boot is released (after 1.2.1), the solution will be to extend RepositoryRestMvcBootConfiguration instead.
As of Spring Boot 1.4.3 the code should be :
spring.data.rest.base-path:api
(I think baseUri is deprecated since 1.2.3)
Although I couldn't change the base path of the REST services using the annotation #RepositoryRestResource combined with a CrudRepository, I managed to do it using a JpaRepository and a custom controller with the annotation #RequestMapping.
The repository could be something like:
#Repository
interface PersonRepository : JpaRepository<Person, Long>
And the controller:
#RestController
#RequestMapping("/api/people")
class PersonRestController(private val personRepository: PersonRepository) {
...
On the other hand, you can change the base path of all your REST services modifying it in the application.properties file of your project. Add the lines:
# DATA REST (RepositoryRestConfiguration)
spring.data.rest.base-path = api
Change api with the path you wish you use in your URLs. The first line is a comment and, as so, it's not mandatory, but is useful to mark the nature of the configuration value for future references.
You can find all the common application properties of Spring Boot 2.0.1 in the Appendix A of the documentation.

GWT GIN HOW TO: injection of remote services

The gin tutorial seems to imply that to inject remote services all you need to do is annotate with #Inject.
Do you you still need to define this in a module somewhere or is the point that you can just annotate with #Inject and it will work?
Gin has automatic support for remote services, as outlined in the tutorial you mentioned:
Every time Gin is asked to inject an asynchronous remote service, it will inject an instance retrieved through calling GWT.create on its regular remote service.
Therefore, it will 'just work'.