1: using System;
2: using System.Collections.Generic;
3: using System.Text.Json;
4:
5: namespace Ia.Cl.Model.Db
6: {
7: ////////////////////////////////////////////////////////////////////////////
8:
9: /// <summary publish="true">
10: /// MSMQ Database support class. This handles storing and retrieving MSMQ storage.
11: /// </summary>
12: /// <remarks>
13: /// Copyright © 2020-2021 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
14: ///
15: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
16: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
17: ///
18: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20: ///
21: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
22: ///
23: /// Copyright notice: This notice may not be removed or altered from any source distribution.
24: /// </remarks>
25:
26: public class Msmq
27: {
28: static readonly string storeName = "msmq/data/default/store/";
29:
30: ////////////////////////////////////////////////////////////////////////////
31:
32: /// <summary>
33: ///
34: /// </summary>
35: private static T Deserialize<T>(string json)
36: {
37: T t;
38:
39: if (!string.IsNullOrEmpty(json))
40: {
41: t = JsonSerializer.Deserialize<T>(json);
42: }
43: else t = default(T);
44:
45: return t;
46: }
47:
48: ////////////////////////////////////////////////////////////////////////////
49:
50: /// <summary>
51: ///
52: /// </summary>
53: public static void Write<T>(string name, T t)
54: {
55: string label, body;
56:
57: var jsonSerializerOptions = new JsonSerializerOptions
58: {
59: WriteIndented = true
60: };
61:
62: var json = JsonSerializer.Serialize<T>(t, jsonSerializerOptions);
63:
64: // clear the existing message
65:
66: do
67: {
68: Ia.Cl.Model.Msmq.RecievePrivate(storeName + name, out label, out body);
69: }
70: while (!string.IsNullOrEmpty(label) || !string.IsNullOrEmpty(body));
71:
72: Ia.Cl.Model.Msmq.SendPrivate(storeName + name, storeName, json);
73: }
74:
75: ////////////////////////////////////////////////////////////////////////////
76:
77: /// <summary>
78: ///
79: /// </summary>
80: public static T Read<T>(string name)
81: {
82: Ia.Cl.Model.Msmq.PeekPrivate(storeName + name, out string label, out string json);
83:
84: var t = Deserialize<T>(json);
85:
86: return t;
87: }
88:
89: ////////////////////////////////////////////////////////////////////////////
90: ////////////////////////////////////////////////////////////////////////////
91: }
92:
93: ////////////////////////////////////////////////////////////////////////////
94: ////////////////////////////////////////////////////////////////////////////
95: }
96: