-
Notifications
You must be signed in to change notification settings - Fork 0
/
FabricApi.cs
89 lines (80 loc) · 3.46 KB
/
FabricApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Fabric;
using System.Fabric.Management.ServiceModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.ServiceFabric.AspNetCore.Configuration;
using Microsoft.ServiceFabric.Services.Communication.AspNetCore;
using Microsoft.ServiceFabric.Services.Communication.Runtime;
using Microsoft.ServiceFabric.Services.Runtime;
using Microsoft.ServiceFabric.Data;
namespace FabricApi
{
/// <summary>
/// The FabricRuntime creates an instance of this class for each service type instance.
/// </summary>
internal sealed class FabricApi : StatelessService
{
public FabricApi(StatelessServiceContext context)
: base(context)
{ }
/// <summary>
/// Optional override to create listeners (like tcp, http) for this service instance.
/// </summary>
/// <returns>The collection of listeners.</returns>
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext =>
new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");
return WebHost.CreateDefaultBuilder(new string[0])
.UseKestrel()
.ConfigureAppConfiguration(builder => builder.AddServiceFabricConfiguration(FabricRuntime.GetActivationContext(), options => options.IncludePackageName = false))
//.ConfigureAppConfiguration(builder => builder.AddTestServiceFabricConfiguration())
//.UseConfiguration(new ConfigurationBuilder().AddServiceFabricConfiguration().Build())
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
}))
};
}
}
internal class ServiceFabricConfigurationProvider : ConfigurationProvider
{
public override void Load()
{
Data["Logging:LogLevel:Default"] = "Information";
Data["Logging:Debug:LogLevel:Default"] = "Information";
}
}
internal class ServiceFabricConfigurationSource : IConfigurationSource
{
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new ServiceFabricConfigurationProvider();
}
}
public static class ServiceFabricConfigurationExtensions
{
public static IConfigurationBuilder AddTestServiceFabricConfiguration(this IConfigurationBuilder builder)
{
builder.Add(new ServiceFabricConfigurationSource());
return builder;
}
}
}