Skip to content

Commit 0953b5c

Browse files
Henrik Otte SørensenHenrik Otte Sørensen
Henrik Otte Sørensen
authored and
Henrik Otte Sørensen
committed
fix: Be really specific and culture agnostic about how we serialise dates for soap messages.
1 parent dd17223 commit 0953b5c

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

src/STIL.ServiceClient/DTOs/SPSA/GetOrdrer/GetOrdrerQuery.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Globalization;
34
using System.Xml.Serialization;
5+
using STIL.ServiceClient.Util.SoapHelper;
46

57
namespace STIL.ServiceClient.DTOs.SPSA.GetOrdrer;
68

@@ -15,13 +17,13 @@ public GetOrdrerQuery()
1517

1618
public GetOrdrerQuery(DateTime? from = null, DateTime? to = null)
1719
{
18-
fromTime = from?.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
19-
toTime = to?.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
20+
fromTime = from?.ToSoapString();
21+
toTime = to?.ToSoapString();
2022
}
2123

2224
[XmlElement(IsNullable = true, Order = 0)]
23-
public string fromTime { get; set; }
25+
public string? fromTime { get; set; }
2426

2527
[XmlElement(IsNullable = true, Order = 1)]
26-
public string toTime { get; set; }
28+
public string? toTime { get; set; }
2729
}

src/STIL.ServiceClient/STIL.ServiceClient.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
<PropertyGroup Label="Nuget information">
14-
<Version>1.1.0</Version>
14+
<Version>1.1.1</Version>
1515
<Authors>PensionDanmark</Authors>
1616
<Owners>PensionDanmark</Owners>
1717
<Copyright>Copyright © PensionDanmark 2023</Copyright>

src/STIL.ServiceClient/Util/SoapHelper/SoapBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ private Guid AddBinarySecurityToken(XDocument document, X509Certificate2 signing
131131
private void AddTimeStamp(XDocument document)
132132
{
133133
XElement secElement = document.Descendants(securityWSNs + "Security").Single();
134-
string created = $"{DateTime.UtcNow:O}".Substring(0, 23) + "Z";
135-
string expires = $"{DateTime.UtcNow.AddMinutes(5):O}".Substring(0, 23) + "Z";
134+
string created = DateTime.UtcNow.ToSoapString();
135+
string expires = DateTime.UtcNow.AddMinutes(5).ToSoapString();
136136
secElement.Add(new XElement(
137137
securityUtilNs + "Timestamp",
138138
new XAttribute(securityUtilNs + "Id", $"TimestampRef"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Globalization;
3+
4+
namespace STIL.ServiceClient.Util.SoapHelper;
5+
6+
/// <summary>
7+
/// Extension method to format DateTimes so that STIL's IPL Soap server will accept them.
8+
/// </summary>
9+
public static class SoapDateTimeFormatter
10+
{
11+
private const string _formatString = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'";
12+
13+
/// <summary>
14+
/// Formats DateTime as a ISO-8601 UTC string.
15+
/// </summary>
16+
/// <param name="dateTime">DateTime.</param>
17+
/// <returns>Soap friendly formatted DateTime string.</returns>
18+
public static string ToSoapString(this DateTime dateTime)
19+
{
20+
return dateTime.ToUniversalTime().ToString(_formatString, CultureInfo.InvariantCulture);
21+
}
22+
}

0 commit comments

Comments
 (0)