Skip to content

Commit b84f4d7

Browse files
authored
Merge pull request #194800 from yashagarwal23/yashagarwal23/ihost-documentation
Service Fabric: IHost and Minimal Hosting code samples
2 parents 52537bb + 79a6c64 commit b84f4d7

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

articles/service-fabric/service-fabric-reliable-services-communication-aspnetcore.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,155 @@ In this configuration, `KestrelCommunicationListener` will automatically select
328328
For HTTPS, it should have the Endpoint configured with HTTPS protocol without a port specified in ServiceManifest.xml and pass the endpoint name to KestrelCommunicationListener constructor.
329329

330330

331+
## IHost and Minimal Hosting integration
332+
In addition to IWebHost/IWebHostBuilder, `KestrelCommunicationListener` and `HttpSysCommunicationListener` support building ASP.NET Core services using IHost/IHostBuilder.
333+
This is available starting v5.2.1363 of `Microsoft.ServiceFabric.AspNetCore.Kestrel` and `Microsoft.ServiceFabric.AspNetCore.HttpSys` packages.
334+
335+
```csharp
336+
// Stateless Service
337+
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
338+
{
339+
return new ServiceInstanceListener[]
340+
{
341+
new ServiceInstanceListener(serviceContext =>
342+
new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
343+
{
344+
return Host.CreateDefaultBuilder()
345+
.ConfigureWebHostDefaults(webBuilder =>
346+
{
347+
webBuilder.UseKestrel()
348+
.UseStartup<Startup>()
349+
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
350+
.UseContentRoot(Directory.GetCurrentDirectory())
351+
.UseUrls(url);
352+
})
353+
.ConfigureServices(services => services.AddSingleton<StatelessServiceContext>(serviceContext))
354+
.Build();
355+
}))
356+
};
357+
}
358+
359+
```
360+
361+
```csharp
362+
// Stateful Service
363+
protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
364+
{
365+
return new ServiceReplicaListener[]
366+
{
367+
new ServiceReplicaListener(serviceContext =>
368+
new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
369+
{
370+
return Host.CreateDefaultBuilder()
371+
.ConfigureWebHostDefaults(webBuilder =>
372+
{
373+
webBuilder.UseKestrel()
374+
.UseStartup<Startup>()
375+
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.UseUniqueServiceUrl)
376+
.UseContentRoot(Directory.GetCurrentDirectory())
377+
.UseUrls(url);
378+
})
379+
.ConfigureServices(services =>
380+
{
381+
services.AddSingleton<StatefulServiceContext>(serviceContext);
382+
services.AddSingleton<IReliableStateManager>(this.StateManager);
383+
})
384+
.Build();
385+
}))
386+
};
387+
}
388+
```
389+
390+
391+
>[!NOTE]
392+
> As KestrelCommunicationListener and HttpSysCommunicationListener are meant for web services, it is required to register/configure a web server (using [ConfigureWebHostDefaults](/dotnet/api/microsoft.extensions.hosting.generichostbuilderextensions.configurewebhostdefaults) or [ConfigureWebHost](/dotnet/api/microsoft.extensions.hosting.generichostwebhostbuilderextensions.configurewebhost) method) over the IHost
393+
394+
395+
ASP.NET 6 introduced the Minimal Hosting model which is a more simplified and streamlined way of creating web applications. Minimal hosting model can also be used with KestrelCommunicationListener and HttpSysCommunicationListener.
396+
397+
```csharp
398+
// Stateless Service
399+
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
400+
{
401+
return new ServiceInstanceListener[]
402+
{
403+
new ServiceInstanceListener(serviceContext =>
404+
new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
405+
{
406+
var builder = WebApplication.CreateBuilder();
407+
408+
builder.Services.AddSingleton<StatelessServiceContext>(serviceContext);
409+
builder.WebHost
410+
.UseKestrel()
411+
.UseContentRoot(Directory.GetCurrentDirectory())
412+
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
413+
.UseUrls(url);
414+
415+
builder.Services.AddControllersWithViews();
416+
417+
var app = builder.Build();
418+
419+
if (!app.Environment.IsDevelopment())
420+
{
421+
app.UseExceptionHandler("/Home/Error");
422+
}
423+
424+
app.UseHttpsRedirection();
425+
app.UseStaticFiles();
426+
app.UseRouting();
427+
app.UseAuthorization();
428+
app.MapControllerRoute(
429+
name: "default",
430+
pattern: "{controller=Home}/{action=Index}/{id?}");
431+
432+
return app;
433+
}))
434+
};
435+
}
436+
```
437+
438+
```csharp
439+
// Stateful Service
440+
protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
441+
{
442+
return new ServiceReplicaListener[]
443+
{
444+
new ServiceReplicaListener(serviceContext =>
445+
new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
446+
{
447+
var builder = WebApplication.CreateBuilder();
448+
449+
builder.Services
450+
.AddSingleton<StatefulServiceContext>(serviceContext)
451+
.AddSingleton<IReliableStateManager>(this.StateManager);
452+
builder.WebHost
453+
.UseKestrel()
454+
.UseContentRoot(Directory.GetCurrentDirectory())
455+
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.UseUniqueServiceUrl)
456+
.UseUrls(url);
457+
458+
builder.Services.AddControllersWithViews();
459+
460+
var app = builder.Build();
461+
462+
if (!app.Environment.IsDevelopment())
463+
{
464+
app.UseExceptionHandler("/Home/Error");
465+
}
466+
app.UseStaticFiles();
467+
app.UseRouting();
468+
app.UseAuthorization();
469+
app.MapControllerRoute(
470+
name: "default",
471+
pattern: "{controller=Home}/{action=Index}/{id?}");
472+
473+
return app;
474+
}))
475+
};
476+
}
477+
```
478+
479+
331480
## Service Fabric configuration provider
332481
App configuration in ASP.NET Core is based on key-value pairs established by the configuration provider. Read [Configuration in ASP.NET Core](/aspnet/core/fundamentals/configuration/) to understand more on general ASP.NET Core configuration support.
333482

0 commit comments

Comments
 (0)