| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using CoreWCF.Channels; |
| | | 6 | | using CoreWCF.Dispatcher; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Description |
| | | 10 | | { |
| | | 11 | | public abstract class TypedMessageConverter |
| | | 12 | | { |
| | | 13 | | public static TypedMessageConverter Create(Type messageContract, string action) |
| | | 14 | | { |
| | | 15 | | return Create(messageContract, action, null, TypeLoader.DefaultDataContractFormatAttribute); |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public static TypedMessageConverter Create(Type messageContract, string action, string defaultNamespace) |
| | | 19 | | { |
| | | 20 | | return Create(messageContract, action, defaultNamespace, TypeLoader.DefaultDataContractFormatAttribute); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public static TypedMessageConverter Create(Type messageContract, string action, XmlSerializerFormatAttribute for |
| | | 24 | | { |
| | | 25 | | return Create(messageContract, action, null, formatterAttribute); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public static TypedMessageConverter Create(Type messageContract, string action, DataContractFormatAttribute form |
| | | 29 | | { |
| | | 30 | | return Create(messageContract, action, null, formatterAttribute); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public static TypedMessageConverter Create(Type messageContract, string action, string defaultNamespace, XmlSeri |
| | | 34 | | { |
| | | 35 | | if (messageContract == null) |
| | | 36 | | { |
| | | 37 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(messageContra |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | if (defaultNamespace == null) |
| | | 41 | | { |
| | | 42 | | defaultNamespace = NamingHelper.DefaultNamespace; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | return new XmlMessageConverter(GetOperationFormatter(messageContract, formatterAttribute, defaultNamespace, |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public static TypedMessageConverter Create(Type messageContract, string action, string defaultNamespace, DataCon |
| | | 49 | | { |
| | | 50 | | if (messageContract == null) |
| | | 51 | | { |
| | | 52 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(messageContra |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | if (!messageContract.IsDefined(typeof(MessageContractAttribute), false)) |
| | | 56 | | { |
| | | 57 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.SFxMessageC |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | if (defaultNamespace == null) |
| | | 61 | | { |
| | | 62 | | defaultNamespace = NamingHelper.DefaultNamespace; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | return new XmlMessageConverter(GetOperationFormatter(messageContract, formatterAttribute, defaultNamespace, |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public abstract Message ToMessage(object typedMessage); |
| | | 69 | | public abstract Message ToMessage(object typedMessage, MessageVersion version); |
| | | 70 | | public abstract object FromMessage(Message message); |
| | | 71 | | |
| | | 72 | | private static OperationFormatter GetOperationFormatter(Type t, Attribute formatAttribute, string defaultNS, str |
| | | 73 | | { |
| | | 74 | | bool isXmlSerializer = (formatAttribute is XmlSerializerFormatAttribute); |
| | | 75 | | TypeLoader<object> typeLoader = new TypeLoader<object>(); |
| | | 76 | | MessageDescription message = typeLoader.CreateTypedMessageDescription(t, null, null, defaultNS, action, Mess |
| | | 77 | | ContractDescription contract = new ContractDescription("dummy_contract", defaultNS); |
| | | 78 | | OperationDescription operation = new OperationDescription(NamingHelper.XmlName(t.Name), contract, false); |
| | | 79 | | operation.Messages.Add(message); |
| | | 80 | | |
| | | 81 | | if (isXmlSerializer) |
| | | 82 | | { |
| | | 83 | | return XmlSerializerOperationBehavior.CreateOperationFormatter(operation, (XmlSerializerFormatAttribute) |
| | | 84 | | } |
| | | 85 | | else |
| | | 86 | | { |
| | | 87 | | return new DataContractSerializerOperationFormatter(operation, (DataContractFormatAttribute)formatAttrib |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | internal class XmlMessageConverter : TypedMessageConverter |
| | | 93 | | { |
| | | 94 | | private OperationFormatter _formatter; |
| | | 95 | | |
| | 0 | 96 | | internal XmlMessageConverter(OperationFormatter formatter) |
| | | 97 | | { |
| | 0 | 98 | | _formatter = formatter; |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | internal string Action => _formatter.RequestAction; |
| | | 102 | | |
| | | 103 | | public override Message ToMessage(object typedMessage) |
| | | 104 | | { |
| | 0 | 105 | | return ToMessage(typedMessage, MessageVersion.Soap12WSAddressing10); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | public override Message ToMessage(object typedMessage, MessageVersion version) |
| | | 109 | | { |
| | 0 | 110 | | if (typedMessage == null) |
| | | 111 | | { |
| | 0 | 112 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(typedMessage) |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | return _formatter.SerializeRequest(version, new object[] { typedMessage }); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | public override object FromMessage(Message message) |
| | | 119 | | { |
| | | 120 | | Fx.Assert(message.Headers != null, ""); |
| | 0 | 121 | | if (Action != null && message.Headers.Action != null && message.Headers.Action != Action) |
| | | 122 | | { |
| | 0 | 123 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | object[] result = new object[1]; |
| | 0 | 127 | | _formatter.DeserializeRequest(message, result); |
| | | 128 | | |
| | 0 | 129 | | return result[0]; |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | } |