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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Runtime;
using System.Security;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Xml;
using System.Xml.Serialization;
public class XmlSerializerOperationBehavior : IOperationBehavior, IWsdlExportExtension
{
readonly Reflector.OperationReflector reflector;
readonly bool builtInOperationBehavior;
public XmlSerializerOperationBehavior(OperationDescription operation)
: this(operation, null)
{
}
public XmlSerializerOperationBehavior(OperationDescription operation, XmlSerializerFormatAttribute attribute)
{
if (operation == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("operation");
#pragma warning suppress 56506 // Declaring contract cannot be null
Reflector parentReflector = new Reflector(operation.DeclaringContract.Namespace, operation.DeclaringContract.ContractType);
#pragma warning suppress 56506 // parentReflector cannot be null
this.reflector = parentReflector.ReflectOperation(operation, attribute ?? new XmlSerializerFormatAttribute());
}
internal XmlSerializerOperationBehavior(OperationDescription operation, XmlSerializerFormatAttribute attribute, Reflector parentReflector)
: this(operation, attribute)
{
// used by System.ServiceModel.Web
this.reflector = parentReflector.ReflectOperation(operation, attribute ?? new XmlSerializerFormatAttribute());
}
XmlSerializerOperationBehavior(Reflector.OperationReflector reflector, bool builtInOperationBehavior)
{
Fx.Assert(reflector != null, "");
this.reflector = reflector;
this.builtInOperationBehavior = builtInOperationBehavior;
}
internal Reflector.OperationReflector OperationReflector
{
get { return this.reflector; }
}
internal bool IsBuiltInOperationBehavior
{
get { return this.builtInOperationBehavior; }
}
public XmlSerializerFormatAttribute XmlSerializerFormatAttribute
{
get
{
return this.reflector.Attribute;
}
}
internal static XmlSerializerOperationFormatter CreateOperationFormatter(OperationDescription operation)
{
return new XmlSerializerOperationBehavior(operation).CreateFormatter();
}
internal static XmlSerializerOperationFormatter CreateOperationFormatter(OperationDescription operation, XmlSerializerFormatAttribute attr)
{
return new XmlSerializerOperationBehavior(operation, attr).CreateFormatter();
}
internal static void AddBehaviors(ContractDescription contract)
{
AddBehaviors(contract, false);
}
internal static void AddBuiltInBehaviors(ContractDescription contract)
{
AddBehaviors(contract, true);
}
static void AddBehaviors(ContractDescription contract, bool builtInOperationBehavior)
{
Reflector reflector = new Reflector(contract.Namespace, contract.ContractType);
foreach (OperationDescription operation in contract.Operations)
{
Reflector.OperationReflector operationReflector = reflector.ReflectOperation(operation);
if (operationReflector != null)
{
bool isInherited = operation.DeclaringContract != contract;
if (!isInherited)
{
operation.Behaviors.Add(new XmlSerializerOperationBehavior(operationReflector, builtInOperationBehavior));
operation.Behaviors.Add(new XmlSerializerOperationGenerator(new XmlSerializerImportOptions()));
}
}
}
}
internal XmlSerializerOperationFormatter CreateFormatter()
{
return new XmlSerializerOperationFormatter(reflector.Operation, reflector.Attribute, reflector.Request, reflector.Reply);
}
XmlSerializerFaultFormatter CreateFaultFormatter(SynchronizedCollection<FaultContractInfo> faultContractInfos)
{
return new XmlSerializerFaultFormatter(faultContractInfos, reflector.XmlSerializerFaultContractInfos);
}
void IOperationBehavior.Validate(OperationDescription description)
{
}
void IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
{
}
void IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
{
if (description == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
if (dispatch == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("dispatch");
if (dispatch.Formatter == null)
{
dispatch.Formatter = (IDispatchMessageFormatter)CreateFormatter();
dispatch.DeserializeRequest = reflector.RequestRequiresSerialization;
dispatch.SerializeReply = reflector.ReplyRequiresSerialization;
}
if (reflector.Attribute.SupportFaults)
{
if (!dispatch.IsFaultFormatterSetExplicit)
{
dispatch.FaultFormatter = (IDispatchFaultFormatter)CreateFaultFormatter(dispatch.FaultContractInfos);
}
else
{
var wrapper = dispatch.FaultFormatter as IDispatchFaultFormatterWrapper;
if (wrapper != null)
{
wrapper.InnerFaultFormatter = (IDispatchFaultFormatter)CreateFaultFormatter(dispatch.FaultContractInfos);
}
}
}
}
void IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
{
if (description == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
if (proxy == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("proxy");
if (proxy.Formatter == null)
{
proxy.Formatter = (IClientMessageFormatter)CreateFormatter();
proxy.SerializeRequest = reflector.RequestRequiresSerialization;
proxy.DeserializeReply = reflector.ReplyRequiresSerialization;
}
if (reflector.Attribute.SupportFaults && !proxy.IsFaultFormatterSetExplicit)
proxy.FaultFormatter = (IClientFaultFormatter)CreateFaultFormatter(proxy.FaultContractInfos);
}
void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext endpointContext)
{
if (exporter == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exporter");
if (endpointContext == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpointContext");
MessageContractExporter.ExportMessageBinding(exporter, endpointContext, typeof(XmlSerializerMessageContractExporter), this.reflector.Operation);
}
void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext)
{
if (exporter == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exporter");
if (contractContext == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contractContext");
new XmlSerializerMessageContractExporter(exporter, contractContext, this.reflector.Operation, this).ExportMessageContract();
}
public Collection<XmlMapping> GetXmlMappings()
{
Collection<XmlMapping> mappings = new Collection<XmlMapping>();
if (OperationReflector.Request != null && OperationReflector.Request.HeadersMapping != null)
mappings.Add(OperationReflector.Request.HeadersMapping);
if (OperationReflector.Request != null && OperationReflector.Request.BodyMapping != null)
mappings.Add(OperationReflector.Request.BodyMapping);
if (OperationReflector.Reply != null && OperationReflector.Reply.HeadersMapping != null)
mappings.Add(OperationReflector.Reply.HeadersMapping);
if (OperationReflector.Reply != null && OperationReflector.Reply.BodyMapping != null)
mappings.Add(OperationReflector.Reply.BodyMapping);
return mappings;
}
// helper for reflecting operations
internal class Reflector
{
readonly XmlSerializerImporter importer;
readonly SerializerGenerationContext generation;
Collection<OperationReflector> operationReflectors = new Collection<OperationReflector>();
object thisLock = new object();
internal Reflector(string defaultNs, Type type)
{
this.importer = new XmlSerializerImporter(defaultNs);
this.generation = new SerializerGenerationContext(type);
}
internal void EnsureMessageInfos()
{
lock (this.thisLock)
{
foreach (OperationReflector operationReflector in operationReflectors)
{
operationReflector.EnsureMessageInfos();
}
}
}
static XmlSerializerFormatAttribute FindAttribute(OperationDescription operation)
{
Type contractType = operation.DeclaringContract != null ? operation.DeclaringContract.ContractType : null;
XmlSerializerFormatAttribute contractFormatAttribute = contractType != null ? TypeLoader.GetFormattingAttribute(contractType, null) as XmlSerializerFormatAttribute : null;
return TypeLoader.GetFormattingAttribute(operation.OperationMethod, contractFormatAttribute) as XmlSerializerFormatAttribute;
}
// auto-reflects the operation, returning null if no attribute was found or inherited
internal OperationReflector ReflectOperation(OperationDescription operation)
{
XmlSerializerFormatAttribute attr = FindAttribute(operation);
if (attr == null)
return null;
return ReflectOperation(operation, attr);
}
// overrides the auto-reflection with an attribute
internal OperationReflector ReflectOperation(OperationDescription operation, XmlSerializerFormatAttribute attrOverride)
{
OperationReflector operationReflector = new OperationReflector(this, operation, attrOverride, true/*reflectOnDemand*/);
operationReflectors.Add(operationReflector);
return operationReflector;
}
internal class OperationReflector
{
readonly Reflector parent;
internal readonly OperationDescription Operation;
internal readonly XmlSerializerFormatAttribute Attribute;
internal readonly bool IsEncoded;
internal readonly bool IsRpc;
internal readonly bool IsOneWay;
internal readonly bool RequestRequiresSerialization;
internal readonly bool ReplyRequiresSerialization;
readonly string keyBase;
MessageInfo request;
MessageInfo reply;
SynchronizedCollection<XmlSerializerFaultContractInfo> xmlSerializerFaultContractInfos;
internal OperationReflector(Reflector parent, OperationDescription operation, XmlSerializerFormatAttribute attr, bool reflectOnDemand)
{
Fx.Assert(parent != null, "");
Fx.Assert(operation != null, "");
Fx.Assert(attr != null, "");
OperationFormatter.Validate(operation, attr.Style == OperationFormatStyle.Rpc, attr.IsEncoded);
this.parent = parent;
this.Operation = operation;
this.Attribute = attr;
this.IsEncoded = attr.IsEncoded;
this.IsRpc = (attr.Style == OperationFormatStyle.Rpc);
this.IsOneWay = operation.Messages.Count == 1;
this.RequestRequiresSerialization = !operation.Messages[0].IsUntypedMessage;
this.ReplyRequiresSerialization = !this.IsOneWay && !operation.Messages[1].IsUntypedMessage;
MethodInfo methodInfo = operation.OperationMethod;
if (methodInfo == null)
{
// keyBase needs to be unique within the scope of the parent reflector
keyBase = string.Empty;
if (operation.DeclaringContract != null)
{
keyBase = operation.DeclaringContract.Name + "," + operation.DeclaringContract.Namespace + ":";
}
keyBase = keyBase + operation.Name;
}
else
keyBase = methodInfo.DeclaringType.FullName + ":" + methodInfo.ToString();
foreach (MessageDescription message in operation.Messages)
foreach (MessageHeaderDescription header in message.Headers)
SetUnknownHeaderInDescription(header);
if (!reflectOnDemand)
{
this.EnsureMessageInfos();
}
}
private void SetUnknownHeaderInDescription(MessageHeaderDescription header)
{
if (this.IsEncoded) //XmlAnyElementAttribute does not apply
return;
if (header.AdditionalAttributesProvider != null)
{
XmlAttributes xmlAttributes = new XmlAttributes(header.AdditionalAttributesProvider);
foreach (XmlAnyElementAttribute anyElement in xmlAttributes.XmlAnyElements)
{
if (String.IsNullOrEmpty(anyElement.Name))
{
header.IsUnknownHeaderCollection = true;
}
}
}
}
string ContractName
{
get { return this.Operation.DeclaringContract.Name; }
}
string ContractNamespace
{
get { return this.Operation.DeclaringContract.Namespace; }
}
internal MessageInfo Request
{
get
{
parent.EnsureMessageInfos();
return this.request;
}
}
internal MessageInfo Reply
{
get
{
parent.EnsureMessageInfos();
return this.reply;
}
}
internal SynchronizedCollection<XmlSerializerFaultContractInfo> XmlSerializerFaultContractInfos
{
get
{
parent.EnsureMessageInfos();
return this.xmlSerializerFaultContractInfos;
}
}
internal void EnsureMessageInfos()
{
if (this.request == null)
{
foreach (Type knownType in Operation.KnownTypes)
{
if (knownType == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxKnownTypeNull, Operation.Name)));
parent.importer.IncludeType(knownType, IsEncoded);
}
this.request = CreateMessageInfo(this.Operation.Messages[0], ":Request");
if (this.request != null && this.IsRpc && this.Operation.IsValidateRpcWrapperName && this.request.BodyMapping.XsdElementName != this.Operation.Name)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxRpcMessageBodyPartNameInvalid, Operation.Name, this.Operation.Messages[0].MessageName, request.BodyMapping.XsdElementName, this.Operation.Name)));
if (!this.IsOneWay)
{
this.reply = CreateMessageInfo(this.Operation.Messages[1], ":Response");
XmlName responseName = TypeLoader.GetBodyWrapperResponseName(this.Operation.Name);
if (this.reply != null && this.IsRpc && this.Operation.IsValidateRpcWrapperName && this.reply.BodyMapping.XsdElementName != responseName.EncodedName)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxRpcMessageBodyPartNameInvalid, Operation.Name, this.Operation.Messages[1].MessageName, reply.BodyMapping.XsdElementName, responseName.EncodedName)));
}
if (this.Attribute.SupportFaults)
{
GenerateXmlSerializerFaultContractInfos();
}
}
}
void GenerateXmlSerializerFaultContractInfos()
{
SynchronizedCollection<XmlSerializerFaultContractInfo> faultInfos = new SynchronizedCollection<XmlSerializerFaultContractInfo>();
for (int i = 0; i < this.Operation.Faults.Count; i++)
{
FaultDescription fault = this.Operation.Faults[i];
FaultContractInfo faultContractInfo = new FaultContractInfo(fault.Action, fault.DetailType, fault.ElementName, fault.Namespace, this.Operation.KnownTypes);
XmlQualifiedName elementName;
XmlMembersMapping xmlMembersMapping = this.ImportFaultElement(fault, out elementName);
SerializerStub serializerStub = parent.generation.AddSerializer(xmlMembersMapping);
faultInfos.Add(new XmlSerializerFaultContractInfo(faultContractInfo, serializerStub, elementName));
}
this.xmlSerializerFaultContractInfos = faultInfos;
}
MessageInfo CreateMessageInfo(MessageDescription message, string key)
{
if (message.IsUntypedMessage)
return null;
MessageInfo info = new MessageInfo();
if (message.IsTypedMessage)
key = message.MessageType.FullName + ":" + IsEncoded + ":" + IsRpc;
XmlMembersMapping headersMapping = LoadHeadersMapping(message, key + ":Headers");
info.SetHeaders(parent.generation.AddSerializer(headersMapping));
MessagePartDescriptionCollection rpcEncodedTypedMessgeBodyParts;
info.SetBody(parent.generation.AddSerializer(LoadBodyMapping(message, key, out rpcEncodedTypedMessgeBodyParts)), rpcEncodedTypedMessgeBodyParts);
CreateHeaderDescriptionTable(message, info, headersMapping);
return info;
}
private void CreateHeaderDescriptionTable(MessageDescription message, MessageInfo info, XmlMembersMapping headersMapping)
{
int headerNameIndex = 0;
OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable = new OperationFormatter.MessageHeaderDescriptionTable();
info.SetHeaderDescriptionTable(headerDescriptionTable);
foreach (MessageHeaderDescription header in message.Headers)
{
if (header.IsUnknownHeaderCollection)
info.SetUnknownHeaderDescription(header);
else if (headersMapping != null)
{
XmlMemberMapping memberMapping = headersMapping[headerNameIndex++];
string headerName, headerNs;
if (IsEncoded)
{
headerName = memberMapping.TypeName;
headerNs = memberMapping.TypeNamespace;
}
else
{
headerName = memberMapping.XsdElementName;
headerNs = memberMapping.Namespace;
}
if (headerName != header.Name)
{
if (message.MessageType != null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxHeaderNameMismatchInMessageContract, message.MessageType, header.MemberInfo.Name, header.Name, headerName)));
else
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxHeaderNameMismatchInOperation, this.Operation.Name, this.Operation.DeclaringContract.Name, this.Operation.DeclaringContract.Namespace, header.Name, headerName)));
}
if (headerNs != header.Namespace)
{
if (message.MessageType != null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxHeaderNamespaceMismatchInMessageContract, message.MessageType, header.MemberInfo.Name, header.Namespace, headerNs)));
else
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxHeaderNamespaceMismatchInOperation, this.Operation.Name, this.Operation.DeclaringContract.Name, this.Operation.DeclaringContract.Namespace, header.Namespace, headerNs)));
}
headerDescriptionTable.Add(headerName, headerNs, header);
}
}
}
XmlMembersMapping LoadBodyMapping(MessageDescription message, string mappingKey, out MessagePartDescriptionCollection rpcEncodedTypedMessageBodyParts)
{
MessagePartDescription returnPart;
string wrapperName, wrapperNs;
MessagePartDescriptionCollection bodyParts;
if (IsEncoded && message.IsTypedMessage && message.Body.WrapperName == null)
{
MessagePartDescription wrapperPart = GetWrapperPart(message);
returnPart = null;
rpcEncodedTypedMessageBodyParts = bodyParts = GetWrappedParts(wrapperPart);
wrapperName = wrapperPart.Name;
wrapperNs = wrapperPart.Namespace;
}
else
{
rpcEncodedTypedMessageBodyParts = null;
returnPart = OperationFormatter.IsValidReturnValue(message.Body.ReturnValue) ? message.Body.ReturnValue : null;
bodyParts = message.Body.Parts;
wrapperName = message.Body.WrapperName;
wrapperNs = message.Body.WrapperNamespace;
}
bool isWrapped = (wrapperName != null);
bool hasReturnValue = returnPart != null;
int paramCount = bodyParts.Count + (hasReturnValue ? 1 : 0);
if (paramCount == 0 && !isWrapped) // no need to create serializer
{
return null;
}
XmlReflectionMember[] members = new XmlReflectionMember[paramCount];
int paramIndex = 0;
if (hasReturnValue)
members[paramIndex++] = XmlSerializerHelper.GetXmlReflectionMember(returnPart, IsRpc, IsEncoded, isWrapped);
for (int i = 0; i < bodyParts.Count; i++)
members[paramIndex++] = XmlSerializerHelper.GetXmlReflectionMember(bodyParts[i], IsRpc, IsEncoded, isWrapped);
if (!isWrapped)
wrapperNs = ContractNamespace;
return ImportMembersMapping(wrapperName, wrapperNs, members, isWrapped, IsRpc, mappingKey);
}
private MessagePartDescription GetWrapperPart(MessageDescription message)
{
if (message.Body.Parts.Count != 1)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxRpcMessageMustHaveASingleBody, Operation.Name, message.MessageName)));
MessagePartDescription bodyPart = message.Body.Parts[0];
Type bodyObjectType = bodyPart.Type;
if (bodyObjectType.BaseType != null && bodyObjectType.BaseType != typeof(object))
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxBodyObjectTypeCannotBeInherited, bodyObjectType.FullName)));
if (typeof(IEnumerable).IsAssignableFrom(bodyObjectType))
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxBodyObjectTypeCannotBeInterface, bodyObjectType.FullName, typeof(IEnumerable).FullName)));
if (typeof(IXmlSerializable).IsAssignableFrom(bodyObjectType))
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxBodyObjectTypeCannotBeInterface, bodyObjectType.FullName, typeof(IXmlSerializable).FullName)));
return bodyPart;
}
private MessagePartDescriptionCollection GetWrappedParts(MessagePartDescription bodyPart)
{
Type bodyObjectType = bodyPart.Type;
MessagePartDescriptionCollection partList = new MessagePartDescriptionCollection();
foreach (MemberInfo member in bodyObjectType.GetMembers(BindingFlags.Instance | BindingFlags.Public))
{
if ((member.MemberType & (MemberTypes.Field | MemberTypes.Property)) == 0)
continue;
if (member.IsDefined(typeof(SoapIgnoreAttribute), false/*inherit*/))
continue;
XmlName xmlName = new XmlName(member.Name);
MessagePartDescription part = new MessagePartDescription(xmlName.EncodedName, string.Empty);
part.AdditionalAttributesProvider = part.MemberInfo = member;
part.Index = part.SerializationPosition = partList.Count;
part.Type = (member.MemberType == MemberTypes.Property) ? ((PropertyInfo)member).PropertyType : ((FieldInfo)member).FieldType;
if (bodyPart.HasProtectionLevel)
part.ProtectionLevel = bodyPart.ProtectionLevel;
partList.Add(part);
}
return partList;
}
XmlMembersMapping LoadHeadersMapping(MessageDescription message, string mappingKey)
{
int headerCount = message.Headers.Count;
if (headerCount == 0)
return null;
if (IsEncoded)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxHeadersAreNotSupportedInEncoded, message.MessageName)));
int unknownHeaderCount = 0, headerIndex = 0;
XmlReflectionMember[] members = new XmlReflectionMember[headerCount];
for (int i = 0; i < headerCount; i++)
{
MessageHeaderDescription header = message.Headers[i];
if (!header.IsUnknownHeaderCollection)
{
members[headerIndex++] = XmlSerializerHelper.GetXmlReflectionMember(header, false/*isRpc*/, IsEncoded, false/*isWrapped*/);
}
else
{
unknownHeaderCount++;
}
}
if (unknownHeaderCount == headerCount)
{
return null;
}
if (unknownHeaderCount > 0)
{
XmlReflectionMember[] newMembers = new XmlReflectionMember[headerCount - unknownHeaderCount];
Array.Copy(members, newMembers, newMembers.Length);
members = newMembers;
}
return ImportMembersMapping(ContractName, ContractNamespace, members, false /*isWrapped*/, false /*isRpc*/, mappingKey);
}
internal XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc, string mappingKey)
{
string key = mappingKey.StartsWith(":", StringComparison.Ordinal) ? keyBase + mappingKey : mappingKey;
return this.parent.importer.ImportMembersMapping(new XmlName(elementName, true /*isEncoded*/), ns, members, hasWrapperElement, rpc, this.IsEncoded, key);
}
internal XmlMembersMapping ImportFaultElement(FaultDescription fault, out XmlQualifiedName elementName)
{
// the number of reflection members is always 1 because there is only one fault detail type
XmlReflectionMember[] members = new XmlReflectionMember[1];
XmlName faultElementName = fault.ElementName;
string faultNamespace = fault.Namespace;
if (faultElementName == null)
{
XmlTypeMapping mapping = this.parent.importer.ImportTypeMapping(fault.DetailType, this.IsEncoded);
faultElementName = new XmlName(mapping.ElementName, this.IsEncoded);
faultNamespace = mapping.Namespace;
if (faultElementName == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxFaultTypeAnonymous, this.Operation.Name, fault.DetailType.FullName)));
}
elementName = new XmlQualifiedName(faultElementName.DecodedName, faultNamespace);
members[0] = XmlSerializerHelper.GetXmlReflectionMember(null /*memberName*/, faultElementName, faultNamespace, fault.DetailType,
null /*additionalAttributesProvider*/, false /*isMultiple*/, this.IsEncoded, false /*isWrapped*/);
string mappingKey = "fault:" + faultElementName.DecodedName + ":" + faultNamespace;
return ImportMembersMapping(faultElementName.EncodedName, faultNamespace, members, false /*hasWrapperElement*/, this.IsRpc, mappingKey);
}
}
class XmlSerializerImporter
{
readonly string defaultNs;
XmlReflectionImporter xmlImporter;
SoapReflectionImporter soapImporter;
Dictionary<string, XmlMembersMapping> xmlMappings;
internal XmlSerializerImporter(string defaultNs)
{
this.defaultNs = defaultNs;
this.xmlImporter = null;
this.soapImporter = null;
}
SoapReflectionImporter SoapImporter
{
get
{
if (this.soapImporter == null)
{
this.soapImporter = new SoapReflectionImporter(NamingHelper.CombineUriStrings(defaultNs, "encoded"));
}
return this.soapImporter;
}
}
XmlReflectionImporter XmlImporter
{
get
{
if (this.xmlImporter == null)
{
this.xmlImporter = new XmlReflectionImporter(defaultNs);
}
return this.xmlImporter;
}
}
Dictionary<string, XmlMembersMapping> XmlMappings
{
get
{
if (this.xmlMappings == null)
{
this.xmlMappings = new Dictionary<string, XmlMembersMapping>();
}
return this.xmlMappings;
}
}
internal XmlMembersMapping ImportMembersMapping(XmlName elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc, bool isEncoded, string mappingKey)
{
XmlMembersMapping mapping;
string mappingName = elementName.DecodedName;
if (XmlMappings.TryGetValue(mappingKey, out mapping))
{
return mapping;
}
if (isEncoded)
mapping = this.SoapImporter.ImportMembersMapping(mappingName, ns, members, hasWrapperElement, rpc);
else
mapping = this.XmlImporter.ImportMembersMapping(mappingName, ns, members, hasWrapperElement, rpc);
mapping.SetKey(mappingKey);
XmlMappings.Add(mappingKey, mapping);
return mapping;
}
internal XmlTypeMapping ImportTypeMapping(Type type, bool isEncoded)
{
if (isEncoded)
return this.SoapImporter.ImportTypeMapping(type);
else
return this.XmlImporter.ImportTypeMapping(type);
}
internal void IncludeType(Type knownType, bool isEncoded)
{
if (isEncoded)
this.SoapImporter.IncludeType(knownType);
else
this.XmlImporter.IncludeType(knownType);
}
}
internal class SerializerGenerationContext
{
List<XmlMembersMapping> Mappings = new List<XmlMembersMapping>();
XmlSerializer[] serializers = null;
Type type;
object thisLock = new object();
internal SerializerGenerationContext(Type type)
{
this.type = type;
}
// returns a stub to a serializer
internal SerializerStub AddSerializer(XmlMembersMapping mapping)
{
int handle = -1;
if (mapping != null)
{
handle = ((IList)Mappings).Add(mapping);
}
return new SerializerStub(this, mapping, handle);
}
internal XmlSerializer GetSerializer(int handle)
{
if (handle < 0)
{
return null;
}
if (this.serializers == null)
{
lock (this.thisLock)
{
if (this.serializers == null)
{
this.serializers = GenerateSerializers();
}
}
}
return this.serializers[handle];
}
XmlSerializer[] GenerateSerializers()
{
//this.Mappings may have duplicate mappings (for e.g. samed message contract is used by more than one operation)
//XmlSerializer.FromMappings require unique mappings. The following code uniquifies, calls FromMappings and deuniquifies
List<XmlMembersMapping> uniqueMappings = new List<XmlMembersMapping>();
int[] uniqueIndexes = new int[Mappings.Count];
for (int srcIndex = 0; srcIndex < Mappings.Count; srcIndex++)
{
XmlMembersMapping mapping = Mappings[srcIndex];
int uniqueIndex = uniqueMappings.IndexOf(mapping);
if (uniqueIndex < 0)
{
uniqueMappings.Add(mapping);
uniqueIndex = uniqueMappings.Count - 1;
}
uniqueIndexes[srcIndex] = uniqueIndex;
}
XmlSerializer[] uniqueSerializers = CreateSerializersFromMappings(uniqueMappings.ToArray(), type);
if (uniqueMappings.Count == Mappings.Count)
return uniqueSerializers;
XmlSerializer[] serializers = new XmlSerializer[Mappings.Count];
for (int i = 0; i < Mappings.Count; i++)
{
serializers[i] = uniqueSerializers[uniqueIndexes[i]];
}
return serializers;
}
[Fx.Tag.SecurityNote(Critical = "XmlSerializer.FromMappings has a LinkDemand.",
Safe = "LinkDemand is spurious, not protecting anything in particular.")]
[SecuritySafeCritical]
XmlSerializer[] CreateSerializersFromMappings(XmlMapping[] mappings, Type type)
{
return XmlSerializer.FromMappings(mappings, type);
}
}
internal struct SerializerStub
{
readonly SerializerGenerationContext context;
internal readonly XmlMembersMapping Mapping;
internal readonly int Handle;
internal SerializerStub(SerializerGenerationContext context, XmlMembersMapping mapping, int handle)
{
this.context = context;
this.Mapping = mapping;
this.Handle = handle;
}
internal XmlSerializer GetSerializer()
{
return context.GetSerializer(Handle);
}
}
internal class XmlSerializerFaultContractInfo
{
FaultContractInfo faultContractInfo;
SerializerStub serializerStub;
XmlQualifiedName faultContractElementName;
XmlSerializerObjectSerializer serializer;
internal XmlSerializerFaultContractInfo(FaultContractInfo faultContractInfo, SerializerStub serializerStub,
XmlQualifiedName faultContractElementName)
{
if (faultContractInfo == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("faultContractInfo");
}
if (faultContractElementName == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("faultContractElementName");
}
this.faultContractInfo = faultContractInfo;
this.serializerStub = serializerStub;
this.faultContractElementName = faultContractElementName;
}
internal FaultContractInfo FaultContractInfo
{
get { return this.faultContractInfo; }
}
internal XmlQualifiedName FaultContractElementName
{
get { return this.faultContractElementName; }
}
internal XmlSerializerObjectSerializer Serializer
{
get
{
if (this.serializer == null)
this.serializer = new XmlSerializerObjectSerializer(faultContractInfo.Detail, this.faultContractElementName, this.serializerStub.GetSerializer());
return this.serializer;
}
}
}
internal class MessageInfo : XmlSerializerOperationFormatter.MessageInfo
{
SerializerStub headers;
SerializerStub body;
OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable;
MessageHeaderDescription unknownHeaderDescription;
MessagePartDescriptionCollection rpcEncodedTypedMessageBodyParts;
internal XmlMembersMapping BodyMapping
{
get { return body.Mapping; }
}
internal override XmlSerializer BodySerializer
{
get { return body.GetSerializer(); }
}
internal XmlMembersMapping HeadersMapping
{
get { return headers.Mapping; }
}
internal override XmlSerializer HeaderSerializer
{
get { return headers.GetSerializer(); }
}
internal override OperationFormatter.MessageHeaderDescriptionTable HeaderDescriptionTable
{
get { return this.headerDescriptionTable; }
}
internal override MessageHeaderDescription UnknownHeaderDescription
{
get { return this.unknownHeaderDescription; }
}
internal override MessagePartDescriptionCollection RpcEncodedTypedMessageBodyParts
{
get { return rpcEncodedTypedMessageBodyParts; }
}
internal void SetBody(SerializerStub body, MessagePartDescriptionCollection rpcEncodedTypedMessageBodyParts)
{
this.body = body;
this.rpcEncodedTypedMessageBodyParts = rpcEncodedTypedMessageBodyParts;
}
internal void SetHeaders(SerializerStub headers)
{
this.headers = headers;
}
internal void SetHeaderDescriptionTable(OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable)
{
this.headerDescriptionTable = headerDescriptionTable;
}
internal void SetUnknownHeaderDescription(MessageHeaderDescription unknownHeaderDescription)
{
this.unknownHeaderDescription = unknownHeaderDescription;
}
}
}
}
static class XmlSerializerHelper
{
static internal XmlReflectionMember GetXmlReflectionMember(MessagePartDescription part, bool isRpc, bool isEncoded, bool isWrapped)
{
string ns = isRpc ? null : part.Namespace;
ICustomAttributeProvider additionalAttributesProvider = null;
if (isEncoded || part.AdditionalAttributesProvider is MemberInfo)
additionalAttributesProvider = part.AdditionalAttributesProvider;
XmlName memberName = string.IsNullOrEmpty(part.UniquePartName) ? null : new XmlName(part.UniquePartName, true /*isEncoded*/);
XmlName elementName = part.XmlName;
return GetXmlReflectionMember(memberName, elementName, ns, part.Type, additionalAttributesProvider, part.Multiple, isEncoded, isWrapped);
}
static internal XmlReflectionMember GetXmlReflectionMember(XmlName memberName, XmlName elementName, string ns, Type type, ICustomAttributeProvider additionalAttributesProvider, bool isMultiple, bool isEncoded, bool isWrapped)
{
if (isEncoded && isMultiple)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxMultiplePartsNotAllowedInEncoded, elementName.DecodedName, ns)));
XmlReflectionMember member = new XmlReflectionMember();
member.MemberName = (memberName ?? elementName).DecodedName;
member.MemberType = type;
if (member.MemberType.IsByRef)
member.MemberType = member.MemberType.GetElementType();
if (isMultiple)
member.MemberType = member.MemberType.MakeArrayType();
if (additionalAttributesProvider != null)
{
if (isEncoded)
member.SoapAttributes = new SoapAttributes(additionalAttributesProvider);
else
member.XmlAttributes = new XmlAttributes(additionalAttributesProvider);
}
if (isEncoded)
{
if (member.SoapAttributes == null)
member.SoapAttributes = new SoapAttributes();
else
{
Type invalidAttributeType = null;
if (member.SoapAttributes.SoapAttribute != null)
invalidAttributeType = typeof(SoapAttributeAttribute);
else if (member.SoapAttributes.SoapIgnore)
invalidAttributeType = typeof(SoapIgnoreAttribute);
else if (member.SoapAttributes.SoapType != null)
invalidAttributeType = typeof(SoapTypeAttribute);
if (invalidAttributeType != null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidSoapAttribute, invalidAttributeType, elementName.DecodedName)));
}
if (member.SoapAttributes.SoapElement == null)
member.SoapAttributes.SoapElement = new SoapElementAttribute(elementName.DecodedName);
}
else
{
if (member.XmlAttributes == null)
member.XmlAttributes = new XmlAttributes();
else
{
Type invalidAttributeType = null;
if (member.XmlAttributes.XmlAttribute != null)
invalidAttributeType = typeof(XmlAttributeAttribute);
else if (member.XmlAttributes.XmlAnyAttribute != null && !isWrapped)
invalidAttributeType = typeof(XmlAnyAttributeAttribute);
else if (member.XmlAttributes.XmlChoiceIdentifier != null)
invalidAttributeType = typeof(XmlChoiceIdentifierAttribute);
else if (member.XmlAttributes.XmlIgnore)
invalidAttributeType = typeof(XmlIgnoreAttribute);
else if (member.XmlAttributes.Xmlns)
invalidAttributeType = typeof(XmlNamespaceDeclarationsAttribute);
else if (member.XmlAttributes.XmlText != null)
invalidAttributeType = typeof(XmlTextAttribute);
else if (member.XmlAttributes.XmlEnum != null)
invalidAttributeType = typeof(XmlEnumAttribute);
if (invalidAttributeType != null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(isWrapped ? SR.SFxInvalidXmlAttributeInWrapped : SR.SFxInvalidXmlAttributeInBare, invalidAttributeType, elementName.DecodedName)));
if (member.XmlAttributes.XmlArray != null && isMultiple)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxXmlArrayNotAllowedForMultiple, elementName.DecodedName, ns)));
}
bool isArray = member.MemberType.IsArray;
if ((isArray && !isMultiple && member.MemberType != typeof(byte[])) ||
(!isArray && typeof(IEnumerable).IsAssignableFrom(member.MemberType) && member.MemberType != typeof(string) && !typeof(XmlNode).IsAssignableFrom(member.MemberType) && !typeof(IXmlSerializable).IsAssignableFrom(member.MemberType)))
{
if (member.XmlAttributes.XmlArray != null)
{
if (member.XmlAttributes.XmlArray.ElementName == String.Empty)
member.XmlAttributes.XmlArray.ElementName = elementName.DecodedName;
if (member.XmlAttributes.XmlArray.Namespace == null)
member.XmlAttributes.XmlArray.Namespace = ns;
}
else if (HasNoXmlParameterAttributes(member.XmlAttributes))
{
member.XmlAttributes.XmlArray = new XmlArrayAttribute();
member.XmlAttributes.XmlArray.ElementName = elementName.DecodedName;
member.XmlAttributes.XmlArray.Namespace = ns;
}
}
else
{
if (member.XmlAttributes.XmlElements == null || member.XmlAttributes.XmlElements.Count == 0)
{
if (HasNoXmlParameterAttributes(member.XmlAttributes))
{
XmlElementAttribute elementAttribute = new XmlElementAttribute();
elementAttribute.ElementName = elementName.DecodedName;
elementAttribute.Namespace = ns;
member.XmlAttributes.XmlElements.Add(elementAttribute);
}
}
else
{
foreach (XmlElementAttribute elementAttribute in member.XmlAttributes.XmlElements)
{
if (elementAttribute.ElementName == String.Empty)
elementAttribute.ElementName = elementName.DecodedName;
if (elementAttribute.Namespace == null)
elementAttribute.Namespace = ns;
}
}
}
}
return member;
}
static bool HasNoXmlParameterAttributes(XmlAttributes xmlAttributes)
{
return xmlAttributes.XmlAnyAttribute == null &&
(xmlAttributes.XmlAnyElements == null || xmlAttributes.XmlAnyElements.Count == 0) &&
xmlAttributes.XmlArray == null &&
xmlAttributes.XmlAttribute == null &&
!xmlAttributes.XmlIgnore &&
xmlAttributes.XmlText == null &&
xmlAttributes.XmlChoiceIdentifier == null &&
(xmlAttributes.XmlElements == null || xmlAttributes.XmlElements.Count == 0) &&
!xmlAttributes.Xmlns;
}
}
}
|