| Name | Message | Date |
|---|---|---|
| 📄 ReceiptPrinterService.cs | 12 hours ago |
📄
ReceiptPrinterService.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
using System; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using Grpc.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using ReceiptPrinter; using static ReceiptPrinter.ReceiptPrinter; namespace Receipt.Web.Services; public sealed partial class ReceiptPrinterService(ILogger<ReceiptPrinterService> logger, ReceiptPrinterClient client) { public async Task<string?> Print(string content) { LogPrintingContent(content); var request = new PrintRequest() { Typst = new() { Content = content } }; try { await client.PrintAsync(request); return null; } catch (RpcException ex) { LogPrintingException(ex); return ex.Status.Detail; } } [LoggerMessage(Message = "Printing content: {Content}", Level = LogLevel.Debug)] private partial void LogPrintingContent(string content); [LoggerMessage(Message = "Printer failed", Level = LogLevel.Warning)] private partial void LogPrintingException(Exception ex); } public sealed class ReceiptPrinterOptions { [Required] public required Uri Address { get; set; } } [OptionsValidator] public sealed partial class ReceiptPrinterOptionsValidator : IValidateOptions<ReceiptPrinterOptions>; public static class ReceiptPrinterServiceCollectionExtensions { public static IServiceCollection AddReceiptPrinterService(this IServiceCollection services) { services.AddOptions<ReceiptPrinterOptions>().BindConfiguration("ReceiptPrinter").ValidateOnStart(); services.AddTransient<IValidateOptions<ReceiptPrinterOptions>, ReceiptPrinterOptionsValidator>(); services.AddGrpcClient<ReceiptPrinterClient>( "ReceiptPrinter", (sp, options) => { options.Address = sp.GetRequiredService<IOptions<ReceiptPrinterOptions>>().Value.Address; } ); services.AddTransient<ReceiptPrinterService>(); return services; } }