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 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Runtime;
using System.Runtime.Diagnostics;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Diagnostics;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using WsdlNS = System.Web.Services.Description;
class MessageContractImporter
{
static readonly XmlQualifiedName AnyType = new XmlQualifiedName("anyType", XmlSchema.Namespace);
readonly XmlSchemaSet allSchemas;
readonly WsdlContractConversionContext contractContext;
readonly WsdlImporter importer;
SchemaImporter schemaImporter;
readonly FaultImportOptions faultImportOptions;
static object schemaImporterLock = new object();
Dictionary<WsdlNS.Message, IList<string>> bodyPartsTable;
Dictionary<WsdlNS.Message, IList<string>> BodyPartsTable
{
get
{
if (bodyPartsTable == null)
bodyPartsTable = new Dictionary<WsdlNS.Message, IList<string>>();
return bodyPartsTable;
}
}
static internal void ImportMessageBinding(WsdlImporter importer, WsdlEndpointConversionContext endpointContext, Type schemaImporterType)
{
// All the work is done in ImportMessageContract call
bool isReferencedContract = IsReferencedContract(importer, endpointContext);
MarkSoapExtensionsAsHandled(endpointContext.WsdlBinding);
foreach (WsdlNS.OperationBinding wsdlOperationBinding in endpointContext.WsdlBinding.Operations)
{
OperationDescription operation = endpointContext.GetOperationDescription(wsdlOperationBinding);
if (isReferencedContract || OperationHasBeenHandled(operation))
{
MarkSoapExtensionsAsHandled(wsdlOperationBinding);
if (wsdlOperationBinding.Input != null)
{
MarkSoapExtensionsAsHandled(wsdlOperationBinding.Input);
}
if (wsdlOperationBinding.Output != null)
{
MarkSoapExtensionsAsHandled(wsdlOperationBinding.Output);
}
foreach (WsdlNS.MessageBinding wsdlMessageBinding in wsdlOperationBinding.Faults)
{
MarkSoapExtensionsAsHandled(wsdlMessageBinding);
}
}
}
}
static bool OperationHasBeenHandled(OperationDescription operation)
{
return (operation.Behaviors.Find<IOperationContractGenerationExtension>() != null);
}
static bool IsReferencedContract(WsdlImporter importer, WsdlEndpointConversionContext endpointContext)
{
return importer.KnownContracts.ContainsValue(endpointContext.Endpoint.Contract);
}
static void MarkSoapExtensionsAsHandled(WsdlNS.NamedItem item)
{
foreach (object o in item.Extensions)
{
WsdlNS.ServiceDescriptionFormatExtension ext = o as WsdlNS.ServiceDescriptionFormatExtension;
if (ext != null && IsSoapBindingExtension(ext))
ext.Handled = true;
else if (SoapHelper.IsSoapFaultBinding(o as XmlElement))
ext.Handled = true;
}
}
static bool IsSoapBindingExtension(WsdlNS.ServiceDescriptionFormatExtension ext)
{
if (ext is WsdlNS.SoapBinding
|| ext is WsdlNS.SoapBodyBinding
|| ext is WsdlNS.SoapHeaderBinding
|| ext is WsdlNS.SoapOperationBinding
|| ext is WsdlNS.SoapFaultBinding
|| ext is WsdlNS.SoapHeaderFaultBinding
)
{
return true;
}
return false;
}
static internal void ImportMessageContract(WsdlImporter importer, WsdlContractConversionContext contractContext, SchemaImporter schemaImporter)
{
new MessageContractImporter(importer, contractContext, schemaImporter).ImportMessageContract();
}
MessageContractImporter(WsdlImporter importer, WsdlContractConversionContext contractContext, SchemaImporter schemaImporter)
{
this.contractContext = contractContext;
this.importer = importer;
this.allSchemas = GatherSchemas(importer);
this.schemaImporter = schemaImporter;
object faultImportOptions;
if (this.importer.State.TryGetValue(typeof(FaultImportOptions), out faultImportOptions))
this.faultImportOptions = (FaultImportOptions)faultImportOptions;
else
this.faultImportOptions = new FaultImportOptions();
}
XmlSchemaSet AllSchemas
{
get { return this.allSchemas; }
}
SchemaImporter CurrentSchemaImporter
{
get { return schemaImporter; }
}
internal void AddWarning(string message)
{
AddError(message, true);
}
void AddError(string message)
{
AddError(message, false);
}
void AddError(string message, bool isWarning)
{
MetadataConversionError warning = new MetadataConversionError(message, isWarning);
if (!importer.Errors.Contains(warning))
importer.Errors.Add(warning);
}
void TraceImportInformation(OperationDescription operation)
{
if (DiagnosticUtility.ShouldTraceInformation)
{
Dictionary<String, String> ht = new Dictionary<string, string>(2)
{
{ "Operation", operation.Name },
{ "Format", CurrentSchemaImporter.GetFormatName() }
};
TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.CannotBeImportedInCurrentFormat,
SR.GetString(SR.TraceCodeCannotBeImportedInCurrentFormat), new DictionaryTraceRecord(ht), null, null);
}
}
void ImportMessageContract()
{
if (contractContext.Contract.Operations.Count <= 0)
return;
CurrentSchemaImporter.PreprocessSchema();
bool importerUsed = true;
OperationInfo[] infos = new OperationInfo[contractContext.Contract.Operations.Count];
int i = 0;
foreach (OperationDescription operation in contractContext.Contract.Operations)
{
OperationInfo operationInfo;
if (!CanImportOperation(operation, out operationInfo))
{
TraceImportInformation(operation);
importerUsed = false;
break;
}
infos[i++] = operationInfo;
}
if (importerUsed)
{
i = 0;
foreach (OperationDescription operation in contractContext.Contract.Operations)
{
ImportOperationContract(operation, infos[i++]);
}
}
CurrentSchemaImporter.PostprocessSchema(importerUsed);
}
bool CanImportOperation(OperationDescription operation, out OperationInfo operationInfo)
{
operationInfo = null;
if (OperationHasBeenHandled(operation))
return false;
WsdlNS.Operation wsdlOperation = contractContext.GetOperation(operation);
Collection<WsdlNS.OperationBinding> wsdlOperationBindings = contractContext.GetOperationBindings(wsdlOperation);
return CanImportOperation(operation, wsdlOperation, wsdlOperationBindings, out operationInfo)
&& CanImportFaults(wsdlOperation, operation);
}
bool CanImportFaults(WsdlNS.Operation operation, OperationDescription description)
{
// When this.faultImportOptions.UseMessageFormat is false, we fall back to the V1 behavior of using DataContractSerializer to import all faults.
// We can, therefore, return true in those cases without actually doing the checks involved in CanImportFaults.
if (!this.faultImportOptions.UseMessageFormat)
return true;
foreach (WsdlNS.OperationFault fault in operation.Faults)
{
if (!CanImportFault(fault, description))
return false;
}
return true;
}
bool CanImportFault(WsdlNS.OperationFault fault, OperationDescription description)
{
XmlSchemaElement detailElement;
XmlQualifiedName detailElementTypeName;
XmlQualifiedName detailElementQname;
if (!ValidateFault(fault, description, out detailElement, out detailElementTypeName, out detailElementQname))
return false;
return this.CurrentSchemaImporter.CanImportFault(detailElement, detailElementTypeName);
}
void ImportOperationContract(OperationDescription operation, OperationInfo operationInfo)
{
Fx.Assert(!OperationHasBeenHandled(operation), "");
Fx.Assert(operationInfo != null, "");
WsdlNS.Operation wsdlOperation = contractContext.GetOperation(operation);
Collection<WsdlNS.OperationBinding> wsdlOperationBindings = contractContext.GetOperationBindings(wsdlOperation);
bool isReply = false;
foreach (WsdlNS.OperationMessage operationMessage in wsdlOperation.Messages)
{
ImportMessage(operationMessage, isReply, operationInfo.IsEncoded, operationInfo.AreAllMessagesWrapped);
isReply = true;
}
if (operationInfo.Style == OperationFormatStyle.Rpc)
SetWrapperName(operation);
this.CurrentSchemaImporter.SetOperationStyle(operation, operationInfo.Style);
this.CurrentSchemaImporter.SetOperationIsEncoded(operation, operationInfo.IsEncoded);
this.CurrentSchemaImporter.SetOperationSupportFaults(operation,
this.faultImportOptions.UseMessageFormat);
ImportFaults(wsdlOperation, operation, operationInfo.IsEncoded);
foreach (WsdlNS.OperationBinding wsdlOperationBinding in wsdlOperationBindings)
{
foreach (MessageDescription message in operation.Messages)
{
WsdlNS.OperationMessage wsdlOperationMessage = contractContext.GetOperationMessage(message);
WsdlNS.ServiceDescriptionCollection wsdlDocuments = wsdlOperationMessage.Operation.PortType.ServiceDescription.ServiceDescriptions;
WsdlNS.Message wsdlMessage = wsdlDocuments.GetMessage(wsdlOperationMessage.Message);
WsdlNS.MessageBinding messageBinding = (message.Direction == MessageDirection.Input)
? (WsdlNS.MessageBinding)wsdlOperationBinding.Input
: (WsdlNS.MessageBinding)wsdlOperationBinding.Output;
if (messageBinding != null)
ImportMessageBinding(messageBinding, wsdlMessage, message, operationInfo.Style, operationInfo.IsEncoded);
}
}
operation.Behaviors.Add(CurrentSchemaImporter.GetOperationGenerator());
}
bool CanImportOperation(OperationDescription operation, WsdlNS.Operation wsdlOperation, Collection<WsdlNS.OperationBinding> operationBindings,
out OperationInfo operationInfo)
{
operationInfo = null;
OperationFormatStyle style = OperationFormatStyle.Document;
bool isEncoded = false;
bool areAllMessagesWrapped = true;
// Check if operation bindings can be imported
StyleAndUse? styleAndUse = null;
WsdlNS.ServiceDescriptionCollection documents = wsdlOperation.PortType.ServiceDescription.ServiceDescriptions;
WsdlNS.OperationBinding prevOperationBinding = null;
foreach (WsdlNS.OperationBinding operationBinding in operationBindings)
{
OperationFormatStyle operationStyle = GetStyle(operationBinding);
bool? isOperationEncoded = null;
foreach (MessageDescription message in operation.Messages)
{
WsdlNS.OperationMessage operationMessage = contractContext.GetOperationMessage(message);
if (operationMessage.Message.IsEmpty)
{
if (operationMessage is WsdlNS.OperationInput)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxWsdlOperationInputNeedsMessageAttribute2, wsdlOperation.Name, wsdlOperation.PortType.Name)));
if (operationMessage is WsdlNS.OperationOutput)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxWsdlOperationOutputNeedsMessageAttribute2, wsdlOperation.Name, wsdlOperation.PortType.Name)));
}
WsdlNS.Message wsdlMessage = documents.GetMessage(operationMessage.Message);
if (wsdlMessage != null)
{
WsdlNS.MessageBinding messageBinding = (message.Direction == MessageDirection.Input)
? (WsdlNS.MessageBinding)operationBinding.Input
: (WsdlNS.MessageBinding)operationBinding.Output;
if (messageBinding != null)
{
bool isMessageEncoded;
if (!CanImportMessageBinding(messageBinding, wsdlMessage, operationStyle, out isMessageEncoded))
return false;
if (isOperationEncoded == null)
isOperationEncoded = isMessageEncoded;
else if (isOperationEncoded != isMessageEncoded)
AddError(SR.GetString(SR.SFxInconsistentWsdlOperationUseInBindingMessages, messageBinding.OperationBinding.Name, messageBinding.OperationBinding.Binding.Name));
}
}
}
foreach (WsdlNS.FaultBinding faultBinding in operationBinding.Faults)
{
bool isFaultEncoded;
if (!CanImportFaultBinding(faultBinding, operationStyle, out isFaultEncoded))
return false;
if (isOperationEncoded == null)
isOperationEncoded = isFaultEncoded;
else if (isOperationEncoded != isFaultEncoded)
AddError(SR.GetString(SR.SFxInconsistentWsdlOperationUseInBindingFaults, faultBinding.OperationBinding.Name, faultBinding.OperationBinding.Binding.Name));
}
isOperationEncoded = isOperationEncoded ?? false;
if (styleAndUse == null)
{
styleAndUse = GetStyleAndUse(operationStyle, isOperationEncoded.Value);
style = operationStyle;
isEncoded = isOperationEncoded.Value;
prevOperationBinding = operationBinding;
}
else
{
StyleAndUse operationStyleAndUse = GetStyleAndUse(operationStyle, isOperationEncoded.Value);
if (operationStyleAndUse != styleAndUse)
{
AddError(SR.GetString(SR.SFxInconsistentWsdlOperationUseAndStyleInBinding,
operation.Name, operationBinding.Binding.Name, GetUse(operationStyleAndUse), GetStyle(operationStyleAndUse),
prevOperationBinding.Binding.Name, GetUse(styleAndUse.Value), GetStyle(styleAndUse.Value)));
}
if (operationStyleAndUse < styleAndUse)
{
styleAndUse = operationStyleAndUse;
style = operationStyle;
isEncoded = isOperationEncoded.Value;
prevOperationBinding = operationBinding;
}
}
}
// Check if operation can be imported
OperationFormatStyle? inferredOperationStyle = null;
foreach (WsdlNS.OperationMessage wsdlOperationMessage in wsdlOperation.Messages)
{
if (wsdlOperationMessage.Message.IsEmpty)
{
if (wsdlOperationMessage is WsdlNS.OperationInput)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxWsdlOperationInputNeedsMessageAttribute2, wsdlOperation.Name, wsdlOperation.PortType.Name)));
if (wsdlOperationMessage is WsdlNS.OperationOutput)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxWsdlOperationOutputNeedsMessageAttribute2, wsdlOperation.Name, wsdlOperation.PortType.Name)));
}
WsdlNS.Message wsdlMessage = documents.GetMessage(wsdlOperationMessage.Message);
OperationFormatStyle? inferredMessageStyle;
if (!CanImportMessage(wsdlMessage, wsdlOperationMessage.Name, out inferredMessageStyle, ref areAllMessagesWrapped))
return false;
if (wsdlMessage.Parts.Count > 0)
{
if (inferredOperationStyle == null
|| (inferredMessageStyle != null
&& inferredMessageStyle != inferredOperationStyle
&& inferredMessageStyle.Value == OperationFormatStyle.Document))
{
inferredOperationStyle = inferredMessageStyle;
}
}
}
// Verify that information in operation bindings and operation match
if (styleAndUse == null)
style = inferredOperationStyle ?? OperationFormatStyle.Document;
else if (inferredOperationStyle != null)
{
if (inferredOperationStyle.Value != style && inferredOperationStyle.Value == OperationFormatStyle.Document)
AddError(SR.GetString(SR.SFxInconsistentWsdlOperationStyleInOperationMessages, operation.Name, inferredOperationStyle, style));
}
operationInfo = new OperationInfo(style, isEncoded, areAllMessagesWrapped);
return true;
}
bool CanImportMessage(WsdlNS.Message wsdlMessage, string operationName, out OperationFormatStyle? inferredStyle, ref bool areAllMessagesWrapped)
{
WsdlNS.MessagePartCollection messageParts = wsdlMessage.Parts;
// try the special cases: wrapped and Message
if (messageParts.Count == 1)
{
if (CanImportAnyMessage(messageParts[0]))
{
areAllMessagesWrapped = false;
inferredStyle = OperationFormatStyle.Document;
return true;
}
if (CanImportStream(messageParts[0], out inferredStyle, ref areAllMessagesWrapped))
return true;
if (areAllMessagesWrapped && CanImportWrappedMessage(messageParts[0]))
{
inferredStyle = OperationFormatStyle.Document;
return true;
}
areAllMessagesWrapped = false;
}
inferredStyle = null;
IList<string> bodyPartsFromBindings;
BodyPartsTable.TryGetValue(wsdlMessage, out bodyPartsFromBindings);
foreach (WsdlNS.MessagePart part in messageParts)
{
if (bodyPartsFromBindings != null && !bodyPartsFromBindings.Contains(part.Name))
continue;
OperationFormatStyle style;
if (!CurrentSchemaImporter.CanImportMessagePart(part, out style))
return false;
if (inferredStyle == null)
inferredStyle = style;
else if (style != inferredStyle.Value)
AddError(SR.GetString(SR.SFxInconsistentWsdlOperationStyleInMessageParts, operationName));
}
return true;
}
void ImportMessage(WsdlNS.OperationMessage wsdlOperationMessage, bool isReply, bool isEncoded, bool areAllMessagesWrapped)
{
MessageDescription messageDescription = contractContext.GetMessageDescription(wsdlOperationMessage);
OperationDescription operation = contractContext.GetOperationDescription(wsdlOperationMessage.Operation);
WsdlNS.ServiceDescriptionCollection wsdlDocuments = wsdlOperationMessage.Operation.PortType.ServiceDescription.ServiceDescriptions;
WsdlNS.Message wsdlMessage = wsdlDocuments.GetMessage(wsdlOperationMessage.Message);
if (wsdlMessage.Parts.Count == 1)
{
if (TryImportAnyMessage(wsdlMessage.Parts[0], messageDescription, isReply))
return;
if (TryImportStream(wsdlMessage.Parts[0], messageDescription, isReply, areAllMessagesWrapped))
return;
if (areAllMessagesWrapped && TryImportWrappedMessage(messageDescription, operation.Messages[0], wsdlMessage, isReply))
return;
}
WsdlNS.MessagePartCollection messageParts = wsdlMessage.Parts;
IList<string> bodyPartsFromBindings;
BodyPartsTable.TryGetValue(wsdlMessage, out bodyPartsFromBindings);
string[] parameterOrder = wsdlOperationMessage.Operation.ParameterOrder;
foreach (WsdlNS.MessagePart part in messageParts)
{
if (!ValidWsdl.Check(part, wsdlMessage, AddWarning))
continue;
if (bodyPartsFromBindings != null && !bodyPartsFromBindings.Contains(part.Name))
continue;
bool isReturn = false;
if (parameterOrder != null && isReply)
isReturn = Array.IndexOf<string>(parameterOrder, part.Name) == -1;
MessagePartDescription partDesc = CurrentSchemaImporter.ImportMessagePart(part, false/*isHeader*/, isEncoded);
if (isReturn && messageDescription.Body.ReturnValue == null)
messageDescription.Body.ReturnValue = partDesc;
else
messageDescription.Body.Parts.Add(partDesc);
}
if (isReply && messageDescription.Body.ReturnValue == null)
{
if (messageDescription.Body.Parts.Count > 0)
{
if (!CheckIsRef(operation.Messages[0], messageDescription.Body.Parts[0]))
{
messageDescription.Body.ReturnValue = messageDescription.Body.Parts[0];
messageDescription.Body.Parts.RemoveAt(0);
}
}
}
}
enum StyleAndUse
{
DocumentLiteral,
RpcLiteral,
RpcEncoded,
DocumentEncoded,
}
static StyleAndUse GetStyleAndUse(OperationFormatStyle style, bool isEncoded)
{
if (style == OperationFormatStyle.Document)
{
return isEncoded ? StyleAndUse.DocumentEncoded : StyleAndUse.DocumentLiteral;
}
else
{
return isEncoded ? StyleAndUse.RpcEncoded : StyleAndUse.RpcLiteral;
}
}
static string GetStyle(StyleAndUse styleAndUse)
{
return (styleAndUse == StyleAndUse.RpcLiteral || styleAndUse == StyleAndUse.RpcEncoded) ? "rpc" : "document";
}
static string GetUse(StyleAndUse styleAndUse)
{
return (styleAndUse == StyleAndUse.RpcEncoded || styleAndUse == StyleAndUse.DocumentEncoded) ? "encoded" : "literal";
}
static void SetWrapperName(OperationDescription operation)
{
MessageDescriptionCollection messages = operation.Messages;
if (messages != null && messages.Count > 0)
{
MessageDescription request = messages[0];
if (request != null)
{
request.Body.WrapperName = operation.Name;
request.Body.WrapperNamespace = operation.DeclaringContract.Namespace;
}
if (messages.Count > 1)
{
MessageDescription response = messages[1];
if (response != null)
{
response.Body.WrapperName = TypeLoader.GetBodyWrapperResponseName(operation.Name).EncodedName;
response.Body.WrapperNamespace = operation.DeclaringContract.Namespace;
}
}
}
}
void ImportFaults(WsdlNS.Operation operation, OperationDescription description, bool isEncoded)
{
foreach (WsdlNS.OperationFault fault in operation.Faults)
{
ImportFault(fault, description, isEncoded);
}
}
void ImportFault(WsdlNS.OperationFault fault, OperationDescription description, bool isEncoded)
{
XmlSchemaElement detailElement;
XmlQualifiedName detailElementTypeName;
XmlQualifiedName detailElementQname;
if (!ValidateFault(fault, description, out detailElement, out detailElementTypeName, out detailElementQname))
return;
SchemaImporter faultImporter;
if (this.faultImportOptions.UseMessageFormat)
faultImporter = this.CurrentSchemaImporter;
else
faultImporter = DataContractSerializerSchemaImporter.Get(this.importer);
CodeTypeReference detailElementTypeRef;
if (IsNullOrEmpty(detailElementTypeName))
detailElementTypeRef = faultImporter.ImportFaultElement(detailElementQname, detailElement, isEncoded);
else
detailElementTypeRef = faultImporter.ImportFaultType(detailElementQname, detailElementTypeName, isEncoded);
FaultDescription faultDescription = contractContext.GetFaultDescription(fault);
faultDescription.DetailTypeReference = detailElementTypeRef;
faultDescription.ElementName = new XmlName(detailElementQname.Name, true /*isEncoded*/);
faultDescription.Namespace = detailElementQname.Namespace;
}
bool ValidateFault(WsdlNS.OperationFault fault, OperationDescription description, out XmlSchemaElement detailElement,
out XmlQualifiedName detailElementTypeName, out XmlQualifiedName detailElementQname)
{
detailElement = null;
detailElementTypeName = null;
detailElementQname = null;
// this will throw if the message is not found (consider wrapping exception)
WsdlNS.ServiceDescriptionCollection wsdlDocuments = fault.Operation.PortType.ServiceDescription.ServiceDescriptions;
if (fault.Message.IsEmpty)
{
TraceFaultCannotBeImported(fault.Name, description.Name, SR.GetString(SR.SFxWsdlOperationFaultNeedsMessageAttribute2, fault.Name, fault.Operation.PortType.Name));
description.Faults.Remove(contractContext.GetFaultDescription(fault));
return false;
}
WsdlNS.Message faultMessage = wsdlDocuments.GetMessage(fault.Message);
// we only recognize faults with a single part (single element inside detail):
if (faultMessage.Parts.Count != 1)
{
TraceFaultCannotBeImported(fault.Name, description.Name, SR.GetString(SR.UnsupportedWSDLOnlyOneMessage));
description.Faults.Remove(contractContext.GetFaultDescription(fault));
return false;
}
WsdlNS.MessagePart faultMessageDetail = faultMessage.Parts[0];
detailElementQname = faultMessageDetail.Element;
if (IsNullOrEmpty(detailElementQname) || !IsNullOrEmpty(faultMessageDetail.Type))
{
TraceFaultCannotBeImported(fault.Name, description.Name, SR.GetString(SR.UnsupportedWSDLTheFault));
description.Faults.Remove(contractContext.GetFaultDescription(fault));
return false;
}
detailElement = FindSchemaElement(this.AllSchemas, detailElementQname);
detailElementTypeName = GetTypeName(detailElement);
return true;
}
bool CanImportAnyMessage(WsdlNS.MessagePart part)
{
return CheckPart(part.Type, DataContractSerializerMessageContractImporter.GenericMessageTypeName);
}
bool TryImportAnyMessage(WsdlNS.MessagePart part, MessageDescription description, bool isReply)
{
return CheckAndAddPart(part.Type, DataContractSerializerMessageContractImporter.GenericMessageTypeName, part.Name, string.Empty, typeof(Message), description, isReply);
}
bool CanImportStream(WsdlNS.MessagePart part, out OperationFormatStyle? style, ref bool areAllMessagesWrapped)
{
style = OperationFormatStyle.Document;
string ns;
XmlSchemaForm elementFormDefault;
if (areAllMessagesWrapped && IsWrapperPart(part))
{
XmlSchemaComplexType complexType = GetElementComplexType(part.Element, allSchemas, out ns, out elementFormDefault);
if (complexType != null)
{
XmlSchemaSequence rootSequence = GetRootSequence(complexType);
if (rootSequence != null && rootSequence.Items.Count == 1 && rootSequence.Items[0] is XmlSchemaElement)
return CheckPart(((XmlSchemaElement)rootSequence.Items[0]).SchemaTypeName, DataContractSerializerMessageContractImporter.StreamBodyTypeName);
}
return false;
}
areAllMessagesWrapped = false;
XmlQualifiedName typeName = part.Type;
style = OperationFormatStyle.Rpc;
if (IsNullOrEmpty(typeName))
{
if (IsNullOrEmpty(part.Element))
return false;
style = OperationFormatStyle.Document;
typeName = GetTypeName(FindSchemaElement(allSchemas, part.Element));
}
return CheckPart(typeName, DataContractSerializerMessageContractImporter.StreamBodyTypeName);
}
bool TryImportStream(WsdlNS.MessagePart part, MessageDescription description, bool isReply, bool areAllMessagesWrapped)
{
string ns = string.Empty;
XmlSchemaForm elementFormDefault;
if (areAllMessagesWrapped && IsWrapperPart(part))
{
XmlSchemaSequence rootSequence = GetRootSequence(GetElementComplexType(part.Element, allSchemas, out ns, out elementFormDefault));
if (rootSequence != null && rootSequence.Items.Count == 1 && rootSequence.Items[0] is XmlSchemaElement)
{
XmlSchemaElement element = (XmlSchemaElement)rootSequence.Items[0];
description.Body.WrapperName = new XmlName(part.Element.Name, true /*isEncoded*/).EncodedName;
description.Body.WrapperNamespace = part.Element.Namespace;
if (element.SchemaTypeName.IsEmpty && element.RefName != null)
{
return CheckAndAddPart(element.ElementSchemaType.QualifiedName, DataContractSerializerMessageContractImporter.StreamBodyTypeName, element.RefName.Name, GetLocalElementNamespace(element.RefName.Namespace, element, elementFormDefault), typeof(Stream), description, isReply);
}
else
{
return CheckAndAddPart(element.SchemaTypeName, DataContractSerializerMessageContractImporter.StreamBodyTypeName, element.Name, GetLocalElementNamespace(ns, element, elementFormDefault), typeof(Stream), description, isReply);
}
}
return false;
}
XmlQualifiedName typeName = part.Type;
if (IsNullOrEmpty(typeName))
{
if (IsNullOrEmpty(part.Element))
return false;
ns = part.Element.Namespace;
typeName = GetTypeName(FindSchemaElement(allSchemas, part.Element));
}
return CheckAndAddPart(typeName, DataContractSerializerMessageContractImporter.StreamBodyTypeName, part.Name, ns, typeof(Stream), description, isReply);
}
bool CanImportWrappedMessage(WsdlNS.MessagePart wsdlPart)
{
return (IsWrapperPart(wsdlPart)) ? CurrentSchemaImporter.CanImportWrapperElement(wsdlPart.Element) : false;
}
bool TryImportWrappedMessage(MessageDescription messageDescription, MessageDescription requestMessage, WsdlNS.Message wsdlMessage, bool isReply)
{
WsdlNS.MessagePart wsdlPart = wsdlMessage.Parts[0];
if (CanImportWrappedMessage(wsdlPart))
{
XmlQualifiedName elementName = wsdlPart.Element;
MessagePartDescription[] parts = CurrentSchemaImporter.ImportWrapperElement(elementName);
if (parts == null)
return false;
messageDescription.Body.WrapperName = new XmlName(elementName.Name, true /*isEncoded*/).EncodedName;
messageDescription.Body.WrapperNamespace = elementName.Namespace;
if (parts.Length > 0)
{
int partIndex = 0;
if (isReply && messageDescription.Body.ReturnValue == null && !CheckIsRef(requestMessage, parts[0]))
{
messageDescription.Body.ReturnValue = parts[0];
partIndex = 1;
}
for (; partIndex < parts.Length; partIndex++)
{
MessagePartDescription part = parts[partIndex];
messageDescription.Body.Parts.Add(part);
}
}
return true;
}
return false;
}
private bool IsWrapperPart(WsdlNS.MessagePart wsdlPart)
{
bool wrapFlag = false; // turn off special-casing for partname="parameters" if "wrapped" flag was set by user
object wrappedOptions = null;
if (this.importer.State.TryGetValue(typeof(WrappedOptions), out wrappedOptions))
{
wrapFlag = ((WrappedOptions)wrappedOptions).WrappedFlag;
}
return wsdlPart.Name == "parameters" && !IsNullOrEmpty(wsdlPart.Element) && !wrapFlag;
}
bool CheckIsRef(MessageDescription requestMessage, MessagePartDescription part)
{
foreach (MessagePartDescription requestPart in requestMessage.Body.Parts)
{
if (CompareMessageParts(requestPart, part))
return true;
}
return false;
}
bool CompareMessageParts(MessagePartDescription x, MessagePartDescription y)
{
return (x.Name == y.Name && x.Namespace == y.Namespace);
}
static WsdlNS.MessagePart FindPartByName(WsdlNS.Message message, string name)
{
Fx.Assert(message != null, "Should not attempt to look for a part in an null message.");
foreach (WsdlNS.MessagePart part in message.Parts)
if (part.Name == name)
return part;
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxWsdlMessageDoesNotContainPart3, name, message.Name, message.ServiceDescription.TargetNamespace)));
}
static XmlSchemaElement FindSchemaElement(XmlSchemaSet schemaSet, XmlQualifiedName elementName)
{
XmlSchema schema;
return FindSchemaElement(schemaSet, elementName, out schema);
}
static XmlSchemaElement FindSchemaElement(XmlSchemaSet schemaSet, XmlQualifiedName elementName, out XmlSchema containingSchema)
{
XmlSchemaElement element = null;
containingSchema = null;
foreach (XmlSchema schema in GetSchema(schemaSet, elementName.Namespace))
{
element = (XmlSchemaElement)schema.Elements[elementName];
if (element != null)
{
containingSchema = schema;
break;
}
}
if (element == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxSchemaDoesNotContainElement, elementName.Name, elementName.Namespace)));
return element;
}
static XmlSchemaType FindSchemaType(XmlSchemaSet schemaSet, XmlQualifiedName typeName)
{
if (typeName.Namespace == XmlSchema.Namespace)
return null;
XmlSchema schema;
return FindSchemaType(schemaSet, typeName, out schema);
}
static XmlSchemaType FindSchemaType(XmlSchemaSet schemaSet, XmlQualifiedName typeName, out XmlSchema containingSchema)
{
containingSchema = null;
if (StockSchemas.IsKnownSchema(typeName.Namespace))
return null;
XmlSchemaType type = null;
foreach (XmlSchema schema in GetSchema(schemaSet, typeName.Namespace))
{
type = (XmlSchemaType)schema.SchemaTypes[typeName];
if (type != null)
{
containingSchema = schema;
break;
}
}
if (type == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxSchemaDoesNotContainType, typeName.Name, typeName.Namespace)));
return type;
}
static XmlSchemaSet GatherSchemas(WsdlImporter importer)
{
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.XmlResolver = null;
foreach (WsdlNS.ServiceDescription wsdl in importer.WsdlDocuments)
{
XmlQualifiedName[] wsdlPrefixNsPairs = wsdl.Namespaces.ToArray();
if (wsdl.Types != null && wsdl.Types.Schemas != null)
{
foreach (XmlSchema xsd in wsdl.Types.Schemas)
{
XmlSerializerNamespaces xsdNamespaces = xsd.Namespaces;
XmlQualifiedName[] xsdPrefixNsPairs = xsdNamespaces.ToArray();
Dictionary<string, object> prefixesUsed = new Dictionary<string, object>();
foreach (XmlQualifiedName pair in xsdPrefixNsPairs)
prefixesUsed.Add(pair.Name, null);
foreach (XmlQualifiedName pair in wsdlPrefixNsPairs)
if (!prefixesUsed.ContainsKey(pair.Name))
xsdNamespaces.Add(pair.Name, pair.Namespace);
if (xsd.Items.Count > 0)
{
schemaSet.Add(xsd);
}
else
{
// only add include schemas
foreach (XmlSchemaExternal include in xsd.Includes)
{
if (include.Schema != null)
{
schemaSet.Add(include.Schema);
}
}
}
}
}
}
schemaSet.Add(importer.XmlSchemas);
return schemaSet;
}
// Segregate the schemas containing abstract types from those
// containing regular XML definitions. This is important because
// when you import something returning the ur-type (object), then
// you need to import ALL types/elements within ALL schemas. We
// don't want the RPC-based types leaking over into the XML-based
// element definitions or literal types in the encoded schemas,
// beacase it can cause schema coimpilation falure.
static void CollectEncodedAndLiteralSchemas(WsdlNS.ServiceDescriptionCollection serviceDescriptions, XmlSchemas encodedSchemas, XmlSchemas literalSchemas, XmlSchemaSet allSchemas)
{
XmlSchema wsdl = StockSchemas.CreateWsdl();
XmlSchema soap = StockSchemas.CreateSoap();
XmlSchema soapEncoding = StockSchemas.CreateSoapEncoding();
Hashtable references = new Hashtable();
if (!allSchemas.Contains(wsdl.TargetNamespace))
{
references[soap] = wsdl;
}
if (!allSchemas.Contains(soap.TargetNamespace))
{
references[soap] = soap;
}
if (!allSchemas.Contains(soapEncoding.TargetNamespace))
{
references[soapEncoding] = soapEncoding;
}
foreach (WsdlNS.ServiceDescription description in serviceDescriptions)
{
foreach (WsdlNS.Message message in description.Messages)
{
foreach (WsdlNS.MessagePart part in message.Parts)
{
bool isEncoded;
bool isLiteral;
FindUse(part, out isEncoded, out isLiteral);
if (part.Element != null && !part.Element.IsEmpty)
{
XmlSchemaElement element = FindSchemaElement(allSchemas, part.Element);
if (element != null)
{
AddSchema(element.Parent as XmlSchema, isEncoded, isLiteral, encodedSchemas, literalSchemas, references);
if (element.SchemaTypeName != null && !element.SchemaTypeName.IsEmpty)
{
XmlSchemaType type = FindSchemaType(allSchemas, element.SchemaTypeName);
if (type != null)
{
AddSchema(type.Parent as XmlSchema, isEncoded, isLiteral, encodedSchemas, literalSchemas, references);
}
}
}
}
if (part.Type != null && !part.Type.IsEmpty)
{
XmlSchemaType type = FindSchemaType(allSchemas, part.Type);
if (type != null)
{
AddSchema(type.Parent as XmlSchema, isEncoded, isLiteral, encodedSchemas, literalSchemas, references);
}
}
}
}
}
Hashtable imports;
foreach (XmlSchemas schemas in new XmlSchemas[] { encodedSchemas, literalSchemas })
{
// collect all imports
imports = new Hashtable();
foreach (XmlSchema schema in schemas)
{
AddImport(schema, imports, allSchemas);
}
// make sure we add them to the corresponding schema collections
foreach (XmlSchema schema in imports.Keys)
{
if (references[schema] == null && !schemas.Contains(schema))
{
schemas.Add(schema);
}
}
}
// If a schema was not referenced by either a literal or an encoded message part,
// add it to both collections. There's no way to tell which it should be.
imports = new Hashtable();
foreach (XmlSchema schema in allSchemas.Schemas())
{
if (!encodedSchemas.Contains(schema) && !literalSchemas.Contains(schema))
{
AddImport(schema, imports, allSchemas);
}
}
// make sure we add them to the corresponding schema collections
foreach (XmlSchema schema in imports.Keys)
{
if (references[schema] != null)
continue;
if (!encodedSchemas.Contains(schema))
{
encodedSchemas.Add(schema);
}
if (!literalSchemas.Contains(schema))
{
literalSchemas.Add(schema);
}
}
if (encodedSchemas.Count > 0)
{
foreach (XmlSchema schema in references.Values)
{
encodedSchemas.AddReference(schema);
}
}
if (literalSchemas.Count > 0)
{
foreach (XmlSchema schema in references.Values)
{
literalSchemas.AddReference(schema);
}
}
AddSoapEncodingSchemaIfNeeded(literalSchemas);
}
static void AddSoapEncodingSchemaIfNeeded(XmlSchemas schemas)
{
XmlSchema fakeXsdSchema = StockSchemas.CreateFakeXsdSchema();
foreach (XmlSchema schema in schemas)
{
foreach (object include in schema.Includes)
{
XmlSchemaImport import = include as XmlSchemaImport;
if (import != null && import.Namespace == fakeXsdSchema.TargetNamespace)
{
schemas.Add(fakeXsdSchema);
return;
}
}
}
}
static void AddImport(XmlSchema schema, Hashtable imports, XmlSchemaSet allSchemas)
{
if (schema == null || imports[schema] != null)
return;
imports.Add(schema, schema);
foreach (XmlSchemaExternal external in schema.Includes)
{
if (external is XmlSchemaImport)
{
XmlSchemaImport import = (XmlSchemaImport)external;
foreach (XmlSchema s in allSchemas.Schemas(import.Namespace))
{
AddImport(s, imports, allSchemas);
}
}
}
}
static void AddSchema(XmlSchema schema, bool isEncoded, bool isLiteral, XmlSchemas encodedSchemas, XmlSchemas literalSchemas, Hashtable references)
{
if (schema != null)
{
if (isEncoded && !encodedSchemas.Contains(schema))
{
if (references.Contains(schema))
{
encodedSchemas.AddReference(schema);
}
else
{
encodedSchemas.Add(schema);
}
}
if (isLiteral && !literalSchemas.Contains(schema))
{
if (references.Contains(schema))
{
literalSchemas.AddReference(schema);
}
else
{
literalSchemas.Add(schema);
}
}
}
}
static void FindUse(WsdlNS.MessagePart part, out bool isEncoded, out bool isLiteral)
{
isEncoded = false;
isLiteral = false;
string messageName = part.Message.Name;
WsdlNS.Operation associatedOperation = null;
WsdlNS.ServiceDescription description = part.Message.ServiceDescription;
foreach (WsdlNS.PortType portType in description.PortTypes)
{
foreach (WsdlNS.Operation operation in portType.Operations)
{
foreach (WsdlNS.OperationMessage message in operation.Messages)
{
if (message.Message.Equals(new XmlQualifiedName(part.Message.Name, description.TargetNamespace)))
{
associatedOperation = operation;
FindUse(associatedOperation, description, messageName, ref isEncoded, ref isLiteral);
}
}
}
}
if (associatedOperation == null)
FindUse(null, description, messageName, ref isEncoded, ref isLiteral);
}
static void FindUse(WsdlNS.Operation operation, WsdlNS.ServiceDescription description, string messageName, ref bool isEncoded, ref bool isLiteral)
{
string targetNamespace = description.TargetNamespace;
foreach (WsdlNS.Binding binding in description.Bindings)
{
if (operation != null && !new XmlQualifiedName(operation.PortType.Name, targetNamespace).Equals(binding.Type))
continue;
foreach (WsdlNS.OperationBinding bindingOperation in binding.Operations)
{
if (bindingOperation.Input != null) foreach (object extension in bindingOperation.Input.Extensions)
{
if (operation != null)
{
WsdlNS.SoapBodyBinding body = extension as WsdlNS.SoapBodyBinding;
if (body != null && operation.IsBoundBy(bindingOperation))
{
if (body.Use == WsdlNS.SoapBindingUse.Encoded)
isEncoded = true;
else if (body.Use == WsdlNS.SoapBindingUse.Literal)
isLiteral = true;
}
}
else
{
WsdlNS.SoapHeaderBinding header = extension as WsdlNS.SoapHeaderBinding;
if (header != null && header.Message.Name == messageName)
{
if (header.Use == WsdlNS.SoapBindingUse.Encoded)
isEncoded = true;
else if (header.Use == WsdlNS.SoapBindingUse.Literal)
isLiteral = true;
}
}
}
if (bindingOperation.Output != null) foreach (object extension in bindingOperation.Output.Extensions)
{
if (operation != null)
{
if (operation.IsBoundBy(bindingOperation))
{
WsdlNS.SoapBodyBinding body = extension as WsdlNS.SoapBodyBinding;
if (body != null)
{
if (body.Use == WsdlNS.SoapBindingUse.Encoded)
isEncoded = true;
else if (body.Use == WsdlNS.SoapBindingUse.Literal)
isLiteral = true;
}
else if (extension is WsdlNS.MimeXmlBinding)
isLiteral = true;
}
}
else
{
WsdlNS.SoapHeaderBinding header = extension as WsdlNS.SoapHeaderBinding;
if (header != null && header.Message.Name == messageName)
{
if (header.Use == WsdlNS.SoapBindingUse.Encoded)
isEncoded = true;
else if (header.Use == WsdlNS.SoapBindingUse.Literal)
isLiteral = true;
}
}
}
}
}
}
static string GetLocalElementNamespace(string ns, XmlSchemaElement element, XmlSchemaForm elementFormDefault)
{
XmlSchemaForm elementForm = (element.Form != XmlSchemaForm.None) ? element.Form : elementFormDefault;
if (elementForm != XmlSchemaForm.Qualified)
return string.Empty;
return ns;
}
static IEnumerable GetSchema(XmlSchemaSet schemaSet, string ns)
{
ICollection schemas = schemaSet.Schemas(ns);
if (schemas == null || schemas.Count == 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxSchemaNotFound, ns)));
return schemas;
}
static WsdlNS.SoapBindingStyle GetStyle(WsdlNS.Binding binding)
{
WsdlNS.SoapBindingStyle style = WsdlNS.SoapBindingStyle.Default;
if (binding != null)
{
WsdlNS.SoapBinding soapBinding = binding.Extensions.Find(typeof(WsdlNS.SoapBinding)) as WsdlNS.SoapBinding;
if (soapBinding != null)
style = soapBinding.Style;
}
return style;
}
static OperationFormatStyle GetStyle(WsdlNS.OperationBinding operationBinding)
{
WsdlNS.SoapBindingStyle style = GetStyle(operationBinding.Binding);
if (operationBinding != null)
{
WsdlNS.SoapOperationBinding soapOperationBinding = operationBinding.Extensions.Find(typeof(WsdlNS.SoapOperationBinding)) as WsdlNS.SoapOperationBinding;
if (soapOperationBinding != null)
{
if (soapOperationBinding.Style != WsdlNS.SoapBindingStyle.Default)
style = soapOperationBinding.Style;
}
}
return (style == WsdlNS.SoapBindingStyle.Rpc) ? OperationFormatStyle.Rpc : OperationFormatStyle.Document;
}
static XmlQualifiedName GetTypeName(XmlSchemaElement element)
{
if (element.SchemaType != null)
return XmlQualifiedName.Empty;
else if (IsNullOrEmpty(element.SchemaTypeName))
return AnyType;
else
return element.SchemaTypeName;
}
static bool IsNullOrEmpty(XmlQualifiedName qname)
{
return qname == null || qname.IsEmpty;
}
void TraceFaultCannotBeImported(string faultName, string operationName, string message)
{
AddWarning(SR.GetString(SR.SFxFaultCannotBeImported, faultName, operationName, message));
}
static bool CheckAndAddPart(XmlQualifiedName typeNameFound, XmlQualifiedName typeNameRequired, string name, string ns, Type type, MessageDescription description, bool isReply)
{
if (IsNullOrEmpty(typeNameFound) || typeNameFound != typeNameRequired)
return false;
MessagePartDescription bodyPart = new MessagePartDescription(name, ns);
bodyPart.Type = type;
if (isReply && description.Body.ReturnValue == null)
description.Body.ReturnValue = bodyPart;
else
description.Body.Parts.Add(bodyPart);
return true;
}
static bool CheckPart(XmlQualifiedName typeNameFound, XmlQualifiedName typeNameRequired)
{
return !IsNullOrEmpty(typeNameFound) && typeNameFound == typeNameRequired;
}
static XmlSchemaComplexType GetElementComplexType(XmlQualifiedName elementName, XmlSchemaSet schemaSet, out string ns, out XmlSchemaForm elementFormDefault)
{
XmlSchema schema;
XmlSchemaElement schemaElement = FindSchemaElement(schemaSet, elementName, out schema);
ns = elementName.Namespace;
elementFormDefault = schema.ElementFormDefault;
XmlSchemaType schemaType = null;
if (schemaElement.SchemaType != null)
{
schemaType = schemaElement.SchemaType;
}
else
{
XmlQualifiedName schemaTypeName = GetTypeName(schemaElement);
if (schemaTypeName.Namespace == XmlSchema.Namespace)
return null;
schemaType = FindSchemaType(schemaSet, schemaTypeName, out schema);
ns = schemaTypeName.Namespace;
elementFormDefault = schema.ElementFormDefault;
}
if (schemaType == null)
return null;
return schemaType as XmlSchemaComplexType;
}
static XmlSchemaSequence GetRootSequence(XmlSchemaComplexType complexType)
{
if (complexType == null)
return null;
return complexType.Particle != null ? complexType.Particle as XmlSchemaSequence : null;
}
bool CanImportMessageBinding(WsdlNS.MessageBinding messageBinding, WsdlNS.Message wsdlMessage, OperationFormatStyle style, out bool isEncoded)
{
isEncoded = false;
bool? isMessageEncoded = null;
foreach (object extension in messageBinding.Extensions)
{
bool currentIsEncoded;
WsdlNS.SoapHeaderBinding soapHeaderBinding = extension as WsdlNS.SoapHeaderBinding;
if (soapHeaderBinding != null)
{
if (!ValidWsdl.Check(soapHeaderBinding, messageBinding, AddWarning))
return false;
if (!CanImportMessageHeaderBinding(soapHeaderBinding, wsdlMessage, style, out currentIsEncoded))
return false;
if (isMessageEncoded == null)
isMessageEncoded = currentIsEncoded;
else if (isMessageEncoded.Value != currentIsEncoded)
AddError(SR.GetString(SR.SFxInconsistentWsdlOperationUseInBindingExtensions, messageBinding.OperationBinding.Name, messageBinding.OperationBinding.Binding.Name));
}
else
{
WsdlNS.SoapBodyBinding soapBodyBinding = extension as WsdlNS.SoapBodyBinding;
if (soapBodyBinding != null)
{
if (!CanImportMessageBodyBinding(soapBodyBinding, style, out currentIsEncoded))
return false;
if (isMessageEncoded == null)
isMessageEncoded = currentIsEncoded;
else if (isMessageEncoded.Value != currentIsEncoded)
AddError(SR.GetString(SR.SFxInconsistentWsdlOperationUseInBindingExtensions, messageBinding.OperationBinding.Name, messageBinding.OperationBinding.Binding.Name));
string[] messageParts = soapBodyBinding.Parts;
if (messageParts == null)
{
messageParts = new string[wsdlMessage.Parts.Count];
for (int i = 0; i < messageParts.Length; i++)
messageParts[i] = wsdlMessage.Parts[i].Name;
}
IList<string> bodyPartsFromBindings;
bool isFirstBinding = false;
if (!BodyPartsTable.TryGetValue(wsdlMessage, out bodyPartsFromBindings))
{
bodyPartsFromBindings = new List<string>();
BodyPartsTable.Add(wsdlMessage, bodyPartsFromBindings);
isFirstBinding = true;
}
foreach (string partName in messageParts)
{
if (string.IsNullOrEmpty(partName))
continue;
if (isFirstBinding)
bodyPartsFromBindings.Add(partName);
else if (!bodyPartsFromBindings.Contains(partName))
{
AddError(SR.GetString(SR.SFxInconsistentBindingBodyParts, messageBinding.OperationBinding.Name, messageBinding.OperationBinding.Binding.Name, partName));
bodyPartsFromBindings.Add(partName);
}
}
}
}
}
if (isMessageEncoded != null)
isEncoded = isMessageEncoded.Value;
return true;
}
private bool CanImportFaultBinding(WsdlNS.FaultBinding faultBinding, OperationFormatStyle style, out bool isFaultEncoded)
{
bool? isEncoded = null;
bool currentIsEncoded;
foreach (object extension in faultBinding.Extensions)
{
XmlElement soapFaultBindingRaw = extension as XmlElement;
if (SoapHelper.IsSoapFaultBinding(soapFaultBindingRaw))
{
currentIsEncoded = SoapHelper.IsEncoded(soapFaultBindingRaw);
}
else
{
WsdlNS.SoapFaultBinding soapFaultBinding = extension as WsdlNS.SoapFaultBinding;
if (soapFaultBinding == null)
continue;
if (!ValidWsdl.Check(soapFaultBinding, faultBinding, AddWarning))
continue;
currentIsEncoded = (soapFaultBinding.Use == System.Web.Services.Description.SoapBindingUse.Encoded);
}
if (isEncoded == null)
isEncoded = currentIsEncoded;
else if (isEncoded.Value != currentIsEncoded)
AddError(SR.GetString(SR.SFxInconsistentWsdlOperationUseInBindingExtensions, faultBinding.OperationBinding.Name, faultBinding.OperationBinding.Binding.Name));
}
isFaultEncoded = isEncoded ?? false;
return this.CurrentSchemaImporter.CanImportStyleAndUse(style, isFaultEncoded);
}
bool CanImportMessageBodyBinding(WsdlNS.SoapBodyBinding bodyBinding, OperationFormatStyle style, out bool isEncoded)
{
isEncoded = (bodyBinding.Use == WsdlNS.SoapBindingUse.Encoded);
return CurrentSchemaImporter.CanImportStyleAndUse(style, isEncoded);
}
bool CanImportMessageHeaderBinding(WsdlNS.SoapHeaderBinding headerBinding, WsdlNS.Message wsdlMessage, OperationFormatStyle style, out bool isEncoded)
{
isEncoded = (headerBinding.Use == WsdlNS.SoapBindingUse.Encoded);
WsdlNS.Message wsdlHeaderMessage = wsdlMessage.ServiceDescription.ServiceDescriptions.GetMessage(headerBinding.Message);
WsdlNS.MessagePart part = FindPartByName(wsdlHeaderMessage, headerBinding.Part);
OperationFormatStyle headerStyle;
if (!CurrentSchemaImporter.CanImportMessagePart(part, out headerStyle))
return false;
if (headerStyle != style)
AddError(SR.GetString(SR.SFxInconsistentWsdlOperationStyleInHeader, part.Name, headerStyle, style));
return CurrentSchemaImporter.CanImportStyleAndUse(style, isEncoded);
}
void ImportMessageBinding(WsdlNS.MessageBinding messageBinding, WsdlNS.Message wsdlMessage, MessageDescription description, OperationFormatStyle style, bool isEncoded)
{
WsdlNS.OperationMessage wsdlOperationMessage = contractContext.GetOperationMessage(description);
foreach (object extension in messageBinding.Extensions)
{
WsdlNS.SoapHeaderBinding soapHeaderBinding = extension as WsdlNS.SoapHeaderBinding;
if (soapHeaderBinding != null)
{
ImportMessageHeaderBinding(soapHeaderBinding, wsdlMessage, description, style, isEncoded, messageBinding.OperationBinding.Name);
}
else
{
WsdlNS.SoapBodyBinding soapBodyBinding = extension as WsdlNS.SoapBodyBinding;
if (soapBodyBinding != null)
{
ImportMessageBodyBinding(soapBodyBinding, wsdlMessage, description, style, isEncoded, messageBinding.OperationBinding.Name);
}
}
}
}
void ImportMessageBodyBinding(WsdlNS.SoapBodyBinding bodyBinding, WsdlNS.Message wsdlMessage, MessageDescription description, OperationFormatStyle style, bool isEncoded, string operationName)
{
if (style == OperationFormatStyle.Rpc && bodyBinding.Namespace != null)
description.Body.WrapperNamespace = bodyBinding.Namespace;
this.CurrentSchemaImporter.ValidateStyleAndUse(style, isEncoded, operationName);
}
void ImportMessageHeaderBinding(WsdlNS.SoapHeaderBinding headerBinding, WsdlNS.Message wsdlMessage, MessageDescription description, OperationFormatStyle style, bool isEncoded, string operationName)
{
WsdlNS.Message wsdlHeaderMessage = wsdlMessage.ServiceDescription.ServiceDescriptions.GetMessage(headerBinding.Message);
WsdlNS.MessagePart part = FindPartByName(wsdlHeaderMessage, headerBinding.Part);
if (!description.Headers.Contains(this.CurrentSchemaImporter.GetPartName(part)))
{
description.Headers.Add((MessageHeaderDescription)schemaImporter.ImportMessagePart(part, true/*isHeader*/, isEncoded));
this.CurrentSchemaImporter.ValidateStyleAndUse(style, isEncoded, operationName);
}
}
internal abstract class SchemaImporter
{
readonly protected XmlSchemaSet schemaSet;
readonly protected WsdlImporter importer;
internal SchemaImporter(WsdlImporter importer)
{
this.importer = importer;
this.schemaSet = GatherSchemas(importer);
}
internal XmlQualifiedName GetPartName(WsdlNS.MessagePart part)
{
if (!IsNullOrEmpty(part.Element))
return part.Element;
if (!IsNullOrEmpty(part.Type))
return new XmlQualifiedName(part.Name, String.Empty);
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxWsdlPartMustHaveElementOrType, part.Name, part.Message.Name, part.Message.Namespaces)));
}
internal bool CanImportMessagePart(WsdlNS.MessagePart part, out OperationFormatStyle style)
{
style = OperationFormatStyle.Document;
if (!IsNullOrEmpty(part.Element))
return CanImportElement(FindSchemaElement(this.schemaSet, part.Element));
if (!IsNullOrEmpty(part.Type))
{
style = OperationFormatStyle.Rpc;
return CanImportType(part.Type);
}
return false;
}
internal MessagePartDescription ImportMessagePart(WsdlNS.MessagePart part, bool isHeader, bool isEncoded)
{
MessagePartDescription bodyPart = null;
if (!IsNullOrEmpty(part.Element))
return ImportParameterElement(part.Element, isHeader, false/*isMultiple*/);
if (!IsNullOrEmpty(part.Type))
{
bodyPart = isHeader ? (MessagePartDescription)new MessageHeaderDescription(part.Name, String.Empty) : new MessagePartDescription(part.Name, String.Empty);
bodyPart.BaseType = ImportType(bodyPart, part.Type, isEncoded);
return bodyPart;
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxWsdlPartMustHaveElementOrType, part.Name, part.Message.Name, part.Message.Namespaces)));
}
internal MessagePartDescription ImportParameterElement(XmlQualifiedName elementName, bool isHeader, bool isMultiple)
{
return ImportParameterElement(FindSchemaElement(this.schemaSet, elementName), elementName.Namespace, isHeader, isMultiple);
}
internal MessagePartDescription ImportParameterElement(XmlSchemaElement element, string ns, bool isHeader, bool isMultiple)
{
if (element.MaxOccurs > 1)
isMultiple = true;
if (!IsNullOrEmpty(element.RefName))
return ImportParameterElement(element.RefName, isHeader, isMultiple);
MessagePartDescription part = isHeader ? (MessagePartDescription)new MessageHeaderDescription(element.Name, ns) : new MessagePartDescription(element.Name, ns);
part.Multiple = isMultiple;
part.BaseType = ImportElement(part, element, false/*isEncoded*/);
return part;
}
internal virtual bool CanImportFault(XmlSchemaElement detailElement, XmlQualifiedName detailElementTypeName)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException());
}
internal virtual CodeTypeReference ImportFaultElement(XmlQualifiedName elementName, XmlSchemaElement element, bool isEncoded)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException());
}
internal virtual CodeTypeReference ImportFaultType(XmlQualifiedName elementName, XmlQualifiedName typeName, bool isEncoded)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException());
}
internal virtual void SetOperationSupportFaults(OperationDescription operation, bool supportFaults)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException());
}
internal abstract void PreprocessSchema();
internal abstract void PostprocessSchema(bool used);
internal abstract bool CanImportStyleAndUse(OperationFormatStyle style, bool isEncoded);
internal abstract void ValidateStyleAndUse(OperationFormatStyle style, bool isEncoded, string operationName);
internal abstract IOperationBehavior GetOperationGenerator();
internal abstract bool CanImportType(XmlQualifiedName typeName);
internal abstract string ImportType(MessagePartDescription part, XmlQualifiedName typeName, bool isEncoded);
internal abstract bool CanImportElement(XmlSchemaElement element);
internal abstract string ImportElement(MessagePartDescription part, XmlSchemaElement element, bool isEncoded);
internal abstract bool CanImportWrapperElement(XmlQualifiedName elementName);
internal abstract MessagePartDescription[] ImportWrapperElement(XmlQualifiedName elementName);
internal abstract void SetOperationStyle(OperationDescription operation, OperationFormatStyle style);
internal abstract bool GetOperationIsEncoded(OperationDescription operation);
internal abstract void SetOperationIsEncoded(OperationDescription operation, bool isEncoded);
internal abstract string GetFormatName();
}
internal class DataContractSerializerSchemaImporter : SchemaImporter
{
// Same string used in DataContractSet of System.Runtime.Serialization.dll
internal const String FailedReferenceTypeExceptionKey = "System.Runtime.Serialization.FailedReferenceType";
DataContractSerializerOperationGenerator DataContractSerializerOperationGenerator;
ValidationEventHandler compileValidationEventHandler;
Collection<MetadataConversionError> errors;
public DataContractSerializerSchemaImporter(WsdlImporter importer)
: base(importer)
{
DataContractSerializerOperationGenerator = new DataContractSerializerOperationGenerator(DataContractImporter.CodeCompileUnit);
}
XsdDataContractImporter DataContractImporter
{
get
{
object dataContractImporter;
if (!importer.State.TryGetValue(typeof(XsdDataContractImporter), out dataContractImporter))
{
object compileUnit;
if (!importer.State.TryGetValue(typeof(CodeCompileUnit), out compileUnit))
{
compileUnit = new CodeCompileUnit();
importer.State.Add(typeof(CodeCompileUnit), compileUnit);
}
dataContractImporter = new XsdDataContractImporter((CodeCompileUnit)compileUnit);
importer.State.Add(typeof(XsdDataContractImporter), dataContractImporter);
}
return (XsdDataContractImporter)dataContractImporter;
}
}
internal override bool CanImportElement(XmlSchemaElement element)
{
if (!element.IsNillable && !SchemaHelper.IsElementValueType(element))
return false;
return DataContractImporter.CanImport(schemaSet, element);
}
internal override bool CanImportType(XmlQualifiedName typeName)
{
return DataContractImporter.CanImport(schemaSet, typeName);
}
internal override bool CanImportWrapperElement(XmlQualifiedName elementName)
{
string ns;
XmlSchemaForm elementFormDefault;
XmlSchemaComplexType complexType = GetElementComplexType(elementName, schemaSet, out ns, out elementFormDefault);
if (complexType == null)
return false;
if (complexType.Particle == null)
return true;
XmlSchemaSequence rootSequence = complexType.Particle as XmlSchemaSequence;
if (rootSequence == null)
return false;
for (int i = 0; i < rootSequence.Items.Count; i++)
{
XmlSchemaElement element = rootSequence.Items[i] as XmlSchemaElement;
if (element == null)
return false;
if (!IsNullOrEmpty(element.RefName))
element = FindSchemaElement(this.schemaSet, element.RefName);
if (element.MaxOccurs > 1)
return false;
if (!DataContractImporter.CanImport(schemaSet, element))
return false;
}
return true;
}
internal override bool CanImportFault(XmlSchemaElement detailElement, XmlQualifiedName detailElementTypeName)
{
DataContractSerializerSchemaImporter faultImporter = DataContractSerializerSchemaImporter.Get(importer);
if (IsNullOrEmpty(detailElementTypeName))
return faultImporter.CanImportFaultElement(detailElement);
else
return faultImporter.CanImportFaultType(detailElementTypeName);
}
internal static DataContractSerializerSchemaImporter Get(WsdlImporter importer)
{
Type type = typeof(DataContractSerializerSchemaImporter);
object schemaImporter;
if (importer.State.ContainsKey(type))
schemaImporter = importer.State[type];
else
{
schemaImporter = new DataContractSerializerSchemaImporter(importer);
importer.State.Add(type, schemaImporter);
}
return (DataContractSerializerSchemaImporter)schemaImporter;
}
internal override MessagePartDescription[] ImportWrapperElement(XmlQualifiedName elementName)
{
string ns;
XmlSchemaForm elementFormDefault;
XmlSchemaComplexType complexType = GetElementComplexType(elementName, schemaSet, out ns, out elementFormDefault);
if (complexType == null)
return null;
if (complexType.Particle == null)
return new MessagePartDescription[0];
XmlSchemaSequence rootSequence = complexType.Particle as XmlSchemaSequence;
if (rootSequence == null)
return null;
MessagePartDescription[] parts = new MessagePartDescription[rootSequence.Items.Count];
for (int i = 0; i < rootSequence.Items.Count; i++)
{
XmlSchemaElement localElement = rootSequence.Items[i] as XmlSchemaElement;
if (localElement == null)
return null;
parts[i] = ImportParameterElement(localElement, GetLocalElementNamespace(ns, localElement, elementFormDefault), false/*isHeader*/, false/*isMultiple*/);
if (parts[i] == null)
return null;
}
return parts;
}
internal override string ImportType(MessagePartDescription part, XmlQualifiedName typeName, bool isEncoded)
{
if (isEncoded)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDataContractSerializerDoesNotSupportEncoded, part.Name)));
DataContractImporter.Import(schemaSet, typeName);
CodeTypeReference typeRef = DataContractImporter.GetCodeTypeReference(typeName);
ICollection<CodeTypeReference> knownTypeRefs = DataContractImporter.GetKnownTypeReferences(typeName);
DataContractSerializerOperationGenerator.Add(part, typeRef, knownTypeRefs, false/*IsNonNillableReferenceType*/);
if (typeRef.ArrayRank == 0)
return typeRef.BaseType;
else
return typeRef.BaseType + "[]";
}
internal static bool TryGetFailedReferenceType(Exception ex, out Type failedReferenceType)
{
if (null == ex)
throw new ArgumentNullException("ex");
if (ex.Data.Contains(FailedReferenceTypeExceptionKey))
{
failedReferenceType = ex.Data[FailedReferenceTypeExceptionKey] as Type;
if (null != failedReferenceType)
{
return true;
}
}
failedReferenceType = null;
return false;
}
internal override string ImportElement(MessagePartDescription part, XmlSchemaElement element, bool isEncoded)
{
if (part.Multiple)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDataContractSerializerDoesNotSupportBareArray, part.Name)));
XmlQualifiedName typeName = null;
Type failedReferenceType;
while (null == typeName)
{
try
{
typeName = DataContractImporter.Import(schemaSet, element);
break;
}
catch (InvalidDataContractException ex)
{
if (TryGetFailedReferenceType(ex, out failedReferenceType))
{
DataContractImporter.Options.ReferencedTypes.Remove(failedReferenceType);
continue;
}
throw;
}
catch (InvalidOperationException ex)
{
if (TryGetFailedReferenceType(ex, out failedReferenceType))
{
DataContractImporter.Options.ReferencedTypes.Remove(failedReferenceType);
continue;
}
throw;
}
}
CodeTypeReference typeRef = DataContractImporter.GetCodeTypeReference(typeName, element);
ICollection<CodeTypeReference> knownTypeRefs = DataContractImporter.GetKnownTypeReferences(typeName);
DataContractSerializerOperationGenerator.Add(part, typeRef, knownTypeRefs, !element.IsNillable && !IsValueType(typeName));
if (typeRef.ArrayRank == 0)
return typeRef.BaseType;
else
return typeRef.BaseType + "[]";
}
private bool IsValueType(XmlQualifiedName typeName)
{
XmlSchemaElement element = new XmlSchemaElement();
element.IsNillable = true;
CodeTypeReference typeRef = DataContractImporter.GetCodeTypeReference(typeName, element);
return typeRef.BaseType == typeof(Nullable<>).FullName;
}
int SetImportXmlType(bool value)
{
if (DataContractImporter.Options == null)
{
DataContractImporter.Options = new ImportOptions();
DataContractImporter.Options.ImportXmlType = value;
return -1;
}
if (DataContractImporter.Options.ImportXmlType != value)
{
DataContractImporter.Options.ImportXmlType = value;
return 0;
}
return 1;
}
void RestoreImportXmlType(int oldValue)
{
if (oldValue == 1)
return;
if (oldValue == 0)
{
DataContractImporter.Options.ImportXmlType = !DataContractImporter.Options.ImportXmlType;
return;
}
DataContractImporter.Options = null;
}
internal override CodeTypeReference ImportFaultElement(XmlQualifiedName elementName, XmlSchemaElement element, bool isEncoded)
{
int oldValue = SetImportXmlType(true);
try
{
XmlQualifiedName typeName = DataContractImporter.Import(schemaSet, element);
return DataContractImporter.GetCodeTypeReference(typeName, element);
}
finally
{
RestoreImportXmlType(oldValue);
}
}
internal bool CanImportFaultElement(XmlSchemaElement element)
{
int oldValue = SetImportXmlType(false);
try
{
return DataContractImporter.CanImport(schemaSet, element);
}
finally
{
RestoreImportXmlType(oldValue);
}
}
internal override CodeTypeReference ImportFaultType(XmlQualifiedName elementName, XmlQualifiedName typeName, bool isEncoded)
{
int oldValue = SetImportXmlType(true);
try
{
DataContractImporter.Import(schemaSet, typeName);
return DataContractImporter.GetCodeTypeReference(typeName);
}
finally
{
RestoreImportXmlType(oldValue);
}
}
internal bool CanImportFaultType(XmlQualifiedName typeName)
{
int oldValue = SetImportXmlType(false);
try
{
return DataContractImporter.CanImport(schemaSet, typeName);
}
finally
{
RestoreImportXmlType(oldValue);
}
}
internal override void PreprocessSchema()
{
errors = new Collection<MetadataConversionError>();
compileValidationEventHandler = new ValidationEventHandler(delegate(object sender, ValidationEventArgs args)
{
SchemaHelper.HandleSchemaValidationError(sender, args, errors);
}
);
schemaSet.ValidationEventHandler += compileValidationEventHandler;
}
internal override void PostprocessSchema(bool used)
{
if (used && errors != null)
{
foreach (MetadataConversionError error in errors)
importer.Errors.Add(error);
errors.Clear();
}
schemaSet.ValidationEventHandler -= compileValidationEventHandler;
}
internal override IOperationBehavior GetOperationGenerator()
{
return DataContractSerializerOperationGenerator;
}
internal override bool CanImportStyleAndUse(OperationFormatStyle style, bool isEncoded)
{
return !isEncoded;
}
internal override void ValidateStyleAndUse(OperationFormatStyle style, bool isEncoded, string operationName)
{
if (isEncoded)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDataContractSerializerDoesNotSupportEncoded, operationName)));
}
internal override void SetOperationStyle(OperationDescription operation, OperationFormatStyle style)
{
DataContractSerializerOperationBehavior operationBehavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (operationBehavior == null)
{
operationBehavior = new DataContractSerializerOperationBehavior(operation, new DataContractFormatAttribute());
operation.Behaviors.Add(operationBehavior);
}
operationBehavior.DataContractFormatAttribute.Style = style;
}
internal override bool GetOperationIsEncoded(OperationDescription operation)
{
return false;
}
internal override void SetOperationIsEncoded(OperationDescription operation, bool isEncoded)
{
if (isEncoded)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDataContractSerializerDoesNotSupportEncoded, operation.Name)));
}
internal override void SetOperationSupportFaults(OperationDescription operation, bool supportFaults)
{
return;
}
internal override string GetFormatName()
{
return "DataContract";
}
}
internal class XmlSerializerSchemaImporter : SchemaImporter
{
XmlSerializerOperationGenerator xmlSerializerOperationGenerator;
XmlSchemaImporter xmlImporter;
SoapSchemaImporter soapImporter;
CodeDomProvider codeProvider;
XmlSchemas literalSchemas, encodedSchemas;
public XmlSerializerSchemaImporter(WsdlImporter importer)
: base(importer)
{
XmlSerializerImportOptions options;
if (importer.State.ContainsKey(typeof(XmlSerializerImportOptions)))
{
options = (XmlSerializerImportOptions)importer.State[typeof(XmlSerializerImportOptions)];
}
else
{
object compileUnit;
if (!importer.State.TryGetValue(typeof(CodeCompileUnit), out compileUnit))
{
compileUnit = new CodeCompileUnit();
importer.State.Add(typeof(CodeCompileUnit), compileUnit);
}
options = new XmlSerializerImportOptions((CodeCompileUnit)compileUnit);
importer.State.Add(typeof(XmlSerializerImportOptions), options);
}
WsdlNS.WebReferenceOptions webReferenceOptions = options.WebReferenceOptions;
codeProvider = options.CodeProvider;
encodedSchemas = new XmlSchemas();
literalSchemas = new XmlSchemas();
CollectEncodedAndLiteralSchemas(importer.WsdlDocuments, encodedSchemas, literalSchemas, schemaSet);
CodeIdentifiers codeIdentifiers = new CodeIdentifiers();
//SchemaImporter.ctor is not thread safe: MB49115, VSWhidbey580396
lock (schemaImporterLock)
{
xmlImporter = new XmlSchemaImporter(literalSchemas, webReferenceOptions.CodeGenerationOptions, options.CodeProvider, new ImportContext(codeIdentifiers, false));
}
if (webReferenceOptions != null)
{
foreach (string extTypeName in webReferenceOptions.SchemaImporterExtensions)
{
xmlImporter.Extensions.Add(extTypeName, Type.GetType(extTypeName, true /*throwOnError*/));
}
}
//SchemaImporter.ctor is not thread safe: MB49115, VSWhidbey580396
lock (schemaImporterLock)
{
soapImporter = new SoapSchemaImporter(encodedSchemas, webReferenceOptions.CodeGenerationOptions, options.CodeProvider, new ImportContext(codeIdentifiers, false));
}
xmlSerializerOperationGenerator = new XmlSerializerOperationGenerator(options);
}
internal override bool CanImportElement(XmlSchemaElement element)
{
return true;
}
internal override bool CanImportType(XmlQualifiedName typeName)
{
return true;
}
internal override bool CanImportWrapperElement(XmlQualifiedName elementName)
{
string ns;
XmlSchemaForm elementFormDefault;
XmlSchemaComplexType complexType = GetElementComplexType(elementName, schemaSet, out ns, out elementFormDefault);
if (complexType == null)
return false;
return true;
}
internal override bool CanImportFault(XmlSchemaElement detailElement, XmlQualifiedName detailElementTypeName)
{
return true;
}
internal static XmlSerializerSchemaImporter Get(WsdlImporter importer)
{
Type type = typeof(XmlSerializerSchemaImporter);
object schemaImporter;
if (importer.State.ContainsKey(type))
schemaImporter = importer.State[type];
else
{
schemaImporter = new XmlSerializerSchemaImporter(importer);
importer.State.Add(type, schemaImporter);
}
return (XmlSerializerSchemaImporter)schemaImporter;
}
internal override MessagePartDescription[] ImportWrapperElement(XmlQualifiedName elementName)
{
XmlMembersMapping membersMapping = xmlImporter.ImportMembersMapping(elementName);
ArrayList parts = new ArrayList();
for (int i = 0; i < membersMapping.Count; i++)
{
XmlMemberMapping member = membersMapping[i];
string xmlName = NamingHelper.XmlName(member.MemberName);
MessagePartDescription part = new MessagePartDescription(xmlName, member.Namespace == null ? string.Empty : member.Namespace);
xmlSerializerOperationGenerator.Add(part, member, membersMapping, false/*isEncoded*/);
part.BaseType = member.GenerateTypeName(codeProvider);
parts.Add(part);
}
return (MessagePartDescription[])parts.ToArray(typeof(MessagePartDescription));
}
internal override CodeTypeReference ImportFaultElement(XmlQualifiedName elementName, XmlSchemaElement element, bool isEncoded)
{
if (isEncoded)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDocEncodedFaultNotSupported)));
XmlMembersMapping membersMapping = xmlImporter.ImportMembersMapping(new XmlQualifiedName[] { elementName });
this.xmlSerializerOperationGenerator.XmlExporter.ExportMembersMapping(membersMapping);
return new CodeTypeReference(this.xmlSerializerOperationGenerator.GetTypeName(membersMapping[0]));
}
internal override CodeTypeReference ImportFaultType(XmlQualifiedName elementName, XmlQualifiedName typeName, bool isEncoded)
{
XmlName memberName = new XmlName(elementName.Name, true /*isEncoded*/);
string memberNs = elementName.Namespace;
XmlMembersMapping membersMapping;
SoapSchemaMember schemaMember = new SoapSchemaMember();
schemaMember.MemberName = memberName.EncodedName;
schemaMember.MemberType = typeName;
if (isEncoded)
{
membersMapping = soapImporter.ImportMembersMapping(memberName.DecodedName, memberNs, new SoapSchemaMember[] { schemaMember });
this.xmlSerializerOperationGenerator.SoapExporter.ExportMembersMapping(membersMapping);
}
else
{
membersMapping = xmlImporter.ImportMembersMapping(memberName.DecodedName, memberNs, new SoapSchemaMember[] { schemaMember });
this.xmlSerializerOperationGenerator.XmlExporter.ExportMembersMapping(membersMapping);
}
return new CodeTypeReference(this.xmlSerializerOperationGenerator.GetTypeName(membersMapping[0]));
}
internal override string ImportType(MessagePartDescription part, XmlQualifiedName typeName, bool isEncoded)
{
XmlName memberName = new XmlName(part.Name, true /*isEncoded*/);
string memberNs = part.Namespace;
XmlMembersMapping membersMapping;
SoapSchemaMember schemaMember = new SoapSchemaMember();
schemaMember.MemberName = memberName.EncodedName;
schemaMember.MemberType = typeName;
if (isEncoded)
membersMapping = soapImporter.ImportMembersMapping(memberName.DecodedName, memberNs, new SoapSchemaMember[] { schemaMember });
else
membersMapping = xmlImporter.ImportMembersMapping(memberName.DecodedName, memberNs, new SoapSchemaMember[] { schemaMember });
return AddPartType(part, membersMapping, isEncoded);
}
internal override string ImportElement(MessagePartDescription part, XmlSchemaElement element, bool isEncoded)
{
if (isEncoded)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDocEncodedNotSupported, part.Name)));
XmlMembersMapping membersMapping = xmlImporter.ImportMembersMapping(new XmlQualifiedName[] { element.QualifiedName });
return AddPartType(part, membersMapping, isEncoded);
}
private string AddPartType(MessagePartDescription part, XmlMembersMapping membersMapping, bool isEncoded)
{
xmlSerializerOperationGenerator.Add(part, membersMapping[0], membersMapping, isEncoded);
return membersMapping[0].GenerateTypeName(codeProvider);
}
internal override void PreprocessSchema()
{
XmlSchema wsdl = StockSchemas.CreateWsdl();
XmlSchema soap = StockSchemas.CreateSoap();
XmlSchema soapEncoding = StockSchemas.CreateSoapEncoding();
XmlSchema fakeXsdSchema = StockSchemas.CreateFakeXsdSchema();
XmlSchema fakeXmlSchema = StockSchemas.CreateFakeXmlSchema();
schemaSet.Add(wsdl);
schemaSet.Add(soap);
schemaSet.Add(soapEncoding);
schemaSet.Add(fakeXsdSchema);
schemaSet.Add(fakeXmlSchema);
SchemaHelper.Compile(schemaSet, importer.Errors);
schemaSet.Remove(wsdl);
schemaSet.Remove(soap);
schemaSet.Remove(soapEncoding);
schemaSet.Remove(fakeXsdSchema);
schemaSet.Remove(fakeXmlSchema);
}
internal override void PostprocessSchema(bool used)
{
}
internal override IOperationBehavior GetOperationGenerator()
{
return xmlSerializerOperationGenerator;
}
internal override bool CanImportStyleAndUse(OperationFormatStyle style, bool isEncoded)
{
// Intentionally return true in all cases. Warning will be generated later if there are multiple bindings and one is doc-encoded.
return true;
}
internal override void ValidateStyleAndUse(OperationFormatStyle style, bool isEncoded, string operationName)
{
if (isEncoded && style != OperationFormatStyle.Rpc)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxDocEncodedNotSupported, operationName)));
}
internal static XmlSerializerFormatAttribute GetFormatAttribute(OperationDescription operation, bool createNew)
{
XmlSerializerOperationBehavior operationBehavior = operation.Behaviors.Find<XmlSerializerOperationBehavior>();
if (operationBehavior != null)
return operationBehavior.XmlSerializerFormatAttribute;
if (!createNew)
return null;
operationBehavior = new XmlSerializerOperationBehavior(operation);
operation.Behaviors.Add(operationBehavior);
return operationBehavior.XmlSerializerFormatAttribute;
}
internal override void SetOperationStyle(OperationDescription operation, OperationFormatStyle style)
{
XmlSerializerFormatAttribute operationAttribute = GetFormatAttribute(operation, true/*createNew*/);
operationAttribute.Style = style;
}
internal override bool GetOperationIsEncoded(OperationDescription operation)
{
XmlSerializerFormatAttribute operationAttribute = GetFormatAttribute(operation, false /*createNew*/);
if (operationAttribute == null)
return TypeLoader.DefaultXmlSerializerFormatAttribute.IsEncoded;
return operationAttribute.IsEncoded;
}
internal override void SetOperationIsEncoded(OperationDescription operation, bool isEncoded)
{
XmlSerializerFormatAttribute operationAttribute = GetFormatAttribute(operation, true/*createNew*/);
operationAttribute.IsEncoded = isEncoded;
}
internal override void SetOperationSupportFaults(OperationDescription operation, bool supportFaults)
{
XmlSerializerFormatAttribute operationAttribute = GetFormatAttribute(operation, true/*createNew*/);
operationAttribute.SupportFaults = supportFaults;
}
internal override string GetFormatName()
{
return "XmlSerializer";
}
}
class OperationInfo
{
OperationFormatStyle style;
bool isEncoded;
bool areAllMessagesWrapped;
internal OperationInfo(OperationFormatStyle style, bool isEncoded, bool areAllMessagesWrapped)
{
this.style = style;
this.isEncoded = isEncoded;
this.areAllMessagesWrapped = areAllMessagesWrapped;
}
internal OperationFormatStyle Style { get { return style; } }
internal bool IsEncoded { get { return isEncoded; } }
internal bool AreAllMessagesWrapped { get { return areAllMessagesWrapped; } }
}
}
internal delegate void WsdlWarningHandler(string warning);
static class ValidWsdl
{
internal static bool Check(WsdlNS.SoapHeaderBinding soapHeaderBinding, WsdlNS.MessageBinding messageBinding, WsdlWarningHandler warningHandler)
{
if (soapHeaderBinding.Message == null || soapHeaderBinding.Message.IsEmpty)
{
string reason = SR.GetString(SR.XsdMissingRequiredAttribute1, "message");
string warning = SR.GetString(SR.IgnoreSoapHeaderBinding3, messageBinding.OperationBinding.Name, messageBinding.OperationBinding.Binding.ServiceDescription.TargetNamespace, reason);
warningHandler(warning);
return false;
}
if (string.IsNullOrEmpty(soapHeaderBinding.Part))
{
string reason = SR.GetString(SR.XsdMissingRequiredAttribute1, "part");
string warning = SR.GetString(SR.IgnoreSoapHeaderBinding3, messageBinding.OperationBinding.Name, messageBinding.OperationBinding.Binding.ServiceDescription.TargetNamespace, reason);
warningHandler(warning);
return false;
}
return true;
}
internal static bool Check(WsdlNS.SoapFaultBinding soapFaultBinding, WsdlNS.FaultBinding faultBinding, WsdlWarningHandler warningHandler)
{
if (string.IsNullOrEmpty(soapFaultBinding.Name))
{
string reason = SR.GetString(SR.XsdMissingRequiredAttribute1, "name");
string warning = SR.GetString(SR.IgnoreSoapFaultBinding3, faultBinding.OperationBinding.Name, faultBinding.OperationBinding.Binding.ServiceDescription.TargetNamespace, reason);
warningHandler(warning);
return false;
}
return true;
}
internal static bool Check(WsdlNS.MessagePart part, WsdlNS.Message message, WsdlWarningHandler warningHandler)
{
// check required name attribute, do not check NCName validity
if (string.IsNullOrEmpty(part.Name))
{
string reason = SR.GetString(SR.XsdMissingRequiredAttribute1, "name");
string warning = SR.GetString(SR.IgnoreMessagePart3, message.Name, message.ServiceDescription.TargetNamespace, reason);
warningHandler(warning);
return false;
}
return true;
}
}
}
|