-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDI-Example.linq
113 lines (92 loc) · 3.91 KB
/
DI-Example.linq
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<Query Kind="Program">
<NuGetReference>Autofac</NuGetReference>
<NuGetReference>Autofac.Extensions.DependencyInjection</NuGetReference>
<NuGetReference Version="2.0.0-alpha0002" Prerelease="true">Gotenberg.Sharp.API.Client</NuGetReference>
<NuGetReference>Microsoft.Extensions.Configuration</NuGetReference>
<NuGetReference>Microsoft.Extensions.Configuration.Json</NuGetReference>
<NuGetReference>Microsoft.Extensions.DependencyInjection</NuGetReference>
<NuGetReference>Microsoft.Extensions.Logging</NuGetReference>
<NuGetReference>Microsoft.Extensions.Logging.Console</NuGetReference>
<NuGetReference>Microsoft.Extensions.Options</NuGetReference>
<NuGetReference>Microsoft.Extensions.Options.ConfigurationExtensions</NuGetReference>
<Namespace>Autofac</Namespace>
<Namespace>Gotenberg.Sharp.API.Client</Namespace>
<Namespace>Gotenberg.Sharp.API.Client.Domain.Builders</Namespace>
<Namespace>Gotenberg.Sharp.API.Client.Domain.Builders.Faceted</Namespace>
<Namespace>Gotenberg.Sharp.API.Client.Domain.Requests</Namespace>
<Namespace>Gotenberg.Sharp.API.Client.Domain.Settings</Namespace>
<Namespace>Gotenberg.Sharp.API.Client.Extensions</Namespace>
<Namespace>Microsoft.Extensions.Configuration</Namespace>
<Namespace>Microsoft.Extensions.DependencyInjection</Namespace>
<Namespace>Microsoft.Extensions.DependencyInjection.Extensions</Namespace>
<Namespace>Microsoft.Extensions.Logging</Namespace>
<Namespace>Microsoft.Extensions.Logging.Console</Namespace>
<Namespace>Microsoft.Extensions.Options</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
//Builds a simple DI container with logging enabled.
//Client retreived through the SP is configured with options defined in local appsettings.json
//Watch the polly-retry policy in action:
// Turn off gotenberg, run this script and let it fail/retry two or three times.
// Turn gotenberg back on & the request will successfully complete.
//Example builds a 1 page PDF from the specified TargetUrl
const string TargetUrl = "https://www.cnn.com";
const string SaveToPath = @"D:\Gotenberg\Dumps";
static Random Rand = new Random(Math.Abs( (int) DateTime.Now.Ticks));
async Task Main()
{
using (var scope = new ContainerBuilder().Build().BeginLifetimeScope())
{
var services = BuildServiceCollection();
var sp = services.BuildServiceProvider();
var sharpClient = sp.GetRequiredService<GotenbergSharpClient>();
var request = await CreateUrlRequest();
var response = await sharpClient.UrlToPdfAsync(request);
var resultPath = @$"{SaveToPath}\GotenbergFromUrl-{Rand.Next()}.pdf";
using (var destinationStream = File.Create(resultPath))
{
await response.CopyToAsync(destinationStream);
}
var info = new ProcessStartInfo { FileName = resultPath, UseShellExecute = true };
Process.Start(info);
resultPath.Dump("Done");
//var ops = sp.GetRequiredService<IOptions<GotenbergSharpClientOptions>>();
//ops.Dump();
}
}
#region configuration & service collection Setup
IServiceCollection BuildServiceCollection()
{
var config = new ConfigurationBuilder()
.SetBasePath(@$"{Path.GetDirectoryName(Util.CurrentQueryPath)}\Resources\Settings")
.AddJsonFile("appsettings.json")
.Build();
return new ServiceCollection()
.AddOptions<GotenbergSharpClientOptions>()
.Bind(config.GetSection(nameof(GotenbergSharpClient))).Services
.AddGotenbergSharpClient()
.Services.AddLogging(s => s.AddSimpleConsole(ops => {
ops.IncludeScopes = true;
ops.SingleLine = false;
ops.TimestampFormat = "hh:mm:ss ";
}));
}
#endregion
#region gotenberg request creation
Task<UrlRequest> CreateUrlRequest()
{
var builder = new UrlRequestBuilder()
.SetUrl(TargetUrl)
.SetConversionBehaviors(b =>
{
b.SetUserAgent(nameof(GotenbergSharpClient));
})
.ConfigureRequest(b => b.SetPageRanges("1-2"))
.WithDimensions(b =>
{
b.SetPaperSize(PaperSizes.A4)
.SetMargins(Margins.None);
});
return builder.BuildAsync();
}
#endregion