Service Fabric AspNet Core 3.1 Autofac WebHostBuilder - autofac

I have service fabric stateless asp.net core 2.2 application. I trying to upgrade this to asp.net core 3.1. I am using autofac dependency injection container. As per autofac documentation DI registration moved
from WebHostBuilder to Generic HostBuilder https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting. But Service fabric doesn't support asp.net core Generic Host https://github.com/microsoft/service-fabric-aspnetcore/issues/48.
Is there any other way register Autofac in WebHostBuilder?

I think the official stance is that you should provide the generic host implementation yourself (https://github.com/Microsoft/service-fabric-aspnetcore/issues/48)
I do however think that I have a workaround for you (I just started using this myself). You need to modify it to configure whatever you do, but the important line is services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<ContainerBuilder>>(new AutofacServiceProviderFactory(null)));
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new[]
{
new ServiceInstanceListener(
serviceContext => new KestrelCommunicationListener(
serviceContext,
(url, listener) =>
{
return WebHost
.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<ContainerBuilder>>(new AutofacServiceProviderFactory(null)));
services.AddSingleton(serviceContext)
})
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.UseUniqueServiceUrl | ServiceFabricIntegrationOptions.UseReverseProxyIntegration)
.UseStartup<TStartupType>()
.Build();
}))
};
}

Related

Failed to load API definition Fetch Error, when deployed but fine on localhost

I am upgrading my api from netcore2.1 to 3.1
When I run on localhost the UI works fine.
When I deploy via Azure DevOps and go to the myapplication/myapi/swagger.html url I get
Failed to load API definition
Fetch Error
Service Unavailable /myapi/swagger/v1/swagger/json
Yet I can see the json at
myapplication/myapi/swagger/v1/swagger.json
I have the following
public static IApplicationBuilder UseSwaggerDocumentation(this IApplicationBuilder app)
{
app.UseSwagger(c =>
c.RouteTemplate = "myapi/swagger/{documentName}/swagger.json"
);
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/myapi/swagger/v1/swagger.json", "Versioned API v1.0");
c.RoutePrefix = "myapi/swagger";
});
return app;
}
I am using
Swashbuckle.AspNetCore (5.2.0)
I found the following worked.
public static IApplicationBuilder UseSwaggerDocumentation(this IApplicationBuilder app)
{
app.UseSwagger(c =>
c.RouteTemplate = "myapi/{documentName}/swagger.json"
);
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./v1/swagger.json", "Versioned API v1.0");
c.RoutePrefix = "myapi";
});
return app;
}
The docs state
If using directories with IIS or a reverse proxy, set the Swagger
endpoint to a relative path using the ./ prefix. For example,
./swagger/v1/swagger.json. Using /swagger/v1/swagger.json instructs
the app to look for the JSON file at the true root of the URL (plus
the route prefix, if used). For example, use
http://localhost://swagger/v1/swagger.json instead
of
http://localhost:///swagger/v1/swagger.json.
However unfortunately my solution doesn't work with Autorest.
Thus I asked another question

Container managed MongoDB Connection in Liberty + Spring Data

