16
16
using System . Text ;
17
17
using System . Xml ;
18
18
using Microsoft . PowerPlatform . Dataverse . Client . Utils ;
19
- //using Microsoft.Crm.Protocols.WSTrust.Bindings;
20
19
using Microsoft . Xrm . Sdk ;
21
20
using Microsoft . Xrm . Sdk . Client ;
22
21
using Microsoft . Xrm . Sdk . Common ;
@@ -262,13 +261,13 @@ public static IssuerEndpoint GetIssuer(Binding binding)
262
261
}
263
262
264
263
return null ;
265
- }
264
+ }
266
265
266
+ #if NETFRAMEWORK
267
267
private static KerberosSecurityTokenParameters GetKerberosTokenParameters ( SecurityBindingElement securityElement )
268
268
{
269
269
if ( securityElement != null )
270
270
{
271
- #if NETFRAMEWORK
272
271
if ( securityElement . EndpointSupportingTokenParameters != null )
273
272
{
274
273
if ( securityElement . EndpointSupportingTokenParameters . Endorsing != null )
@@ -279,15 +278,12 @@ private static KerberosSecurityTokenParameters GetKerberosTokenParameters(Securi
279
278
}
280
279
}
281
280
}
282
- #else
283
- throw new PlatformNotSupportedException ( "Xrm.Sdk WSTrust" ) ;
284
- #endif
285
281
}
286
-
287
282
return null ;
288
283
}
284
+ #endif
289
285
290
- private static IssuedSecurityTokenParameters GetIssuedTokenParameters ( SecurityBindingElement securityElement )
286
+ private static IssuedSecurityTokenParameters GetIssuedTokenParameters ( SecurityBindingElement securityElement )
291
287
{
292
288
if ( securityElement != null )
293
289
{
@@ -351,29 +347,27 @@ public static CustomBinding SetIssuer(Binding binding, IssuerEndpoint issuerEndp
351
347
}
352
348
353
349
return new CustomBinding ( elements ) ;
354
- }
350
+ }
355
351
352
+ #if NETFRAMEWORK
356
353
private static void ParseEndpoints ( ServiceEndpointDictionary serviceEndpoints , ServiceEndpointCollection serviceEndpointCollection )
357
354
{
358
355
serviceEndpoints . Clear ( ) ;
359
356
360
357
if ( serviceEndpointCollection != null )
361
358
{
362
- #if NETFRAMEWORK
363
359
foreach ( var endpoint in serviceEndpointCollection )
364
360
{
365
361
if ( IsEndpointSupported ( endpoint ) )
366
362
{
367
363
serviceEndpoints . Add ( endpoint . Name , endpoint ) ;
368
364
}
369
365
}
370
- #else
371
- throw new PlatformNotSupportedException ( "Xrm.Sdk WSDL" ) ;
372
- #endif
373
366
}
374
367
}
368
+ #endif
375
369
376
- private static bool IsEndpointSupported ( ServiceEndpoint endpoint )
370
+ private static bool IsEndpointSupported ( ServiceEndpoint endpoint )
377
371
{
378
372
if ( endpoint != null )
379
373
{
@@ -389,6 +383,7 @@ private static bool IsEndpointSupported(ServiceEndpoint endpoint)
389
383
390
384
internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata ( Type contractType , Uri serviceUri , bool checkForSecondary )
391
385
{
386
+ #if NETFRAMEWORK // WebInfra; MetadataSet and CreateMetadataClient are NETFRAMEWORK-ONLY
392
387
ServiceEndpointMetadata serviceEndpointMetadata = new ServiceEndpointMetadata ( ) ;
393
388
394
389
serviceEndpointMetadata . ServiceUrls = ServiceConfiguration < IOrganizationService > . CalculateEndpoints ( serviceUri ) ;
@@ -398,25 +393,17 @@ internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type con
398
393
serviceEndpointMetadata . ServiceUrls . AlternateEndpoint = null ;
399
394
}
400
395
401
- #if ! NETFRAMEWORK
402
- // TODO: Waiting on work for updated WCF endpoints collection to be completed. // hard throw here to prevent any futher progress.
403
- throw new PlatformNotSupportedException ( "Xrm.Sdk WSDL" ) ;
404
- #endif
405
-
406
396
// Get version of current assembly which is the version of the SDK
407
- #pragma warning disable CS0162 // Unreachable code detected
408
397
Version sdkVersion = GetSDKVersionNumberFromAssembly ( ) ;
409
- #pragma warning restore CS0162 // Unreachable code detected
410
398
var wsdlUri = new Uri ( string . Format ( CultureInfo . InvariantCulture , "{0}{1}&sdkversion={2}" , serviceUri . AbsoluteUri , "?wsdl" , sdkVersion . ToString ( 2 ) ) ) ;
411
399
412
400
var mcli = CreateMetadataClient ( wsdlUri . Scheme ) ;
413
401
if ( mcli != null )
414
402
{
415
- #if NETFRAMEWORK
416
403
try
417
404
{
418
405
serviceEndpointMetadata . ServiceMetadata = mcli . GetMetadata ( wsdlUri , MetadataExchangeClientMode . HttpGet ) ;
419
- }
406
+ }
420
407
catch ( InvalidOperationException ioexp )
421
408
{
422
409
bool rethrow = true ;
@@ -447,29 +434,14 @@ internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type con
447
434
throw ;
448
435
}
449
436
}
450
- #else
451
- throw new PlatformNotSupportedException ( "Xrm.Sdk WSDL" ) ;
452
- #endif
453
437
}
454
- else
455
- {
456
- #if ! NETFRAMEWORK
457
438
458
- if ( serviceEndpointMetadata . ServiceMetadata == null )
459
- serviceEndpointMetadata . ServiceMetadata = new MetadataSet ( ) ;
460
- var MetadataBody = GetMexDocument ( wsdlUri ) ;
461
- #else
462
- throw new PlatformNotSupportedException ( "Xrm.Sdk WSDL" ) ;
463
- #endif
464
- }
465
-
466
- ClientExceptionHelper . ThrowIfNull ( serviceEndpointMetadata . ServiceMetadata , "STS Metadata" ) ;
439
+ ClientExceptionHelper . ThrowIfNull ( serviceEndpointMetadata . ServiceMetadata , "STS Metadata" ) ;
467
440
468
441
var contracts = CreateContractCollection ( contractType ) ;
469
442
470
443
if ( contracts != null )
471
444
{
472
- #if NETFRAMEWORK
473
445
// The following code inserts a custom WsdlImporter without removing the other
474
446
// importers already in the collection.
475
447
var importer = new WsdlImporter ( serviceEndpointMetadata . ServiceMetadata ) ;
@@ -497,34 +469,15 @@ internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type con
497
469
}
498
470
499
471
ParseEndpoints ( serviceEndpointMetadata . ServiceEndpoints , endpoints ) ;
500
- #else
501
-
502
- // Dataverse requires Message Transport security which is not supported in .net core for ActiveDirectory.
503
-
504
-
505
- //AuthenticationPolicy authenticationPolicy = new AuthenticationPolicy();
506
- //authenticationPolicy.PolicyElements.Add("AuthenticationType", "ActiveDirectory"); // Need to read these from metdata in the future if WCF does not provide support/.
507
- //TextMessageEncodingBindingElement text01 = new TextMessageEncodingBindingElement();
508
- //HttpsTransportBindingElement http1 = new HttpsTransportBindingElement();
509
- //http1.ExtendedProtectionPolicy = new System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicy(System.Security.Authentication.ExtendedProtection.PolicyEnforcement.WhenSupported, System.Security.Authentication.ExtendedProtection.ProtectionScenario.TransportSelected, null);
510
- //CustomBinding bind = new CustomBinding(authenticationPolicy, new TextMessageEncodingBindingElement(), http1);
511
- //bind.Name = "CustomBinding_IOrganizationService";
512
- //bind.Namespace = "http://schemas.microsoft.com/xrm/2011/Contracts/Services";
513
- //serviceEndpointMetadata.ServiceEndpoints.Add(
514
- // "CustomBinding_IOrganizationService",
515
- // new ServiceEndpoint(contracts[0],
516
- // bind,
517
- // new EndpointAddress(serviceEndpointMetadata.ServiceUrls.PrimaryEndpoint)));
518
-
519
-
520
- throw new PlatformNotSupportedException ( "Xrm.Sdk WSDL" ) ;
521
- #endif
522
472
}
523
473
524
474
return serviceEndpointMetadata ;
475
+ #else
476
+ throw new NotImplementedException ( "ServiceModel metadata support is limited for this target framework" ) ;
477
+ #endif
525
478
}
526
479
527
- private static Version GetSDKVersionNumberFromAssembly ( )
480
+ private static Version GetSDKVersionNumberFromAssembly ( )
528
481
{
529
482
string fileVersion = OrganizationServiceProxy . GetXrmSdkAssemblyFileVersion ( ) ;
530
483
@@ -536,8 +489,9 @@ private static Version GetSDKVersionNumberFromAssembly()
536
489
}
537
490
538
491
return parsedVersion ;
539
- }
492
+ }
540
493
494
+ #if NETFRAMEWORK
541
495
/// <summary>
542
496
/// Returns a list of policy import extensions in the importer parameter and adds a SecurityBindingElementImporter if not already present in the list.
543
497
/// </summary>
@@ -546,7 +500,7 @@ private static Version GetSDKVersionNumberFromAssembly()
546
500
private static List < IPolicyImportExtension > AddSecurityBindingToPolicyImporter ( WsdlImporter importer )
547
501
{
548
502
List < IPolicyImportExtension > newExts = new List < IPolicyImportExtension > ( ) ;
549
- #if NETFRAMEWORK
503
+
550
504
551
505
KeyedByTypeCollection < IPolicyImportExtension > policyExtensions = importer . PolicyImportExtensions ;
552
506
SecurityBindingElementImporter securityBindingElementImporter = policyExtensions . Find < SecurityBindingElementImporter > ( ) ;
@@ -564,18 +518,14 @@ private static List<IPolicyImportExtension> AddSecurityBindingToPolicyImporter(W
564
518
newExts . AddRange ( policyExtensions ) ;
565
519
566
520
return newExts ;
567
- #else
568
-
569
- newExts . Add ( new AuthenticationPolicyImporter ( new SecurityBindingElementImporter ( ) ) ) ;
570
- return newExts ;
571
- //throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
572
- #endif
573
521
}
522
+ #endif
574
523
575
- [ SuppressMessage ( "Microsoft.Design" , "CA1031:DoNotCatchGeneralExceptionTypes" , Justification = "Need to catch any exception here and fail." ) ]
524
+ #if NETFRAMEWORK
525
+ [ SuppressMessage ( "Microsoft.Design" , "CA1031:DoNotCatchGeneralExceptionTypes" , Justification = "Need to catch any exception here and fail." ) ]
576
526
private static bool TryRetrieveMetadata ( MetadataExchangeClient mcli , Uri serviceEndpoint , ServiceEndpointMetadata serviceEndpointMetadata )
577
527
{
578
- #if NETFRAMEWORK
528
+
579
529
bool rethrow = true ;
580
530
try
581
531
{
@@ -589,24 +539,22 @@ private static bool TryRetrieveMetadata(MetadataExchangeClient mcli, Uri service
589
539
}
590
540
591
541
return rethrow ;
592
- #else
593
- throw new PlatformNotSupportedException ( "Xrm.Sdk WSDL" ) ;
594
- #endif
595
542
}
543
+ #endif
596
544
597
- private static XmlQualifiedName GetPortTypeQName ( ContractDescription contract )
545
+ private static XmlQualifiedName GetPortTypeQName ( ContractDescription contract )
598
546
{
599
547
return new XmlQualifiedName ( contract . Name , contract . Namespace ) ;
600
548
}
601
549
602
550
private static Collection < ContractDescription > CreateContractCollection ( Type contract )
603
551
{
604
552
return new Collection < ContractDescription > { ContractDescription . GetContract ( contract ) } ;
605
- }
553
+ }
606
554
555
+ #if NETFRAMEWORK
607
556
private static MetadataExchangeClient CreateMetadataClient ( string scheme )
608
557
{
609
- #if NETFRAMEWORK
610
558
WSHttpBinding mexBinding = null ;
611
559
612
560
if ( string . Compare ( scheme , "https" , StringComparison . OrdinalIgnoreCase ) == 0 )
@@ -628,13 +576,10 @@ private static MetadataExchangeClient CreateMetadataClient(string scheme)
628
576
mcli . MaximumResolvedReferences = 100 ;
629
577
630
578
return mcli ;
631
- #else
632
- return null ;
633
- //throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
634
- #endif
635
579
}
580
+ #endif
636
581
637
- public static void ReplaceEndpointAddress ( ServiceEndpoint endpoint , Uri adddress )
582
+ public static void ReplaceEndpointAddress ( ServiceEndpoint endpoint , Uri adddress )
638
583
{
639
584
var addressBuilder = new EndpointAddressBuilder ( endpoint . Address ) ;
640
585
addressBuilder . Uri = adddress ;
0 commit comments