SoapMessageEncoderDefaults.cs
1.91 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Text;
namespace SoapCore.MessageEncoder
{
internal class SoapMessageEncoderDefaults
{
public static readonly Encoding[] SupportedEncodings = { Encoding.UTF8, Encoding.Unicode, Encoding.BigEndianUnicode };
// Desktop: System.ServiceModel.Configuration.ConfigurationStrings.Soap12WSAddressing10;
public static readonly CharSetEncoding[] CharSetEncodings =
{
new CharSetEncoding("utf-8", Encoding.UTF8),
new CharSetEncoding("utf-16LE", Encoding.Unicode),
new CharSetEncoding("utf-16BE", Encoding.BigEndianUnicode),
new CharSetEncoding("utf-16", null), // Ignore. Ambiguous charSet, so autodetect.
new CharSetEncoding(null, null), // CharSet omitted, so autodetect.
};
public static void ValidateEncoding(Encoding encoding)
{
string charSet = encoding.WebName;
Encoding[] supportedEncodings = SupportedEncodings;
for (int i = 0; i < supportedEncodings.Length; i++)
{
if (charSet == supportedEncodings[i].WebName)
{
return;
}
}
throw new ArgumentException($"The text encoding '{charSet}' used in the text message format is not supported.", nameof(encoding));
}
public static string EncodingToCharSet(Encoding encoding)
{
string webName = encoding.WebName;
foreach (var charSetEncoding in CharSetEncodings)
{
if (charSetEncoding.Encoding == null)
{
continue;
}
if (charSetEncoding.Encoding.WebName == webName)
{
return charSetEncoding.CharSet;
}
}
return null;
}
public class CharSetEncoding
{
public CharSetEncoding(string charSet, Encoding enc)
{
CharSet = charSet;
Encoding = enc;
}
public string CharSet { get; }
public Encoding Encoding { get; }
}
}
}