| | | 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 System.Collections.Generic; |
| | | 6 | | using System.Runtime.Serialization; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Dispatcher; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Description |
| | | 12 | | { |
| | | 13 | | public class DataContractSerializerOperationBehavior : IOperationBehavior, IWsdlExportExtension |
| | | 14 | | { |
| | | 15 | | private readonly OperationDescription _operation; |
| | | 16 | | internal bool ignoreExtensionDataObject = DataContractSerializerDefaults.IgnoreExtensionDataObject; |
| | | 17 | | internal int maxItemsInObjectGraph = DataContractSerializerDefaults.MaxItemsInObjectGraph; |
| | 3074 | 18 | | |
| | | 19 | | public DataContractFormatAttribute DataContractFormatAttribute { get; } |
| | 3269 | 20 | | |
| | | 21 | | public DataContractSerializerOperationBehavior(OperationDescription operation) |
| | | 22 | | : this(operation, null) |
| | 441 | 23 | | { |
| | | 24 | | } |
| | 441 | 25 | | |
| | | 26 | | public DataContractSerializerOperationBehavior(OperationDescription operation, DataContractFormatAttribute dataC |
| | 3074 | 27 | | { |
| | | 28 | | DataContractFormatAttribute = dataContractFormatAttribute ?? new DataContractFormatAttribute(); |
| | 3074 | 29 | | _operation = operation; |
| | 3074 | 30 | | } |
| | 3074 | 31 | | |
| | | 32 | | public DataContractSerializerOperationBehavior(OperationDescription operation, |
| | | 33 | | DataContractFormatAttribute dataContractFormatAttribute, bool builtInOperationBehavior) |
| | | 34 | | : this(operation, dataContractFormatAttribute) |
| | 2633 | 35 | | { |
| | | 36 | | IsBuiltInOperationBehavior = builtInOperationBehavior; |
| | 2633 | 37 | | } |
| | 2633 | 38 | | |
| | | 39 | | internal bool IsBuiltInOperationBehavior { get; } |
| | 0 | 40 | | |
| | | 41 | | public int MaxItemsInObjectGraph |
| | | 42 | | { |
| | | 43 | | get { return maxItemsInObjectGraph; } |
| | 804 | 44 | | set |
| | | 45 | | { |
| | | 46 | | maxItemsInObjectGraph = value; |
| | 0 | 47 | | MaxItemsInObjectGraphSetExplicit = true; |
| | 0 | 48 | | } |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | internal bool MaxItemsInObjectGraphSetExplicit { get; set; } |
| | 2978 | 52 | | |
| | | 53 | | public bool IgnoreExtensionDataObject |
| | | 54 | | { |
| | | 55 | | get { return ignoreExtensionDataObject; } |
| | 272 | 56 | | set |
| | | 57 | | { |
| | | 58 | | ignoreExtensionDataObject = value; |
| | 0 | 59 | | IgnoreExtensionDataObjectSetExplicit = true; |
| | 0 | 60 | | } |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | internal bool IgnoreExtensionDataObjectSetExplicit { get; set; } |
| | 2978 | 64 | | |
| | | 65 | | public ISerializationSurrogateProvider SerializationSurrogateProvider { get; set; } |
| | 535 | 66 | | |
| | | 67 | | public DataContractResolver DataContractResolver { get; set; } |
| | 534 | 68 | | |
| | | 69 | | public virtual XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes) |
| | | 70 | | { |
| | | 71 | | XmlDictionary dictionary = new XmlDictionary(2); |
| | 9 | 72 | | DataContractSerializerSettings settings = new DataContractSerializerSettings(); |
| | 9 | 73 | | settings.RootName = dictionary.Add(name); |
| | 9 | 74 | | settings.RootNamespace = dictionary.Add(ns); |
| | 9 | 75 | | settings.KnownTypes = knownTypes; |
| | 9 | 76 | | settings.MaxItemsInObjectGraph = MaxItemsInObjectGraph; |
| | 9 | 77 | | settings.DataContractResolver = DataContractResolver; |
| | 9 | 78 | | DataContractSerializer dcs = new DataContractSerializer(type, settings); |
| | 9 | 79 | | if (SerializationSurrogateProvider != null) |
| | 9 | 80 | | { |
| | | 81 | | dcs.SetSerializationSurrogateProvider(SerializationSurrogateProvider); |
| | 1 | 82 | | } |
| | | 83 | | return dcs; |
| | 9 | 84 | | } |
| | | 85 | | |
| | | 86 | | public virtual XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, |
| | | 87 | | { |
| | | 88 | | DataContractSerializerSettings settings = new DataContractSerializerSettings(); |
| | 523 | 89 | | settings.RootName = name; |
| | 523 | 90 | | settings.RootNamespace = ns; |
| | 523 | 91 | | settings.KnownTypes = knownTypes; |
| | 523 | 92 | | settings.MaxItemsInObjectGraph = MaxItemsInObjectGraph; |
| | 523 | 93 | | settings.DataContractResolver = DataContractResolver; |
| | 523 | 94 | | DataContractSerializer dcs = new DataContractSerializer(type, settings); |
| | 523 | 95 | | if (SerializationSurrogateProvider != null) |
| | 523 | 96 | | { |
| | | 97 | | dcs.SetSerializationSurrogateProvider(SerializationSurrogateProvider); |
| | 0 | 98 | | } |
| | | 99 | | return dcs; |
| | 523 | 100 | | } |
| | | 101 | | |
| | | 102 | | internal object GetFormatter(OperationDescription operation, out bool formatRequest, out bool formatReply, bool |
| | | 103 | | { |
| | | 104 | | MessageDescription request = operation.Messages[0]; |
| | | 105 | | MessageDescription response = null; |
| | | 106 | | if (operation.Messages.Count == 2) |
| | | 107 | | { |
| | | 108 | | response = operation.Messages[1]; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | formatRequest = (request != null) && !request.IsUntypedMessage; |
| | | 112 | | formatReply = (response != null) && !response.IsUntypedMessage; |
| | | 113 | | |
| | | 114 | | if (formatRequest || formatReply) |
| | | 115 | | { |
| | 1 | 116 | | if (PrimitiveOperationFormatter.IsContractSupported(operation)) |
| | | 117 | | { |
| | | 118 | | return new PrimitiveOperationFormatter(operation, DataContractFormatAttribute.Style == OperationForm |
| | | 119 | | } |
| | | 120 | | else |
| | 2815 | 121 | | { |
| | 2815 | 122 | | return new DataContractSerializerOperationFormatter(operation, DataContractFormatAttribute, this); |
| | 2815 | 123 | | } |
| | | 124 | | } |
| | 2491 | 125 | | |
| | | 126 | | return null; |
| | | 127 | | } |
| | 2815 | 128 | | |
| | 2815 | 129 | | |
| | | 130 | | void IOperationBehavior.Validate(OperationDescription description) |
| | 2815 | 131 | | { |
| | | 132 | | } |
| | 2789 | 133 | | |
| | | 134 | | void IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parame |
| | 1972 | 135 | | { |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | void IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch) |
| | 817 | 139 | | { |
| | | 140 | | if (description == null) |
| | | 141 | | { |
| | | 142 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(description)); |
| | 26 | 143 | | } |
| | | 144 | | |
| | | 145 | | if (dispatch == null) |
| | | 146 | | { |
| | | 147 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dispatch)); |
| | | 148 | | } |
| | 2595 | 149 | | |
| | | 150 | | if (dispatch.Formatter != null) |
| | | 151 | | { |
| | | 152 | | return; |
| | 3128 | 153 | | } |
| | | 154 | | |
| | | 155 | | dispatch.Formatter = (IDispatchMessageFormatter)GetFormatter(description, out bool formatRequest, out bool f |
| | | 156 | | dispatch.DeserializeRequest = formatRequest; |
| | 2976 | 157 | | dispatch.SerializeReply = formatReply; |
| | | 158 | | } |
| | 0 | 159 | | |
| | | 160 | | void IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy) |
| | | 161 | | { |
| | 2976 | 162 | | if (description == null) |
| | | 163 | | { |
| | 0 | 164 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(description)); |
| | | 165 | | } |
| | | 166 | | |
| | 2976 | 167 | | if (proxy == null) |
| | | 168 | | { |
| | 163 | 169 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(proxy)); |
| | | 170 | | } |
| | | 171 | | |
| | 2813 | 172 | | if (proxy.Formatter != null) |
| | 2813 | 173 | | { |
| | 2813 | 174 | | return; |
| | 2813 | 175 | | } |
| | | 176 | | |
| | | 177 | | proxy.Formatter = (IClientMessageFormatter)GetFormatter(description, out bool formatRequest, out bool format |
| | | 178 | | proxy.SerializeRequest = formatRequest; |
| | 2 | 179 | | proxy.DeserializeReply = formatReply; |
| | | 180 | | } |
| | 0 | 181 | | |
| | | 182 | | void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext endpointContext) |
| | | 183 | | { |
| | 2 | 184 | | if (exporter == null) |
| | | 185 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(exporter)); |
| | 0 | 186 | | if (endpointContext == null) |
| | | 187 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpointContext)); |
| | | 188 | | |
| | 2 | 189 | | MessageContractExporter.ExportMessageBinding(exporter, endpointContext, typeof(DataContractSerializerMessage |
| | | 190 | | } |
| | 0 | 191 | | |
| | | 192 | | void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext) |
| | | 193 | | { |
| | 2 | 194 | | if (exporter == null) |
| | 2 | 195 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(exporter)); |
| | 2 | 196 | | if (contractContext == null) |
| | 2 | 197 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contractContext)); |
| | | 198 | | |
| | | 199 | | new DataContractSerializerMessageContractExporter(exporter, contractContext, _operation, this).ExportMessage |
| | | 200 | | } |
| | 150 | 201 | | } |
| | 0 | 202 | | } |