OperationDescription.cs
5.25 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace SoapCore.ServiceModel
{
public class OperationDescription
{
public OperationDescription(ContractDescription contract, MethodInfo operationMethod, OperationContractAttribute contractAttribute)
{
Contract = contract;
Name = contractAttribute.Name ?? GetNameByAction(contractAttribute.Action) ?? GetNameByMethod(operationMethod);
SoapAction = contractAttribute.Action ?? $"{contract.Namespace.TrimEnd('/')}/{contract.Name}/{Name}";
IsOneWay = contractAttribute.IsOneWay;
DispatchMethod = operationMethod;
var returnType = operationMethod.ReturnType;
if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>))
{
returnType = returnType.GenericTypeArguments[0];
}
IsMessageContractResponse = returnType.CustomAttributes
.FirstOrDefault(ca => ca.AttributeType == typeof(MessageContractAttribute)) != null;
AllParameters = operationMethod.GetParameters()
.Select((info, index) => CreateParameterInfo(info, index, contract))
.ToArray();
InParameters = AllParameters
.Where(soapParam => soapParam.Direction != SoapMethodParameterDirection.OutOnlyRef)
.ToArray();
OutParameters = AllParameters
.Where(soapParam => soapParam.Direction != SoapMethodParameterDirection.InOnly)
.ToArray();
IsMessageContractRequest =
InParameters.Length == 1
&& InParameters.First().Parameter.ParameterType
.CustomAttributes
.FirstOrDefault(ca =>
ca.AttributeType == typeof(MessageContractAttribute)) != null;
var elementAttribute = operationMethod.ReturnParameter.GetCustomAttribute<XmlElementAttribute>();
ReturnName = operationMethod.ReturnParameter.GetCustomAttribute<MessageParameterAttribute>()?.Name ?? Name + "Result";
ReturnElementName = elementAttribute?.ElementName;
ReturnNamespace = elementAttribute?.Form == XmlSchemaForm.Unqualified ? string.Empty : elementAttribute?.Namespace;
ReplyAction = contractAttribute.ReplyAction ?? $"{Contract.Namespace.TrimEnd('/')}/{contract.Name}/{Name + "Response"}";
var faultContractAttributes = operationMethod.GetCustomAttributes<FaultContractAttribute>();
Faults = faultContractAttributes
.Where(a => a.DetailType?.Name != null)
.Select(a => a.DetailType)
.ToArray();
}
public ContractDescription Contract { get; private set; }
public string SoapAction { get; private set; }
public string ReplyAction { get; private set; }
public string Name { get; private set; }
public MethodInfo DispatchMethod { get; private set; }
public bool IsOneWay { get; private set; }
public bool IsMessageContractResponse { get; private set; }
public bool IsMessageContractRequest { get; private set; }
public SoapMethodParameterInfo[] AllParameters { get; private set; }
public SoapMethodParameterInfo[] InParameters { get; private set; }
public SoapMethodParameterInfo[] OutParameters { get; private set; }
public System.Type[] Faults { get; private set; }
public string ReturnName { get; private set; }
public string ReturnElementName { get; private set; }
public string ReturnNamespace { get; private set; }
private static SoapMethodParameterInfo CreateParameterInfo(ParameterInfo info, int index, ContractDescription contract)
{
var elementAttribute = info.GetCustomAttribute<XmlElementAttribute>();
var arrayAttribute = info.GetCustomAttribute<XmlArrayAttribute>();
var arrayItemAttribute = info.GetCustomAttribute<XmlArrayItemAttribute>();
var parameterName = elementAttribute?.ElementName
?? arrayAttribute?.ElementName
?? info.GetCustomAttribute<MessageParameterAttribute>()?.Name
?? info.ParameterType.GetCustomAttribute<MessageContractAttribute>()?.WrapperName
?? info.Name;
var arrayName = arrayAttribute?.ElementName;
var arrayItemName = arrayItemAttribute?.ElementName;
var parameterNs = elementAttribute?.Form == XmlSchemaForm.Unqualified
? string.Empty
: elementAttribute?.Namespace
?? arrayAttribute?.Namespace
?? contract.Namespace;
var dataContractAttribute = info.ParameterType.GetCustomAttribute<DataContractAttribute>();
if (dataContractAttribute != null && dataContractAttribute.IsNamespaceSetExplicitly && !string.IsNullOrWhiteSpace(dataContractAttribute.Namespace))
{
parameterNs = dataContractAttribute.Namespace;
}
return new SoapMethodParameterInfo(info, index, parameterName, arrayName, arrayItemName, parameterNs);
}
private static string GetNameByAction(string action)
{
var index = action?.LastIndexOf("/");
return (index ?? -1) > -1
? action.Substring(index.Value + 1, action.Length - index.Value - 1)
: null;
}
private static string GetNameByMethod(MethodInfo operationMethod)
{
var returnType = operationMethod.ReturnType;
var name = operationMethod.Name;
if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>))
{
if (name.EndsWith("Async"))
{
name = name.Substring(0, name.LastIndexOf("Async"));
}
}
if (returnType == typeof(Task) && name.EndsWith("Async"))
{
name = name.Substring(0, name.LastIndexOf("Async"));
}
return name;
}
}
}