Welcome to mirror list, hosted at ThFree Co, Russian Federation.

VSWCFServiceContractGenerator.cs « WCFModel « Compilation « System.Web.Extensions « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 45b862eda3a4a3bdb819532581a2764e61f27264 (plain)
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
#region Copyright (c) Microsoft Corporation
/// <copyright company='Microsoft Corporation'>
///    Copyright (c) Microsoft Corporation. All Rights Reserved.
///    Information Contained Herein is Proprietary and Confidential.
/// </copyright>
#endregion

using System.CodeDom;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.Xml;
using System.Xml.Schema;
using System.Security.Permissions;
using System.Linq;

#if WEB_EXTENSIONS_CODE
using System.Security;
using System.Web.Resources;
#else
using Microsoft.VSDesigner.WCF.Resources;
#endif

///
/// The VSWCFServiceContractGenerator takes a SvcMap file and it's associated metadata,
/// imports the metadata using a WsdlImporter and System.ServiceModel.ServiceContractGenerator
/// that are configured according to the options set in the SvcMap file
/// 
using Debug = System.Diagnostics.Debug;
using System.Diagnostics.CodeAnalysis;

#if WEB_EXTENSIONS_CODE
namespace System.Web.Compilation.WCFModel
#else
namespace Microsoft.VSDesigner.WCFModel
#endif
{
    /// <summary>
    /// Proxy and configuration generator
    /// </summary>
#if WEB_EXTENSIONS_CODE
    [PermissionSet(SecurityAction.InheritanceDemand, Name="FullTrust")]    
    [SecurityCritical]
    internal class VSWCFServiceContractGenerator
#else
    // We only check for CLS compliant for the public version of this class since the 
    // compiler will complain about CLS compliance not being checked for non-public classes
    [CLSCompliant(true)]
    [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
    [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
    public class VSWCFServiceContractGenerator
#endif
    {

        #region Private backing fields
        private const string VB_LANGUAGE_NAME = "vb";

        /// <summary>
        /// Collection to hold all bindings generated by this generator
        /// </summary>
        private IEnumerable<System.ServiceModel.Channels.Binding> bindingCollection;

        /// <summary>
        /// Collection to hold all contracts generated by this generator
        /// </summary>
        private IEnumerable<ContractDescription> contractCollection;

        /// <summary>
        /// Collection to hold all endpoints generated by this generator
        /// </summary>
        private List<ServiceEndpoint> serviceEndpointList;

        /// <summary>
        /// Map from service endpoint to the channel endpoint that was actually imported
        /// </summary>
        private Dictionary<ServiceEndpoint, ChannelEndpointElement> serviceEndpointToChannelEndpointElementMap;

        /// <summary>
        /// List of contract types generate by this generator
        /// </summary>
        private List<GeneratedContractType> proxyGeneratedContractTypes;

        /// <summary>
        /// The target compile unit that contains the proxy and data contracts
        /// </summary>
        private CodeCompileUnit targetCompileUnit;

        /// <summary>
        /// Configuration object that we inserterd bindings and endpoints from the
        /// current service into. May be Null/Nothing.
        /// </summary>
        private System.Configuration.Configuration targetConfiguration;

        /// <summary>
        /// Errors encountered while generating the proxy
        /// </summary>
        private IEnumerable<ProxyGenerationError> proxyGenerationErrors;

        /// <summary>
        /// Errors encountered while importing the metadata..
        /// </summary>
        private IList<ProxyGenerationError> importErrors;

        /// <summary>
        /// Helper property that is added to Out parameters for VB
        /// </summary>
        private static CodeAttributeDeclaration outAttribute;

        /// <summary>
        /// version number for 3.5 framework
        /// </summary>
        private const int FRAMEWORK_VERSION_35 = 0x30005;

        /// <summary>
        /// list of types which are new in the 3.5 framework. 
        /// </summary>
        private static Type[] unsupportedTypesInFramework30 = new Type[] {
            typeof(DateTimeOffset),
        };


        #endregion

        #region Public read-only properties

        /// <summary>
        /// The collection of bindings generated by this generator
        /// </summary>
        /// <value></value>
        /// <remarks>
        /// </remarks>
        public IEnumerable<System.ServiceModel.Channels.Binding> BindingCollection
        {
            get
            {
                System.Diagnostics.Debug.Assert(bindingCollection != null);
                return bindingCollection;
            }
        }

        /// <summary>
        /// The collection of generated contract types
        /// </summary>
        public IEnumerable<GeneratedContractType> ProxyGeneratedContractTypes
        {
            get
            {
                System.Diagnostics.Debug.Assert(proxyGeneratedContractTypes != null);
                return proxyGeneratedContractTypes;
            }
        }

        /// <summary>
        /// The collection of errors encountered while generating the
        /// proxy. For errors related to the metadata import, use the
        /// ImportErrors property
        /// </summary>
        public IEnumerable<ProxyGenerationError> ProxyGenerationErrors
        {
            get
            {
                System.Diagnostics.Debug.Assert(proxyGenerationErrors != null);
                return proxyGenerationErrors;
            }
        }

        /// <summary>
        /// The collection of errors encountered while importing metadata.
        /// For errors related to the proxy and config generation, use the
        /// ProxyGenerationErrors property
        /// </summary>
        public IEnumerable<ProxyGenerationError> ImportErrors
        {
            get
            {
                System.Diagnostics.Debug.Assert(importErrors != null);
                return importErrors;
            }
        }

        /// <summary>
        /// The collection of contracts imported by this generator
        /// </summary>
        /// <value></value>
        /// <remarks></remarks>
        public IEnumerable<ContractDescription> ContractCollection
        {
            get
            {
                System.Diagnostics.Debug.Assert(contractCollection != null);
                return contractCollection;
            }
        }

        /// <summary>
        /// Collection of Endpoints in the service model generated by
        /// this generator
        /// </summary>
        /// <value></value>
        /// <remarks></remarks>
        public IEnumerable<ServiceEndpoint> EndpointCollection
        {
            get
            {
                System.Diagnostics.Debug.Assert(serviceEndpointList != null);
                return serviceEndpointList;
            }
        }

        /// <summary>
        /// Map from service endpoints to its corresponding channel endpoint configuration
        /// element
        /// </summary>
        public Dictionary<ServiceEndpoint, ChannelEndpointElement> EndpointMap
        {
            get
            {
                System.Diagnostics.Debug.Assert(serviceEndpointToChannelEndpointElementMap != null);
                return serviceEndpointToChannelEndpointElementMap;
            }
        }

        /// <summary>
        /// The configuratin into which we inject the bindings and endpoints. May be null/Nothing
        /// if no target configuration was provided.
        /// </summary>
        public System.Configuration.Configuration TargetConfiguration
        {
            get
            {
                // Note: it is valid for this to be NULL. Caller beware!
                return targetConfiguration;
            }
        }

        /// <summary>
        /// CodeCompileUnit containing the generated data contracts, service contracts
        /// and WCF client.
        /// </summary>
        public CodeCompileUnit TargetCompileUnit
        {
            get
            {
                System.Diagnostics.Debug.Assert(targetCompileUnit != null);
                return targetCompileUnit;
            }
        }

        #endregion

        /// <summary>
        /// Cached instance of an Out attribute that we use to patch up
        /// the codegen for VB projects (the VB code generates out parameters
        /// as ByRef)
        /// </summary>
        private static CodeAttributeDeclaration OutAttribute
        {
            get
            {
                if (outAttribute == null)
                {
                    outAttribute = new CodeAttributeDeclaration(typeof(System.Runtime.InteropServices.OutAttribute).FullName);
                }
                return outAttribute;
            }
        }

        /// <summary>
        /// protected constructor to block creating instance directly.
        /// </summary>
        /// <param name="importErrors"></param>
        /// <param name="targetCompileUnit"></param>
        /// <param name="targetConfiguration">May be null</param>
        /// <param name="bindingCollection"></param>
        /// <param name="contractCollection"></param>
        /// <param name="serviceEndpointList"></param>
        /// <param name="serviceEndpointToChannelEndpointElementMap"></param>
        /// <param name="proxyGeneratedContractTypes"></param>
        /// <param name="proxyGenerationErrors"></param>
        protected VSWCFServiceContractGenerator(
                List<ProxyGenerationError> importErrors,
                CodeCompileUnit targetCompileUnit,
                System.Configuration.Configuration targetConfiguration,
                IEnumerable<System.ServiceModel.Channels.Binding> bindingCollection,
                IEnumerable<ContractDescription> contractCollection,
                List<ServiceEndpoint> serviceEndpointList,
                Dictionary<ServiceEndpoint, ChannelEndpointElement> serviceEndpointToChannelEndpointElementMap,
                List<GeneratedContractType> proxyGeneratedContractTypes,
                IEnumerable<ProxyGenerationError> proxyGenerationErrors)
        {
            if (importErrors == null) throw new ArgumentNullException("importErrors");
            if (targetCompileUnit == null) throw new ArgumentNullException("targetCompileUnit");
            // Please note - target configuration may be NULL
            if (bindingCollection == null) throw new ArgumentNullException("bindingCollection");
            if (contractCollection == null) throw new ArgumentNullException("contractCollection");
            if (serviceEndpointList == null) throw new ArgumentNullException("serviceEndpointList");
            if (serviceEndpointToChannelEndpointElementMap == null) throw new ArgumentNullException("serviceEndpointToChannelEndpointElementMap");
            if (proxyGeneratedContractTypes == null) throw new ArgumentNullException("proxyGeneratedContractTypes");
            if (proxyGenerationErrors == null) throw new ArgumentNullException("proxyGenerationErrors");

            this.importErrors = importErrors;
            this.targetCompileUnit = targetCompileUnit;
            this.targetConfiguration = targetConfiguration;
            this.bindingCollection = bindingCollection;
            this.contractCollection = contractCollection;
            this.serviceEndpointList = serviceEndpointList;
            this.serviceEndpointToChannelEndpointElementMap = serviceEndpointToChannelEndpointElementMap;
            this.proxyGeneratedContractTypes = proxyGeneratedContractTypes;
            this.proxyGenerationErrors = proxyGenerationErrors;
        }

        /// <summary>
        /// Factory method: generate code and return the resulting VSWCFServiceContractGenerator. 
        /// </summary>
        /// <param name="svcMapFile">
        /// The SvcMapFile that lists the metadata and generation options for the service reference.
        /// </param>
        /// <param name="toolConfiguration">
        /// Configuration from which we are going to pick up WSDL and policy importer extensions as well
        /// as custom MEX bindings for metadata download. May be Null/Nothing.
        /// </param>
        /// <param name="codeDomProvider">
        /// CodeDom provider that is to be used to generate the client code.
        /// </param>
        /// <param name="proxyNamespace">
        /// CLR namespace in which to generate the client code.
        /// </param>
        /// <param name="targetConfiguration">
        /// The configuration into which we will put bindings/endpoints for this service
        /// reference. May be Null/Nothing.
        /// </param>
        /// <param name="configurationNamespace">
        /// The namespace that is to be used in configuration for this service reference.
        /// </param>
        /// <param name="serviceProviderForImportExtensions">
        /// Service provider that we'll pass on to import extensions that accept our site:ing
        /// mechanism
        /// </param>
        /// <param name="typeLoader">
        /// Type loader that can be used to find reference assemblies and/or resolve shared service and
        /// data contract types.
        /// </param>
        /// <param name="targetFrameworkVersion">
        /// The target framework version number. The higher 16 bits contains the major version number, and low 16 bits contains minor version number.
        /// </param>
        /// <param name="typedDataSetSchemaImporterExtension">
        /// Schema importer extension to be used for typed datasets.
        /// </param>
        /// <returns>
        /// A VSWCFServiceContractGenerator instance that contains the result of the generation. To get
        /// hold of the generated information, you can query it's properties.
        /// </returns>
        public static VSWCFServiceContractGenerator GenerateCodeAndConfiguration(SvcMapFile svcMapFile,
                                             System.Configuration.Configuration toolConfiguration,
                                             System.CodeDom.Compiler.CodeDomProvider codeDomProvider,
                                             string proxyNamespace,
                                             System.Configuration.Configuration targetConfiguration,
                                             string configurationNamespace,
                                             IServiceProvider serviceProviderForImportExtensions,
                                             IContractGeneratorReferenceTypeLoader typeLoader,
                                             int targetFrameworkVersion,
                                             System.Type typedDataSetSchemaImporterExtension)
        {
            if (svcMapFile == null) throw new ArgumentNullException("svcMapFile");
            if (codeDomProvider == null) throw new ArgumentNullException("codeDomProvider");
            if (typedDataSetSchemaImporterExtension == null) throw new ArgumentNullException("typedDataSetSchemaImporterExtension");

            List<ProxyGenerationError> importErrors = new List<ProxyGenerationError>();
            List<ProxyGenerationError> proxyGenerationErrors = new List<ProxyGenerationError>();

            CodeCompileUnit targetCompileUnit = new CodeCompileUnit();

            WsdlImporter wsdlImporter = CreateWsdlImporter(svcMapFile,
                                                           toolConfiguration,
                                                           targetCompileUnit,
                                                           codeDomProvider,
                                                           proxyNamespace,
                                                           serviceProviderForImportExtensions,
                                                           typeLoader,
                                                           targetFrameworkVersion,
                                                           importErrors,
                                                           typedDataSetSchemaImporterExtension);

            ServiceContractGenerator contractGenerator = CreateContractGenerator(svcMapFile.ClientOptions,
                                                                                wsdlImporter,
                                                                                targetCompileUnit,
                                                                                proxyNamespace,
                                                                                targetConfiguration,
                                                                                typeLoader,
                                                                                targetFrameworkVersion,
                                                                                importErrors);

            try
            {
                List<ServiceEndpoint> serviceEndpointList = new List<ServiceEndpoint>();
                IEnumerable<System.ServiceModel.Channels.Binding> bindingCollection;
                IEnumerable<ContractDescription> contractCollection;

                ImportWCFModel(wsdlImporter,
                                 targetCompileUnit,
                                 importErrors,
                                 out serviceEndpointList,
                                 out bindingCollection,
                                 out contractCollection);

                Dictionary<ServiceEndpoint, ChannelEndpointElement> serviceEndpointToChannelEndpointElementMap;
                List<GeneratedContractType> proxyGeneratedContractTypes;

                GenerateProxy(wsdlImporter,
                              contractGenerator,
                              targetCompileUnit,
                              proxyNamespace,
                              configurationNamespace,
                              contractCollection,
                              bindingCollection,
                              serviceEndpointList,
                              proxyGenerationErrors,
                              out serviceEndpointToChannelEndpointElementMap,
                              out proxyGeneratedContractTypes);

                if (IsVBCodeDomProvider(codeDomProvider))
                {
                    PatchOutParametersInVB(targetCompileUnit);
                }

                return new VSWCFServiceContractGenerator(importErrors,
                                                         targetCompileUnit,
                                                         targetConfiguration,
                                                         bindingCollection,
                                                         contractCollection,
                                                         serviceEndpointList,
                                                         serviceEndpointToChannelEndpointElementMap,
                                                         proxyGeneratedContractTypes,
                                                         proxyGenerationErrors);
            }
            catch (Exception ex)
            {
                // fatal error... (workaround for bug #135242)
                // We want to convert fatal error exception to a normal code generator error message,
                // so the user could find information from pervious errors to find KB topic.
                proxyGenerationErrors.Add(new ProxyGenerationError(
                                    ProxyGenerationError.GeneratorState.GenerateCode,
                                    String.Empty,
                                    ex,
                                    false));

                return new VSWCFServiceContractGenerator(importErrors,
                                                        new CodeCompileUnit(),
                                                        targetConfiguration,
                                                        new List<System.ServiceModel.Channels.Binding>(),
                                                        new List<ContractDescription>(),
                                                        new List<ServiceEndpoint>(),
                                                        new Dictionary<ServiceEndpoint, ChannelEndpointElement>(),
                                                        new List<GeneratedContractType>(),
                                                        proxyGenerationErrors);
            }

        }


        /// <summary>
        /// Instantiate and configure a ServiceContractGenerator to be used for code and config
        /// generation.
        /// </summary>
        /// <param name="proxyOptions">
        /// Options set in the SvcMap file to control the code/config generation.
        /// </param>
        /// <param name="wsdlImporter">
        /// The WsdlImporter that is to be used to import the metadata for this service reference.
        /// </param>
        /// <param name="targetCompileUnit">
        /// Compile unit into which we will generate the client code
        /// </param>
        /// <param name="proxyNamespace">
        /// The CLR namespace into which we will generate the client code.
        /// </param>
        /// <param name="targetConfiguration">
        /// Optional configuration into which we will generate the endpoints/bindings corresponding
        /// to this service reference. May be Null/Nothing, in which case we will not generate config.
        /// </param>
        /// <param name="typeLoader">
        /// Type loader that can be used to find reference assemblies and/or resolve shared service and
        /// data contract types.
        /// </param>
        /// <param name="targetFrameworkVersion">
        /// The target framework version number. The higher 16 bits contains the major version number, and low 16 bits contains minor version number.
        /// </param>
        /// <param name="importErrors">
        /// The list into which we will add any errors while importing the metadata.
        /// </param>
        /// <returns></returns>
        protected static ServiceContractGenerator CreateContractGenerator(ClientOptions proxyOptions,
                                            WsdlImporter wsdlImporter,
                                            CodeCompileUnit targetCompileUnit,
                                            string proxyNamespace,
                                            System.Configuration.Configuration targetConfiguration,
                                            IContractGeneratorReferenceTypeLoader typeLoader,
                                            int targetFrameworkVersion,
                                            IList<ProxyGenerationError> importErrors)
        {
            ServiceContractGenerator contractGenerator = new ServiceContractGenerator(targetCompileUnit, targetConfiguration);

            // We want to generate all types into the proxy namespace CLR namespace. We indicate
            // this by adding a namespace mapping from all XML namespaces ("*") to the namespace
            // the caller told us to generate the client code in.
            contractGenerator.NamespaceMappings.Add("*", proxyNamespace);

            if (proxyOptions.GenerateInternalTypes)
            {
                contractGenerator.Options |= ServiceContractGenerationOptions.InternalTypes;
            }
            else
            {
                contractGenerator.Options &= ~ServiceContractGenerationOptions.InternalTypes;
            }

            // Make sure at most one of the async options will be set: AsynchronousMethods | TaskBasedAsynchronousMethod.
            contractGenerator.Options &= ~ServiceContractGenerationOptions.AsynchronousMethods &
                                         ~ServiceContractGenerationOptions.EventBasedAsynchronousMethods &
                                         ~ServiceContractGenerationOptions.TaskBasedAsynchronousMethod;

            if (proxyOptions.GenerateTaskBasedAsynchronousMethod)
            {
                contractGenerator.Options |= ServiceContractGenerationOptions.TaskBasedAsynchronousMethod;
            }
            else if (proxyOptions.GenerateAsynchronousMethods)
            {
                contractGenerator.Options |= ServiceContractGenerationOptions.AsynchronousMethods;
                if (targetFrameworkVersion >= FRAMEWORK_VERSION_35)
                {
                    contractGenerator.Options |= ServiceContractGenerationOptions.EventBasedAsynchronousMethods;
                }
            }

            if (proxyOptions.GenerateMessageContracts)
            {
                contractGenerator.Options |= ServiceContractGenerationOptions.TypedMessages;
            }
            else
            {
                contractGenerator.Options &= ~ServiceContractGenerationOptions.TypedMessages;
            }

            // If we have a type loader, we tell the contract generator and wsdl importer about
            // all shared types and assemblies that we've specified in the proxy options...
            if (typeLoader != null)
            {
                foreach (ContractMapping mapping in proxyOptions.ServiceContractMappingList)
                {
                    try
                    {
                        Type sharedType = typeLoader.LoadType(mapping.TypeName);

                        // verify that the type is shareable - if not, we generate an error...
                        if (!IsTypeShareable(sharedType))
                        {
                            importErrors.Add(
                                    new ProxyGenerationError(
                                        ProxyGenerationError.GeneratorState.GenerateCode,
                                        String.Empty,
                                        new FormatException(String.Format(CultureInfo.CurrentCulture, WCFModelStrings.ReferenceGroup_SharedTypeMustBePublic, mapping.TypeName)))
                                );
                            continue;
                        }

                        // Get a contract description corresponding to the type we wanted to share
                        ContractDescription contract = ContractDescription.GetContract(sharedType);

                        if (!String.Equals(mapping.Name, contract.Name, StringComparison.Ordinal) ||
                                !String.Equals(mapping.TargetNamespace, contract.Namespace, StringComparison.Ordinal))
                        {
                            // mismatch
                            importErrors.Add(
                                    new ProxyGenerationError(
                                        ProxyGenerationError.GeneratorState.GenerateCode,
                                        String.Empty,
                                        new FormatException(String.Format(CultureInfo.CurrentCulture, WCFModelStrings.ReferenceGroup_ServiceContractMappingMissMatch, mapping.TypeName, contract.Namespace, contract.Name, mapping.TargetNamespace, mapping.Name)))
                                );
                        }

                        XmlQualifiedName qname = new XmlQualifiedName(contract.Name, contract.Namespace);
                        wsdlImporter.KnownContracts.Add(qname, contract);
                        contractGenerator.ReferencedTypes.Add(contract, sharedType);
                    }
                    catch (Exception ex)
                    {
                        importErrors.Add(new ProxyGenerationError(
                                        ProxyGenerationError.GeneratorState.GenerateCode,
                                        String.Empty,
                                        ex));
                    }
                }
            }

            foreach (NamespaceMapping namespaceMapping in proxyOptions.NamespaceMappingList)
            {
                contractGenerator.NamespaceMappings.Add(namespaceMapping.TargetNamespace, namespaceMapping.ClrNamespace);
            }

            return contractGenerator;
        }

        /// <summary>
        /// Generate Proxy Code and (if available) configuration
        /// </summary>
        /// <param name="contractGenerator">The contract generator to use</param>
        /// <param name="targetCompileUnit">Compile unit into which we should generate the client code</param>
        /// <param name="proxyNamespace">CLR namespace into which we should generate the client code</param>
        /// <param name="configurationNamespace">Namespace to use in configuration</param>
        /// <param name="contractCollection">The contracts for which we should generate code and optionally config</param>
        /// <param name="bindingCollection">The bindings we should generate config for</param>
        /// <param name="serviceEndpointList">The endpoints we should generate config for</param>
        /// <param name="proxyGenerationErrors">A list of errors encountered while generating the client</param>
        /// <param name="serviceEndpointToChannelEndpointElementMap">Map from service endpoint to the configuration element for the endpoint</param>
        /// <param name="proxyGeneratedContractTypes">The generated contract types</param>
        protected static void GenerateProxy(WsdlImporter importer,
                                            ServiceContractGenerator contractGenerator,
                                            CodeCompileUnit targetCompileUnit,
                                            string proxyNamespace,
                                            string configurationNamespace,
                                            IEnumerable<ContractDescription> contractCollection,
                                            IEnumerable<System.ServiceModel.Channels.Binding> bindingCollection,
                                            List<ServiceEndpoint> serviceEndpointList,
                                            IList<ProxyGenerationError> proxyGenerationErrors,
                                            out Dictionary<ServiceEndpoint, ChannelEndpointElement> serviceEndpointToChannelEndpointElementMap,
                                            out List<GeneratedContractType> proxyGeneratedContractTypes)
        {
            // Parameter checking
            if (serviceEndpointList == null) throw new ArgumentNullException("serviceEndpointList");
            if (bindingCollection == null) throw new ArgumentNullException("bindingCollection");
            if (contractCollection == null) throw new ArgumentNullException("contractCollection");
            if (proxyGenerationErrors == null) throw new ArgumentNullException("proxyGenerationErrors");

            proxyGeneratedContractTypes = new List<GeneratedContractType>();
            serviceEndpointToChannelEndpointElementMap = new Dictionary<ServiceEndpoint, ChannelEndpointElement>();

            try
            {
                HttpBindingExtension httpBindingEx = importer.WsdlImportExtensions.Find<HttpBindingExtension>();

                foreach (ContractDescription contract in contractCollection)
                {
                    if (httpBindingEx == null || !httpBindingEx.IsHttpBindingContract(contract) || serviceEndpointList.Any(endpoint => endpoint.Contract == contract))
                    {
                        CodeTypeReference typeReference = contractGenerator.GenerateServiceContractType(contract);
                        if (typeReference != null)
                        {
                            // keep the (targetNamespace, portType) -> CLR type map table...

                            string baseType = typeReference.BaseType;

                            GeneratedContractType generatedType = new GeneratedContractType(contract.Namespace, contract.Name, baseType, baseType);
                            proxyGeneratedContractTypes.Add(generatedType);
                        }
                    }
                }

                // We should only import the Binding & Endpoints if there is a configuration storage...
                if (contractGenerator.Configuration != null)
                {
                    foreach (ServiceEndpoint endpoint in serviceEndpointList)
                    {
                        ChannelEndpointElement endpointElement = null;
                        contractGenerator.GenerateServiceEndpoint(endpoint, out endpointElement);
                        serviceEndpointToChannelEndpointElementMap[endpoint] = endpointElement;
                    }

                    foreach (System.ServiceModel.Channels.Binding bindingDescription in bindingCollection)
                    {
                        string bindingSectionName = null;
                        string bindingConfigurationName = null;
                        // Generate binding will change the state of the contractGenerator... 
                        contractGenerator.GenerateBinding(bindingDescription, out bindingSectionName, out bindingConfigurationName);
                    }

                }

                PatchConfigurationName(proxyNamespace,
                                       configurationNamespace,
                                       proxyGeneratedContractTypes,
                                       serviceEndpointToChannelEndpointElementMap.Values,
                                       targetCompileUnit);
            }
            finally
            {
                foreach (MetadataConversionError error in contractGenerator.Errors)
                {
                    proxyGenerationErrors.Add(new ProxyGenerationError(error));
                }
            }
        }

        /// <summary>
        /// Create appropriate XmlSerializerImportOptions for the generator
        /// </summary>
        /// <param name="proxyOptions">Options for the code/config generation</param>
        /// <param name="targetCompileUnit">Compile unit we are going to generate the client code in</param>
        /// <param name="codeDomProvider">CodeDom provider for the language we are using</param>
        /// <param name="proxyNamespace">CLR namespace we'll put the client code in</param>
        /// <returns></returns>
        protected static XmlSerializerImportOptions CreateXmlSerializerImportOptions(
                                ClientOptions proxyOptions,
                                CodeCompileUnit targetCompileUnit,
                                System.CodeDom.Compiler.CodeDomProvider codeDomProvider,
                                string proxyNamespace,
                                System.Type typedDataSetSchemaImporterExtension)
        {
            System.ServiceModel.Channels.XmlSerializerImportOptions xmlSerializerOptions = new XmlSerializerImportOptions(targetCompileUnit);
            System.Web.Services.Description.WebReferenceOptions webReferenceOptions = new System.Web.Services.Description.WebReferenceOptions();

            webReferenceOptions.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties | System.Xml.Serialization.CodeGenerationOptions.GenerateOrder;

            if (proxyOptions.EnableDataBinding)
            {
                webReferenceOptions.CodeGenerationOptions |= System.Xml.Serialization.CodeGenerationOptions.EnableDataBinding;
            }

            webReferenceOptions.SchemaImporterExtensions.Add(typedDataSetSchemaImporterExtension.AssemblyQualifiedName);
            webReferenceOptions.SchemaImporterExtensions.Add(typeof(System.Data.DataSetSchemaImporterExtension).AssemblyQualifiedName);

            /* 

















*/

            xmlSerializerOptions.WebReferenceOptions = webReferenceOptions;
            xmlSerializerOptions.CodeProvider = codeDomProvider;

            xmlSerializerOptions.ClrNamespace = proxyNamespace;

            return xmlSerializerOptions;
        }

        /// <summary>
        /// Create an appropriate XsdDataContractImporter for the generator
        /// </summary>
        /// <param name="proxyOptions">Code/config generation options to use</param>
        /// <param name="targetCompileUnit">CodeCompileUnit into which we will generate the client code</param>
        /// <param name="codeDomProvider">CodeDomProvider for the language we will use to generate the client</param>
        /// <param name="proxyNamespace">CLR namespace in which the client code will be generated</param>
        /// <param name="typeLoader">Service used to resolve type/assembly names (strings) to actual Types and Assemblies</param>
        /// <param name="targetFrameworkVersion">Targetted Framework version number</param>
        /// <param name="importErrors">List of errors encountered. New errors will be added to this list</param>
        /// <returns></returns>
        protected static XsdDataContractImporter CreateDataContractImporter(
                ClientOptions proxyOptions,
                CodeCompileUnit targetCompileUnit,
                System.CodeDom.Compiler.CodeDomProvider codeDomProvider,
                string proxyNamespace,
                IContractGeneratorReferenceTypeLoader typeLoader,
                int targetFrameworkVersion,
                IList<ProxyGenerationError> importErrors)
        {
            System.Runtime.Serialization.XsdDataContractImporter xsdDataContractImporter = new System.Runtime.Serialization.XsdDataContractImporter(targetCompileUnit);
            System.Runtime.Serialization.ImportOptions options = new System.Runtime.Serialization.ImportOptions();

            options.CodeProvider = codeDomProvider;

            // We specify that we want to generate all types from all XML namespaces into
            // our proxy namespace. By default, each XML namespace get's its own CLR namespace
            options.Namespaces.Add("*", proxyNamespace);
            options.GenerateInternal = proxyOptions.GenerateInternalTypes;
            options.GenerateSerializable = proxyOptions.GenerateSerializableTypes;
            options.EnableDataBinding = proxyOptions.EnableDataBinding;
            options.ImportXmlType = proxyOptions.ImportXmlTypes;

            if (typeLoader != null)
            {
                IEnumerable<Type> referencedTypes = LoadSharedDataContractTypes(proxyOptions, typeLoader, targetFrameworkVersion, importErrors);
                if (referencedTypes != null)
                {
                    foreach (Type sharedType in referencedTypes)
                    {
                        options.ReferencedTypes.Add(sharedType);
                    }
                }

                IEnumerable<Type> referencedCollectionTypes = LoadSharedCollectionTypes(proxyOptions, typeLoader, importErrors);
                if (referencedCollectionTypes != null)
                {
                    foreach (Type collectionType in referencedCollectionTypes)
                    {
                        options.ReferencedCollectionTypes.Add(collectionType);
                    }
                }

            }

            foreach (NamespaceMapping namespaceMapping in proxyOptions.NamespaceMappingList)
            {
                options.Namespaces.Add(namespaceMapping.TargetNamespace, namespaceMapping.ClrNamespace);
            }

            xsdDataContractImporter.Options = options;

            return xsdDataContractImporter;
        }

        /// <summary>
        /// Load DataContract types which could be used in the code generator (shared types)
        /// </summary>
        /// <param name="proxyOptions">Options controlling the generation</param>
        /// <param name="typeLoader">Type loader to resolve referenced assemblies and types</param>
        /// <param name="targetFrameworkVersion">Targetted Framework version number</param>
        /// <param name="importErrors">Errors encountered while loading the shared data contracts</param>
        /// <return>
        /// A list of CLR types from referenced assemblies and/or specific types that we want to share
        /// </return>
        /// <remarks></remarks>
        [SuppressMessage("Microsoft.Usage", "CA2301:EmbeddableTypesInContainersRule", MessageId = "sharedTypeTable", Justification = "This is used within VS to get the types from reference assemblies i.e., the reference assembly for a given assembly but not across assemblies - so com interop is not an issue.")]
        protected static IEnumerable<Type> LoadSharedDataContractTypes(
                ClientOptions proxyOptions, IContractGeneratorReferenceTypeLoader typeLoader, int targetFrameworkVersion, IList<ProxyGenerationError> importErrors)
        {
            if (typeLoader == null) throw new ArgumentNullException("typeLoader");

            // the value in sharedTypeTable is why we add this type in the shared type list. 
            // if it is only added because it is in the referenced assembly, the value will be null, otherwise it contains the entry in the type inclusion list
            // if the type is also in the exclusion list, we will report an error if the type is comming from the inclusion list, but no error if it comes from a referenced assembly only.
            Dictionary<Type, ReferencedType> sharedTypeTable = new Dictionary<Type, ReferencedType>();

            // load all types in referencedAssemblies
            IEnumerable<Assembly> referencedAssemblies = LoadReferenedAssemblies(proxyOptions, typeLoader, importErrors);
            if (referencedAssemblies != null)
            {
                foreach (Assembly referencedAssembly in referencedAssemblies)
                {
                    var typeLoader2 = typeLoader as IContractGeneratorReferenceTypeLoader2;
                    if (typeLoader2 != null)
                    {
                        foreach (Type sharedType in typeLoader2.LoadExportedTypes(referencedAssembly))
                        {
                            sharedTypeTable.Add(sharedType, null);
                        }
                    }
                    else
                    {
                        // Fall back to the original approach using IContractGeneratorReferenceTypeLoader.LoadType().
                        foreach (Type typeInAssembly in referencedAssembly.GetExportedTypes())
                        {
                            try
                            {
                                // Do multi-targeting check by calling IContractGeneratorReferenceTypeLoader.LoadType().                            
                                if (typeLoader.LoadType(typeInAssembly.FullName) != null)
                                {
                                    sharedTypeTable.Add(typeInAssembly, null);
                                }
                            }
                            catch (NotSupportedException)
                            {
                                // NotSupportedException is thrown by multi-targeting check. It's normal if some types not existing in the current FX.
                                // So we can safely ---- it.
                            }
                            catch (Exception ex)
                            {
                                // fail to load one type in an assembly: warning message
                                importErrors.Add(
                                    new ProxyGenerationError(
                                        ProxyGenerationError.GeneratorState.GenerateCode,
                                        String.Empty,
                                        ex,
                                        true));
                            }
                        }
                    }
                }
            }

            // load types in DataContractTypeList
            foreach (ReferencedType referencedType in proxyOptions.ReferencedDataContractTypeList)
            {
                try
                {
                    Type sharedType = typeLoader.LoadType(referencedType.TypeName);

                    // verify...
                    if (!IsTypeShareable(sharedType))
                    {
                        importErrors.Add(
                                new ProxyGenerationError(
                                    ProxyGenerationError.GeneratorState.GenerateCode,
                                    String.Empty,
                                    new FormatException(String.Format(CultureInfo.CurrentCulture, WCFModelStrings.ReferenceGroup_SharedTypeMustBePublic, referencedType.TypeName)))
                            );
                        continue;
                    }

                    sharedTypeTable[sharedType] = referencedType;
                }
                catch (Exception ex)
                {
                    importErrors.Add(new ProxyGenerationError(
                                    ProxyGenerationError.GeneratorState.GenerateCode,
                                    String.Empty,
                                    ex));
                }
            }

            // remove excluded types
            foreach (ReferencedType excludedType in proxyOptions.ExcludedTypeList)
            {
                try
                {
                    Type sharedType = typeLoader.LoadType(excludedType.TypeName);

                    if (sharedTypeTable.ContainsKey(sharedType))
                    {
                        if (sharedTypeTable[sharedType] != null)
                        {
                            // error message
                            importErrors.Add(new ProxyGenerationError(
                                            ProxyGenerationError.GeneratorState.GenerateCode,
                                            String.Empty,
                                            new Exception(String.Format(CultureInfo.CurrentCulture, WCFModelStrings.ReferenceGroup_DataContractExcludedAndIncluded, excludedType.TypeName))));
                        }
                        sharedTypeTable.Remove(sharedType);
                    }
                }
                catch (Exception ex)
                {
                    // waring message for excludedTypes
                    importErrors.Add(new ProxyGenerationError(
                                    ProxyGenerationError.GeneratorState.GenerateCode,
                                    String.Empty,
                                    ex,
                                    true));
                }
            }

            // remove unsupported types
            foreach (Type unsupportedType in GetUnsupportedTypes(targetFrameworkVersion))
            {
                sharedTypeTable.Remove(unsupportedType);
            }

            return sharedTypeTable.Keys;
        }

        /// <summary>
        /// Get list of types which are not supported in the targetted framework.
        /// </summary>
        /// <param name="targetFrameworkVersion">Targetted Framework version number</param>
        /// <return></return>
        /// <remarks></remarks>
        private static IEnumerable<Type> GetUnsupportedTypes(int targetFrameworkVersion)
        {

            if (targetFrameworkVersion < FRAMEWORK_VERSION_35)
            {
                // NOTE: do we need load those types with typeLoader?
                return unsupportedTypesInFramework30;
            }

            return new Type[0];
        }

        /// <summary>
        /// Ensure that the ConfigurationName attribute on service contracts and the channel endpoint elements all agree on the 
        /// name of the service contract.
        /// We want to avoid having root/default namespace values persisted in config, since that would require us
        /// to update config whenever the default/root namespace changes, so we make sure that we exclude it
        /// from the ConfigurationName attribute and the channel endpoint element we generate.
        /// 
        /// For VB, the root namespace is not actually present in the generated code, so we typically don't have to
        /// do anything (the configuration and proxy namespaces are equal) but for C#, we need to strip out the
        /// default namespace.
        /// </summary>
        /// <param name="proxyNamespace">
        /// CLR namespace into which we will generate the service in the CodeCompileUnit
        /// </param>
        /// <param name="configNamespace">
        /// The namespace that we expect to use in configuration.
        /// </param>
        /// <param name="generatedContracts">
        /// The contracts that we have generated
        /// </param>
        /// <param name="endpoints">
        /// All channel endpoints we have generated
        /// </param>
        /// <param name="targetCompileUnit">
        /// The compile unit into which we generated the client
        /// </param>
        private static void PatchConfigurationName(
                                    string proxyNamespace,
                                    string configNamespace,
                                    IEnumerable<GeneratedContractType> generatedContracts,
                                    IEnumerable<ChannelEndpointElement> endpoints,
                                    CodeCompileUnit targetCompileUnit
            )
        {

            // Since the name has to match between configuration and the name we put in the ConfigurationName 
            // attribute in code, we may have some patching to do - but only if the proxy namespace is not equal
            // to the configuration namespace...
            if (configNamespace != null && !configNamespace.Equals(proxyNamespace, StringComparison.Ordinal))
            {
                string proxyNamespaceHead = MakePeriodTerminatedNamespacePrefix(proxyNamespace);
                string configNamespaceHead = MakePeriodTerminatedNamespacePrefix(configNamespace);

                // We need to fix up the configuration name for all generated contracts...
                foreach (GeneratedContractType contract in generatedContracts)
                {
                    contract.ConfigurationName = ReplaceNamespace(proxyNamespaceHead, configNamespaceHead, contract.ConfigurationName);
                }

                // ..and we need to fix up all elements in config...
                foreach (ChannelEndpointElement endpoint in endpoints)
                {
                    endpoint.Contract = ReplaceNamespace(proxyNamespaceHead, configNamespaceHead, endpoint.Contract);
                }

                // ...and all ConfigurationName values in service contract attributes in the generated code as well...
                PatchConfigurationNameInServiceContractAttribute(targetCompileUnit, proxyNamespace, configNamespace);
            }
        }

        /// <summary>
        /// If the type name begins with the namespace specified in originalNamespace, replace the namespace 
        /// for the given type name with the namespace specified in the replacementNamespace parameter.
        /// </summary>
        /// <param name="originalNamespace">
        /// Original namespace to look for.
        /// Must either be an empty string ("") or end with a period (".")
        /// </param>
        /// <param name="replacementNamespace">
        /// Namespace to replace original namespace with
        /// Muse either be an empty string ("") or end with a period...
        /// </param>
        /// <param name="typeName">Typename on which to do the replacement</param>
        /// <returns>
        /// The new type name (potentially the same as passed in)
        /// </returns>
        private static string ReplaceNamespace(string originalNamespace, string replacementNamespace, string typeName)
        {
            Debug.Assert(originalNamespace.Length == 0 || originalNamespace.EndsWith(".", StringComparison.Ordinal));
            Debug.Assert(replacementNamespace.Length == 0 || replacementNamespace.EndsWith(".", StringComparison.Ordinal));
            Debug.Assert(typeName != null);

            if (typeName.StartsWith(originalNamespace, StringComparison.Ordinal))
            {
                // Strip out the original namespace and replace it with the new namespace
                return replacementNamespace + typeName.Substring(originalNamespace.Length);
            }
            else
            {
                return typeName;
            }
        }

        /// <summary>
        /// Given the namespace, return either an empty string (if the given namespace was NULL or emtpy)
        /// or a period-terminated (".") string.
        /// </summary>
        /// <param name="ns"></param>
        /// <returns>
        /// Either an empty string or a string that ends with a period (".")
        /// Can never return Null...
        /// </returns>
        private static string MakePeriodTerminatedNamespacePrefix(string ns)
        {
            if (String.IsNullOrEmpty(ns))
            {
                return "";
            }
            else if (!ns.EndsWith(".", StringComparison.Ordinal))
            {
                return ns + ".";
            }
            else
            {
                return ns;
            }
        }

        /// <summary>
        /// Determine if a type can be shared.
        /// 
        /// In order for a type to be shareable for service references, it has to be a 
        /// public type...
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static bool IsTypeShareable(Type t)
        {
            if (t == null)
            {
                System.Diagnostics.Debug.Fail("Why are you asking if a NULL type is shareable?");
                return false;
            }

            return t.IsPublic || t.IsNestedPublic;
        }

        /// <summary>
        /// Load referenced assemblies
        /// </summary>
        /// <param name="proxyOptions"></param>
        /// <param name="typeLoader"></param>
        /// <param name="importErrors"></param>
        /// <return></return>
        /// <remarks></remarks>
        private static IEnumerable<Assembly> LoadReferenedAssemblies(ClientOptions proxyOptions, IContractGeneratorReferenceTypeLoader typeLoader, IList<ProxyGenerationError> importErrors)
        {
            List<Assembly> referencedAssemblies = new List<Assembly>();
            if (proxyOptions.ReferenceAllAssemblies)
            {
                try
                {
                    IEnumerable<Exception> loadingErrors = null;
                    IEnumerable<Assembly> allAssemblies = null;
                    typeLoader.LoadAllAssemblies(out allAssemblies, out loadingErrors);
                    if (loadingErrors != null)
                    {
                        // treat as warning messages
                        foreach (Exception ex in loadingErrors)
                        {
                            importErrors.Add(new ProxyGenerationError(
                                            ProxyGenerationError.GeneratorState.GenerateCode,
                                            String.Empty,
                                            ex,
                                            true));
                        }
                    }

                    if (allAssemblies != null)
                    {
                        referencedAssemblies.AddRange(allAssemblies);
                    }
                }
                catch (Exception ex)
                {
                    importErrors.Add(new ProxyGenerationError(
                                    ProxyGenerationError.GeneratorState.GenerateCode,
                                    String.Empty,
                                    ex));
                }
            }

            foreach (ReferencedAssembly referencedAssembly in proxyOptions.ReferencedAssemblyList)
            {
                try
                {
                    Assembly refAssembly = typeLoader.LoadAssembly(referencedAssembly.AssemblyName);
                    if (refAssembly != null && !referencedAssemblies.Contains(refAssembly))
                    {
                        referencedAssemblies.Add(refAssembly);
                    }
                }
                catch (Exception ex)
                {
                    importErrors.Add(new ProxyGenerationError(
                                    ProxyGenerationError.GeneratorState.GenerateCode,
                                    String.Empty,
                                    ex));
                }
            }
            return referencedAssemblies;
        }

        /// <summary>
        /// Load the list of types that we have specified as collection types in our client options.
        /// The collection types will be used to generate collections in the data contracts.
        /// </summary>
        /// <param name="proxyOptions">Options specifying the list of collection types</param>
        /// <param name="typeLoader">Type loader that resolves type names to actual CLR types</param>
        /// <param name="importErrors">Errors encountered while loading the collection types</param>
        /// <return></return>
        /// <remarks></remarks>
        protected static IEnumerable<Type> LoadSharedCollectionTypes(ClientOptions proxyOptions, IContractGeneratorReferenceTypeLoader typeLoader, IList<ProxyGenerationError> importErrors)
        {
            List<Type> referencedCollectionTypes = new List<Type>();
            foreach (ReferencedCollectionType referencedCollectionMapping in proxyOptions.CollectionMappingList)
            {
                try
                {
                    Type collectionType = typeLoader.LoadType(referencedCollectionMapping.TypeName);

                    // verify...
                    if (!IsTypeShareable(collectionType))
                    {
                        importErrors.Add(
                                new ProxyGenerationError(
                                    ProxyGenerationError.GeneratorState.GenerateCode,
                                    String.Empty,
                                    new FormatException(String.Format(CultureInfo.CurrentCulture, WCFModelStrings.ReferenceGroup_SharedTypeMustBePublic, referencedCollectionMapping.TypeName)))
                            );
                        continue;
                    }

                    referencedCollectionTypes.Add(collectionType);
                }
                catch (Exception ex)
                {
                    importErrors.Add(new ProxyGenerationError(
                                    ProxyGenerationError.GeneratorState.GenerateCode,
                                    String.Empty,
                                    ex));
                }
            }
            return referencedCollectionTypes;
        }

        /// <summary>
        /// Create an appropriate WsdlImporter for the generator
        /// </summary>
        /// <param name="svcMapFile"></param>
        /// <param name="toolConfiguration"></param>
        /// <param name="targetCompileUnit"></param>
        /// <param name="codeDomProvider"></param>
        /// <param name="targetNamespace"></param>
        /// <param name="typeLoader"></param>
        /// <param name="targetFrameworkVersion">Targetted Framework version number</param>
        /// <param name="importErrors"></param>
        /// <param name="typedDataSetSchemaImporterExtension"></param>
        /// <returns></returns>
        protected static WsdlImporter CreateWsdlImporter(SvcMapFile svcMapFile,
                                                      System.Configuration.Configuration toolConfiguration,
                                                      CodeCompileUnit targetCompileUnit,
                                                      System.CodeDom.Compiler.CodeDomProvider codeDomProvider,
                                                      string targetNamespace,
                                                      IServiceProvider serviceProviderForImportExtensions,
                                                      IContractGeneratorReferenceTypeLoader typeLoader,
                                                      int targetFrameworkVersion,
                                                      IList<ProxyGenerationError> importErrors,
                                                      System.Type typedDataSetSchemaImporterExtension)
        {
            List<MetadataSection> metadataSections = CollectMetadataDocuments(svcMapFile.MetadataList, importErrors);

            WsdlImporter importer = null;

            ClientOptions.ProxySerializerType serializerType = svcMapFile.ClientOptions.Serializer;
            if (serializerType == ClientOptions.ProxySerializerType.Auto && ContainsHttpBindings(metadataSections))
            {
                // NOTE: HTTP Get/Post binding indicates an old web service. We use XmlSerializer to prevent generating dup classes.
                // Please check devdiv bug 94078
                serializerType = ClientOptions.ProxySerializerType.XmlSerializer;
            }

            if (toolConfiguration != null)
            {
                ServiceModelSectionGroup serviceModelSection = ServiceModelSectionGroup.GetSectionGroup(toolConfiguration);

                if (serviceModelSection != null)
                {
                    Collection<IWsdlImportExtension> wsdlImportExtensions = serviceModelSection.Client.Metadata.LoadWsdlImportExtensions();
                    Collection<IPolicyImportExtension> policyImportExtensions = serviceModelSection.Client.Metadata.LoadPolicyImportExtensions();

                    // If we have specified a specific serializer to use, we remove
                    // the other serializer...
                    switch (serializerType)
                    {
                        case ClientOptions.ProxySerializerType.DataContractSerializer:
                            RemoveExtension(typeof(XmlSerializerMessageContractImporter), wsdlImportExtensions);
                            break;
                        case ClientOptions.ProxySerializerType.XmlSerializer:
                            RemoveExtension(typeof(DataContractSerializerMessageContractImporter), wsdlImportExtensions);
                            break;
                        case ClientOptions.ProxySerializerType.Auto:
                            break;
                        default:
                            System.Diagnostics.Debug.Fail("Unknown serializer");
                            break;
                    }

                    ProvideImportExtensionsWithContextInformation(svcMapFile, serviceProviderForImportExtensions, wsdlImportExtensions, policyImportExtensions);

                    wsdlImportExtensions.Add(new HttpBindingExtension());

                    // Create Importer...
                    importer = new WsdlImporter(new MetadataSet(metadataSections), policyImportExtensions, wsdlImportExtensions);
                }
            }

            if (importer == null)
            {
                importer = new WsdlImporter(new MetadataSet(metadataSections));
            }

            // DevDiv 124333 - Always add DataContract importer (even if we are in XmlSerializerMode) to 
            // enable importing Fault contracts...
            importer.State.Add(typeof(System.Runtime.Serialization.XsdDataContractImporter),
                           CreateDataContractImporter(svcMapFile.ClientOptions, targetCompileUnit, codeDomProvider, targetNamespace, typeLoader, targetFrameworkVersion, importErrors));

            if (serializerType != ClientOptions.ProxySerializerType.DataContractSerializer)
            {
                importer.State.Add(typeof(System.ServiceModel.Channels.XmlSerializerImportOptions),
                               CreateXmlSerializerImportOptions(svcMapFile.ClientOptions,
                                                                targetCompileUnit,
                                                                codeDomProvider,
                                                                targetNamespace,
                                                                typedDataSetSchemaImporterExtension));
            }

            // Read the UseSerializerForFaults from Reference.svcmap, create a FaultImportOptions using this information
            // and pass it to WSDL Importer.
            FaultImportOptions faultOptions = new FaultImportOptions();
            faultOptions.UseMessageFormat = svcMapFile.ClientOptions.UseSerializerForFaults;
            importer.State.Add(typeof(System.ServiceModel.FaultImportOptions), faultOptions);

            // Read the WrappedOptions from Reference.svcmap, create a WrappedOptions using this information
            // and pass it to WSDL Importer.
            WrappedOptions wrappedOptions = new WrappedOptions();
            wrappedOptions.WrappedFlag = svcMapFile.ClientOptions.Wrapped;
            importer.State.Add(typeof(System.ServiceModel.Channels.WrappedOptions), wrappedOptions);

            return importer;
        }

        /// <summary>
        /// Look through all the import extensions to see if any of them want access to 
        ///   the service reference's extension files.  They tell us this by implementing
        ///   the interface IWcfReferenceReceiveContextInformation.
        /// </summary>
        /// <param name="svcMapFile"></param>
        /// <param name="serviceProviderForImportExtensions"></param>
        /// <param name="wsdlImportExtensions"></param>
        /// <param name="policyImportExtensions"></param>
        internal static void ProvideImportExtensionsWithContextInformation(SvcMapFile svcMapFile, IServiceProvider serviceProviderForImportExtensions, IEnumerable<IWsdlImportExtension> wsdlImportExtensions, IEnumerable<IPolicyImportExtension> policyImportExtensions)
        {
            // Only make this copy if we need to (not the mainline case)
            Dictionary<string, byte[]> extensionFileContents = null;

            foreach (IWsdlImportExtension wsdlImportExtension in wsdlImportExtensions)
            {
                System.Web.Compilation.IWcfReferenceReceiveContextInformation receiveContext =
                    wsdlImportExtension as System.Web.Compilation.IWcfReferenceReceiveContextInformation;
                if (receiveContext != null)
                {
                    if (extensionFileContents == null)
                    {
                        extensionFileContents = CreateDictionaryOfCopiedExtensionFiles(svcMapFile);
                    }
                    receiveContext.ReceiveImportContextInformation(
                        extensionFileContents,
                        serviceProviderForImportExtensions);
                }
            }
            foreach (IPolicyImportExtension policyImportExtension in policyImportExtensions)
            {
                System.Web.Compilation.IWcfReferenceReceiveContextInformation receiveContext =
                    policyImportExtension as System.Web.Compilation.IWcfReferenceReceiveContextInformation;
                if (receiveContext != null)
                {
                    if (extensionFileContents == null)
                    {
                        extensionFileContents = CreateDictionaryOfCopiedExtensionFiles(svcMapFile);
                    }
                    receiveContext.ReceiveImportContextInformation(
                        extensionFileContents,
                        serviceProviderForImportExtensions);
                }
            }
        }

        /// <summary>
        /// Remove specific wsdl importer extension
        /// </summary>
        /// <param name="extensionType">
        /// The extension to remove
        /// </param>
        /// <param name="wsdlImportExtensions">
        /// The collection to remove the extension from
        /// </param>
        /// <return></return>
        /// <remarks></remarks>
        private static void RemoveExtension(Type extensionType, Collection<IWsdlImportExtension> wsdlImportExtensions)
        {
            Debug.Assert(wsdlImportExtensions != null);

            for (int i = 0; i < wsdlImportExtensions.Count; i++)
            {
                if (wsdlImportExtensions[i].GetType() == extensionType)
                    wsdlImportExtensions.RemoveAt(i);
            }
        }

        /// <summary>
        /// Creates a dictionary containing a copy of the contents of all of the extension files
        /// </summary>
        /// <returns></returns>
        private static Dictionary<string, byte[]> CreateDictionaryOfCopiedExtensionFiles(SvcMapFile svcMapFile)
        {
            Dictionary<string, byte[]> extensionFileContents = new Dictionary<string, byte[]>();
            foreach (ExtensionFile extensionFile in svcMapFile.Extensions)
            {
                // If the extension file was not successfully loaded, do not include it in the
                //   collection.  Users are more likely to expect that the extension file won't
                //   be in the collection on an error than they are to assume they have to check
                //   if the byte array we return is null or not.
                if (extensionFile.ContentBuffer != null && extensionFile.IsBufferValid)
                {
                    extensionFileContents.Add(extensionFile.Name, (byte[])extensionFile.ContentBuffer.Clone());
                }
            }

            return extensionFileContents;
        }


        /// <summary>
        /// Merge metadata files to prepare code generation
        /// </summary>
        /// <returns>metadata collection</returns>
        /// <remarks></remarks>
        protected static List<MetadataSection> CollectMetadataDocuments(IEnumerable<MetadataFile> metadataList, IList<ProxyGenerationError> importErrors)
        {
            List<MetadataSection> metadataCollection = new List<MetadataSection>();

            foreach (MetadataFile metadataItem in metadataList)
            {
                if (!metadataItem.Ignore)
                {
                    try
                    {
                        MetadataSection metadataSection = metadataItem.CreateMetadataSection();
                        if (metadataSection != null)
                        {
                            metadataCollection.Add(metadataSection);
                        }
                    }
                    catch (Exception ex)
                    {
                        importErrors.Add(ConvertMetadataErrorToProxyGenerationError(metadataItem, ex));
                    }
                }
            }

            RemoveDuplicatedSchemaItems(metadataCollection, importErrors);
            CheckDuplicatedWsdlItems(metadataCollection, importErrors);

            return metadataCollection;
        }

        /// <summary>
        /// Convert metadata loading errors into proxy generation error messages
        /// </summary>
        /// <return></return>
        /// <remarks></remarks>
        internal static ProxyGenerationError ConvertMetadataErrorToProxyGenerationError(MetadataFile metadataItem, Exception ex)
        {
            ProxyGenerationError generationError = null;
            if (ex is XmlSchemaException)
            {
                generationError = new ProxyGenerationError(ProxyGenerationError.GeneratorState.LoadMetadata, metadataItem.FileName, (XmlSchemaException)ex);
            }
            else if (ex is XmlException)
            {
                generationError = new ProxyGenerationError(ProxyGenerationError.GeneratorState.LoadMetadata, metadataItem.FileName, (XmlException)ex);
            }
            else if (ex is InvalidOperationException)
            {
                System.Xml.Schema.XmlSchemaException schemaException = ex.InnerException as System.Xml.Schema.XmlSchemaException;
                if (schemaException != null)
                {
                    generationError = new ProxyGenerationError(ProxyGenerationError.GeneratorState.LoadMetadata, metadataItem.FileName, schemaException);
                }
                else
                {
                    System.Xml.XmlException xmlException = ex.InnerException as System.Xml.XmlException;
                    if (xmlException != null)
                    {
                        generationError = new ProxyGenerationError(ProxyGenerationError.GeneratorState.LoadMetadata, metadataItem.FileName, xmlException);
                    }
                    else
                    {
                        generationError = new ProxyGenerationError(ProxyGenerationError.GeneratorState.LoadMetadata, metadataItem.FileName, (InvalidOperationException)ex);
                    }
                }
            }
            else
            {
                generationError = new ProxyGenerationError(ProxyGenerationError.GeneratorState.LoadMetadata, metadataItem.FileName, ex);
            }
            return generationError;
        }

        /// <summary>
        /// Remove duplicated schema items from the metadata collection
        /// </summary>
        /// <remarks></remarks>
        private static void RemoveDuplicatedSchemaItems(List<MetadataSection> metadataCollection, IList<ProxyGenerationError> importErrors)
        {
            Dictionary<XmlSchema, MetadataSection> schemaList = new Dictionary<XmlSchema, MetadataSection>();

            // add independent schema files...
            foreach (MetadataSection metadataSection in metadataCollection)
            {
                if (metadataSection.Dialect == MetadataSection.XmlSchemaDialect)
                {
                    XmlSchema schema = (XmlSchema)metadataSection.Metadata;
                    schemaList.Add(schema, metadataSection);
                }
            }

            // add embedded files...
            foreach (MetadataSection metadataSection in metadataCollection)
            {
                if (metadataSection.Dialect == MetadataSection.ServiceDescriptionDialect)
                {
                    System.Web.Services.Description.ServiceDescription wsdl = (System.Web.Services.Description.ServiceDescription)metadataSection.Metadata;
                    foreach (XmlSchema schema in wsdl.Types.Schemas)
                    {
                        schema.SourceUri = wsdl.RetrievalUrl;
                        schemaList.Add(schema, metadataSection);
                    }
                }
            }

            IEnumerable<XmlSchema> duplicatedSchemas;
            SchemaMerger.MergeSchemas(schemaList.Keys, importErrors, out duplicatedSchemas);

            if (duplicatedSchemas != null)
            {
                foreach (XmlSchema schema in duplicatedSchemas)
                {
                    Debug.Assert(schemaList.ContainsKey(schema), "The schema list should not contain any of the schemas returned in the duplicateSchemas...");

                    MetadataSection metadataSection = schemaList[schema];
                    if (metadataSection.Dialect == MetadataSection.XmlSchemaDialect)
                    {
                        metadataCollection.Remove(metadataSection);
                    }
                    else if (metadataSection.Dialect == MetadataSection.ServiceDescriptionDialect)
                    {
                        System.Web.Services.Description.ServiceDescription wsdl = (System.Web.Services.Description.ServiceDescription)metadataSection.Metadata;
                        wsdl.Types.Schemas.Remove(schema);
                    }
                }
            }
        }

        /// <summary>
        /// check all wsdl files, and generate error messages if one contract have multiple different specifications
        /// </summary>
        /// <remarks></remarks>
        private static void CheckDuplicatedWsdlItems(IList<MetadataSection> metadataCollection, IList<ProxyGenerationError> importErrors)
        {
            List<System.Web.Services.Description.ServiceDescription> wsdlFiles = new List<System.Web.Services.Description.ServiceDescription>();
            foreach (MetadataSection metadataSection in metadataCollection)
            {
                if (metadataSection.Dialect == MetadataSection.ServiceDescriptionDialect)
                {
                    System.Web.Services.Description.ServiceDescription wsdl = (System.Web.Services.Description.ServiceDescription)metadataSection.Metadata;
                    wsdlFiles.Add(wsdl);
                }
            }

            WsdlInspector.CheckDuplicatedWsdlItems(wsdlFiles, importErrors);
        }

        /// <summary>
        /// Given a WSDL importer, a set of metadata files and a compile unit, import the metadata
        /// files.
        /// </summary>
        /// <param name="importer"></param>
        /// <param name="compileUnit"></param>
        /// <param name="generationErrors">
        /// Errors encountered whie importing the model. Any new errors will be appended to this list.
        /// </param>
        /// <param name="serviceEndpointList">List of endpoints imported</param>
        /// <param name="bindingCollection">The collection of bindings imported</param>
        /// <param name="contractCollection">The collection of contracts imported</param>
        protected static void ImportWCFModel(WsdlImporter importer,
                                          System.CodeDom.CodeCompileUnit compileUnit,
                                          IList<ProxyGenerationError> generationErrors,
                                          out List<ServiceEndpoint> serviceEndpointList,
                                          out IEnumerable<System.ServiceModel.Channels.Binding> bindingCollection,
                                          out IEnumerable<ContractDescription> contractCollection)
        {
            // We want to remove soap1.2 endpoints for ASMX references, but we can't use the "normal" way 
            // of using a IWsdlImportExtension to do so since BeforeImport is called too late (DevDiv 7857)
            // If DevDiv 7857 is fixed, we can remove the following two lines and instead add the 
            // AsmxEndpointPickerExtension to the importer's wsdl import extensions...
            IWsdlImportExtension asmxFixerUpper = new AsmxEndpointPickerExtension();
            asmxFixerUpper.BeforeImport(importer.WsdlDocuments, null, null);

            // NOTE: we should import Endpoint before Contracts, otherwise some information (related to binding) will be lost in the model (devdiv: 22396)
            serviceEndpointList = new List<ServiceEndpoint>();

            //
            // First we import all the endpoints (ports). This is required so that any WsdlImportExtension's BeforeImport
            // gets called before we actually try to import anything from the WSDL object model. 
            // If we don't do this, we run into problems if any wsdl import extensions want to delete a specific port
            // and this port happens to be the first port we try to import (you can't interrupt the import)
            importer.ImportAllEndpoints();

            //
            // We need to go through each endpoint element and "re-import" it in order to get the mapping
            // between the wsdlPort and the ServiceEndpoint... Importing the same endpoint twice is a no-op
            // as far as the endpoint collection is concerned - it is simply a hashtable lookup to retreive 
            // the already generated information...
            //
            foreach (System.Web.Services.Description.ServiceDescription wsdlServiceDescription in importer.WsdlDocuments)
            {
                foreach (System.Web.Services.Description.Service wsdlService in wsdlServiceDescription.Services)
                {
                    foreach (System.Web.Services.Description.Port servicePort in wsdlService.Ports)
                    {
                        try
                        {
                            ServiceEndpoint newEndpoint = importer.ImportEndpoint(servicePort);
                            serviceEndpointList.Add(newEndpoint);
                        }
                        catch (InvalidOperationException)
                        {
                            // Invalid operation exceptions should already be in the errors collection for the importer, so we don't
                            // need to add another generationError. The most probable cause for this is that the we failed to import 
                            // the endpoint...
                        }
                        catch (Exception ex)
                        { // It is bad, because WsdlImporter.WsdlImportException is a private class
                            generationErrors.Add(new ProxyGenerationError(ProxyGenerationError.GeneratorState.GenerateCode, wsdlServiceDescription.RetrievalUrl, ex));
                        }
                    }
                }
            }


            bindingCollection = importer.ImportAllBindings();
            System.Diagnostics.Debug.Assert(bindingCollection != null, "The importer should never return a NULL binding collection!");

            contractCollection = importer.ImportAllContracts();
            System.Diagnostics.Debug.Assert(contractCollection != null, "The importer should never return a NULL contract collection!");

            foreach (MetadataConversionError error in importer.Errors)
            {
                generationErrors.Add(new ProxyGenerationError(error));
            }
        }



        /// <summary>
        /// This function patches ServiceContractAttribute in the generated proxy code. It replaces all proxyNamespace in the attribute
        ///   to configNamespace.  The configNamespace does not depend on defaultNamespace of the project.
        /// </summary>
        /// <param name="proxyNamespace"></param>
        /// <param name="configNamespace"></param>
        /// <return></return>
        /// <remarks></remarks>
        private static void PatchConfigurationNameInServiceContractAttribute(CodeCompileUnit proxyCodeUnit, string proxyNamespace, string configNamespace)
        {
            if (proxyNamespace == null)
            {
                proxyNamespace = String.Empty;
            }

            string proxyNamespaceHead = MakePeriodTerminatedNamespacePrefix(proxyNamespace);
            string configNamespaceHead = MakePeriodTerminatedNamespacePrefix(configNamespace);

            if (proxyCodeUnit != null)
            {
                foreach (CodeNamespace proxyCodeNamespace in proxyCodeUnit.Namespaces)
                {
                    // Find the namespace we are patching...
                    if (String.Equals(proxyNamespace, proxyCodeNamespace.Name, StringComparison.Ordinal))
                    {
                        // ...and all types in each namespace...
                        foreach (CodeTypeDeclaration typeDeclaration in proxyCodeNamespace.Types)
                        {
                            if (typeDeclaration.IsInterface)
                            {
                                // ...and each attribute on each interface...
                                foreach (CodeAttributeDeclaration codeAttribute in typeDeclaration.CustomAttributes)
                                {
                                    // find System.ServiceModel.ServiceContractAttribute attribute.
                                    if (String.Equals(codeAttribute.AttributeType.BaseType, typeof(System.ServiceModel.ServiceContractAttribute).FullName, StringComparison.Ordinal))
                                    {
                                        foreach (CodeAttributeArgument argument in codeAttribute.Arguments)
                                        {
                                            if (String.Equals(argument.Name, "ConfigurationName", StringComparison.Ordinal))
                                            {
                                                // we only fix the string here
                                                CodePrimitiveExpression valueExpression = argument.Value as CodePrimitiveExpression;
                                                if (valueExpression != null && valueExpression.Value is string)
                                                {
                                                    valueExpression.Value = ReplaceNamespace(proxyNamespaceHead, configNamespaceHead, (string)valueExpression.Value);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        /// <summary>
        /// Patch VB code for output parameters.
        /// 
        /// Visual Basic doesn't support Out parameters - they are all generated as ByRef.
        /// Unfortunately, the CodeDom provider doesn't add an Out attribute to the ByRef
        /// parameters, so we have to do that ourselves...
        /// </summary>
        /// <param name="codeCompileUnit"></param>
        /// <remarks></remarks>
        private static void PatchOutParametersInVB(CodeCompileUnit codeCompileUnit)
        {
            foreach (CodeNamespace codeNamespace in codeCompileUnit.Namespaces)
            {
                foreach (CodeTypeDeclaration codeClass in codeNamespace.Types)
                {
                    PatchTypeDeclaration(codeClass);
                }
            }
        }

        /// <summary>
        /// Patch TypeDeclaration in VB code for output parameters
        /// </summary>
        /// <param name="codeClass"></param>
        /// <remarks></remarks>
        private static void PatchTypeDeclaration(CodeTypeDeclaration codeClass)
        {
            foreach (CodeTypeMember member in codeClass.Members)
            {
                if (member is CodeTypeDeclaration)
                {
                    // Recurse down in nested types...
                    PatchTypeDeclaration((CodeTypeDeclaration)member);
                }
                else if (member is CodeMemberMethod)
                {
                    CodeMemberMethod method = member as CodeMemberMethod;
                    foreach (CodeParameterDeclarationExpression parameter in method.Parameters)
                    {
                        if (parameter.Direction == FieldDirection.Out)
                        {
                            // Make sure that all Out parameters have an <Out> attribute
                            //
                            // First check for explicit <OutAttribute> declaration to avoid adding duplicate attributes.
                            if (!IsDefinedInCodeAttributeCollection(typeof(System.Runtime.InteropServices.OutAttribute), parameter.CustomAttributes))
                            {
                                parameter.CustomAttributes.Add(OutAttribute);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// check whether code attribuate has already been declared.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="metadata"></param>
        /// <return></return>
        /// <remarks></remarks>
        private static bool IsDefinedInCodeAttributeCollection(Type type, CodeAttributeDeclarationCollection metadata)
        {
            foreach (CodeAttributeDeclaration attribute in metadata)
            {
                if (String.Equals(attribute.Name, type.FullName, StringComparison.Ordinal) || String.Equals(attribute.Name, type.Name, StringComparison.Ordinal))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Check whether it is VB language
        /// </summary>
        /// <param name="codeDomProvider"></param>
        /// <return></return>
        /// <remarks></remarks>
        private static bool IsVBCodeDomProvider(System.CodeDom.Compiler.CodeDomProvider codeDomProvider)
        {
            string fileExtension = codeDomProvider.FileExtension;
            try
            {
                string language = System.CodeDom.Compiler.CodeDomProvider.GetLanguageFromExtension(fileExtension);
                return String.Equals(language, VB_LANGUAGE_NAME, StringComparison.OrdinalIgnoreCase);
            }
            catch (System.Configuration.ConfigurationException)
            {
                // not defined extension
                return false;
            }
        }

        /// <summary>
        /// check whether HTTP Binding is used in those metadata files
        /// </summary>
        /// <param name="metadataCollection">metadata files</param>
        /// <return></return>
        /// <remarks></remarks>
        private static bool ContainsHttpBindings(IEnumerable<MetadataSection> metadataCollection)
        {
            foreach (MetadataSection metadataSection in metadataCollection)
            {
                if (metadataSection.Dialect == MetadataSection.ServiceDescriptionDialect)
                {
                    System.Web.Services.Description.ServiceDescription wsdlFile = (System.Web.Services.Description.ServiceDescription)metadataSection.Metadata;
                    if (ContainsHttpBindings(wsdlFile))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// check whether HTTP Binding is used in one wsdl file
        /// </summary>
        /// <param name="wsdlFile">one wsdl</param>
        /// <return></return>
        /// <remarks></remarks>
        internal static bool ContainsHttpBindings(System.Web.Services.Description.ServiceDescription wsdlFile)
        {
            foreach (System.Web.Services.Description.Binding binding in wsdlFile.Bindings)
            {
                foreach (object extension in binding.Extensions)
                {
                    System.Web.Services.Description.HttpBinding httpBinding = extension as System.Web.Services.Description.HttpBinding;
                    if (httpBinding != null)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}