We have developed an application in Spring Boot + spring data (backend) + MongoDB and used IBM Websphere Liberty as application Server. We were used "Application Managed DB Connection" in an yml file and enjoyed the benefit of Spring Boot autoconfiguration.
Due to policy changes, we would need to manage our DB Connection in Liberty Server(using mongo feature), in Server.xml. I spent whole day in finding out an good example to do this, but dont find any example in Spring with "Container Managed MongoDB Connection" in IBM Websphere Liberty Server.
Can someone please support here?
Check out this other stackoverflow solution. The following is an extension of how you would use that in your Spring Boot app.
You should be able to inject your datasource the same way. You could even inject it into your configuration and wrap it in a Spring DelegatingDataSource.
#Configuration
public class DataSourceConfiguration {
// This is the last code section from that link above
#Resource(lookup = "jdbc/oracle")
DataSource ds;
#Bean
public DataSource mySpringManagedDS() {
return new DelegatingDataSource(ds);
}
}
Then you should be able to inject the mySpringManagedDS DataSource into your Component, Service, etc.
In the past Liberty had a dedicated mongodb-2.0 feature for the server.xml, however this feature provided pretty minimal benefit, since you still needed to bring your own MongoDB libraries. Also, over time MongoDB made significant breaking changes to their API, including how MongoDB gets configured.
Since the MongoDB API is changing so drastically between releases, we found it better to not provide any new MongoDB features in Liberty and instead suggest that users simply use a CDI producer like this:
CDI producer (holds any configuration too):
#ApplicationScoped
public class MongoProducer {
#Produces
public MongoClient createMongo() {
return new MongoClient(new ServerAddress(), new MongoClientOptions.Builder().build());
}
#Produces
public MongoDatabase createDB(MongoClient client) {
return client.getDatabase("testdb");
}
public void close(#Disposes MongoClient toClose) {
toClose.close();
}
}
Example usage:
#Inject
MongoDatabase db;
#POST
#Path("/add")
#Consumes(MediaType.APPLICATION_JSON)
public void add(CrewMember crewMember) {
MongoCollection<Document> crew = db.getCollection("Crew");
Document newCrewMember = new Document();
newCrewMember.put("Name",crewMember.getName());
newCrewMember.put("Rank",crewMember.getRank());
newCrewMember.put("CrewID",crewMember.getCrewID());
crew.insertOne(newCrewMember);
}
This is just the basics, but the following blog post goes into much greater detail along with code examples:
https://openliberty.io/blog/2019/02/19/mongodb-with-open-liberty.html

loadbalanced ribbon client initialization against discovery service (eureka)

I have service which runs some init scripts after application startup (implemented with ApplicationListener<ApplicationReadyEvent>). In this scripts I need to call another services with RestTemplate which is #LoadBalanced. When the call to service is invoked there's no information about instances of remote service because discovery server was not contacted at that time (I guess).
java.lang.IllegalStateException: No instances available for api-service
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:79)
So is there way how to get list of available services from discovery server at application startup, before my init script will execute?
Thanks
edit:
The problem is more related to fact, that in current environment (dev) all services are tied together in one service (api-service). So from within api-service I'm trying to call #LoadBalanced client api-service which doesn't know about self? Can I register some listener or something similar to know when api-service (self) will be available?
here are the sample applications. I'm mainly interested how to have working this method
edit2:
Now there could be the solution to create EurekaListener
public static class InitializerListener implements EurekaEventListener {
private EurekaClient eurekaClient;
private RestOperations restTemplate;
public InitializerListener(EurekaClient eurekaClient, RestOperations restTemplate) {
this.eurekaClient = eurekaClient;
this.restTemplate = restTemplate;
}
#Override
public void onEvent(EurekaEvent event) {
if (event instanceof StatusChangeEvent) {
if (((StatusChangeEvent) event).getStatus().equals(InstanceInfo.InstanceStatus.UP)) {
ResponseEntity<String> helloResponse = restTemplate.getForEntity("http://api-service/hello-controller/{name}", String.class, "my friend");
logger.debug("Response from controller is {}", helloResponse.getBody());
eurekaClient.unregisterEventListener(this);
}
}
}
}
and then register it like this:
EurekaEventListener initializerListener = new InitializerListener(discoveryClient, restTemplate);
discoveryClient.registerEventListener(initializerListener);
However this is only executed only when application is registered to discovery service first time. Next time when I stop the api-service and run it again, event is not published. Is there any other event which can I catch?
Currently, in Camden and earlier, applications are required to be registered in Eureka before they can query for other applications. Your call is likely too early in the registration lifecycle. There is an InstanceRegisteredEvent that may help. There are plans to work on this in the Dalston release train.

Web Api 2 + OWIN 3 + NInject.Web.WebApi.OwinHost, error at startup only

I am building a rest API with Web API2, Owin 3 and NInject Owinhost for my DI.
Using the example provided by NInject, by which I create an "HttpConfiguration" object and call the NInject extension methods in startup.cs, I get an error:
Error activating HttpConfiguration
More than one matching bindings are available.
Matching bindings:
1) binding from HttpConfiguration to method
2) self-binding of HttpConfiguration
3) binding from HttpConfiguration to constant value
Activation path:
1) Request for HttpConfiguration
Suggestions:
1) Ensure that you have defined a binding for HttpConfiguration only once.
My code is as follow in Startup.cs:
public void Configuration(IAppBuilder app)
{
Logger.Info("Entering Startup");
config = new HttpConfiguration();
ConfigureOAuth(app);
// Web API configuration and services
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter("Bearer"));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {id = RouteParameter.Optional}
);
var appXmlType =
config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(
t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
app.UseNinjectMiddleware(CreateKernel);
app.UseNinjectWebApi(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
Logger.Info("Exiting Startup");
}
public static StandardKernel CreateKernel()
{
kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
kernel.Bind<HttpConfiguration>().ToSelf().Named("TestHttpConfiguration");
return kernel;
}
The strange thing is when I refresh the page in the browser, the error goes, which leads me to believe that this happens at application startup only.
So I'm confused with this. Has anyone faced the same issue with it?
Thanks
Vincent
I had this same error, as for some reason I had installed both Ninject.Web.WebApi.WebHost and Ninject.Web.WebApi.OwinHost.
If you look in source for OwinWebApiModule.cs and WebApiWebHostModule.cs, both Ninject modules have a binding for HttpConfiguration.
I removed the one I didn't need and things worked.
UPDATE
After trying everything, I managed to get it to work by... Starting a new project from scratch. I had the luxury of doing this because it is a new proof of concept for me.
The main difference here is I installed the packages required (owin 3, ninject) using the Package Manager console rather than the UI. I followed this link here to install those packages.
I then noticed an error message on one of the package as it was looking for Owin 2 dependencies and could not find any. I forced it to install using -DependencyVersion Highest as parameter and it was working fine from the outset.
Unless I missed it I didn't see this error when I installed the packages using the UI. Is it possible the package didn't install properly previously on my other project? Not sure.
Hope this helps someone.

ClassCastException On JBOSS OSGI 7.1

I use JBOSS OSGI 7.1 for my project.
I have 2 bundle:
usermanagement (service provider)
jerseyBundle (service consumer)
When I deploy and start usermanagement bundle,
Then deploy and start jersey bundle.
jerseyBundle getServiceReference() successful.
Then.
I try to redeploy and restart usermanagement. Then refresh all bundles.
JerseyBundle getServiceReference() with Exception: "ClassCastException"
This is code I use to get service:
public <T> T getService(Class<T> type,List<ServiceReference> _sref) {
try {
ServiceReference sref = bundleContext.getServiceReference(type.getName());
if(sref != null)
{
_sref.add(sref);
}
return type.cast(bundleContext.getService(sref));
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
I use blueprint to register services.
I tried to ungetservice but it don't solved this problem.
public void unGetService(List<ServiceReference> _sref) {
try{
while(_sref != null && _sref.size() >0 )
{
System.err.println("==============" + bundleContext.ungetService(_sref.remove(0)));
}
}catch(Exception ex){
ex.printStackTrace();
}
}
Are there any ways to redeploy service provider bundle, don't need redeploy service consumer bundle?
The reason for the observed behaviour may be that OSGi caches the service object by bundle. So if you do bundleContext.getService(sref) then OSGI will store this object internally and always return the same until you do ungetService.
So when you update the service bundle which also contains the interface and refresh the client you will have a new class for the interface. If you now do a cast of an old service object to the new interface the ClassCastException will occur.
One way to cope with this is to only use the service object for a short period of time and then unget it. Like this:
ServiceReference sref = bundleContext.getServiceReference(type.getName());
myO = type.cast(bundleContext.getService(sref));
doStuff(myO);
bundleContext.ungetService(sref)
Of course this is only practicable for infrequent calls as you have some overhead.
The other way is to use a ServiceTracker and react on service additions and removals. So for example you could inject a service into your class which does "doStuff" and remove / replace the service when there are changes. This is quite hard to do on your own though.
In fact this is the reason why there are frameworks like declarative services (DS) or blueprint. These make sure to reinject service and restart your components when services come and go.
As you are already using blueprint on the provider side you might try to use it on the client side too. The blueprint client should not have the problems you observed.
Btw. blueprint and DS handle service dynamics very differently. Blueprint injects a proxy once and then just replaces the service object inside the proxy while DS will really restart your user component.