1616using System . Text ;
1717using System . Xml ;
1818using Microsoft . PowerPlatform . Dataverse . Client . Utils ;
19- //using Microsoft.Crm.Protocols.WSTrust.Bindings;
2019using Microsoft . Xrm . Sdk ;
2120using Microsoft . Xrm . Sdk . Client ;
2221using Microsoft . Xrm . Sdk . Common ;
@@ -262,13 +261,13 @@ public static IssuerEndpoint GetIssuer(Binding binding)
262261 }
263262
264263 return null ;
265- }
264+ }
266265
266+ #if NETFRAMEWORK
267267 private static KerberosSecurityTokenParameters GetKerberosTokenParameters ( SecurityBindingElement securityElement )
268268 {
269269 if ( securityElement != null )
270270 {
271- #if NETFRAMEWORK
272271 if ( securityElement . EndpointSupportingTokenParameters != null )
273272 {
274273 if ( securityElement . EndpointSupportingTokenParameters . Endorsing != null )
@@ -279,15 +278,12 @@ private static KerberosSecurityTokenParameters GetKerberosTokenParameters(Securi
279278 }
280279 }
281280 }
282- #else
283- throw new PlatformNotSupportedException ( "Xrm.Sdk WSTrust" ) ;
284- #endif
285281 }
286-
287282 return null ;
288283 }
284+ #endif
289285
290- private static IssuedSecurityTokenParameters GetIssuedTokenParameters ( SecurityBindingElement securityElement )
286+ private static IssuedSecurityTokenParameters GetIssuedTokenParameters ( SecurityBindingElement securityElement )
291287 {
292288 if ( securityElement != null )
293289 {
@@ -351,29 +347,27 @@ public static CustomBinding SetIssuer(Binding binding, IssuerEndpoint issuerEndp
351347 }
352348
353349 return new CustomBinding ( elements ) ;
354- }
350+ }
355351
352+ #if NETFRAMEWORK
356353 private static void ParseEndpoints ( ServiceEndpointDictionary serviceEndpoints , ServiceEndpointCollection serviceEndpointCollection )
357354 {
358355 serviceEndpoints . Clear ( ) ;
359356
360357 if ( serviceEndpointCollection != null )
361358 {
362- #if NETFRAMEWORK
363359 foreach ( var endpoint in serviceEndpointCollection )
364360 {
365361 if ( IsEndpointSupported ( endpoint ) )
366362 {
367363 serviceEndpoints . Add ( endpoint . Name , endpoint ) ;
368364 }
369365 }
370- #else
371- throw new PlatformNotSupportedException ( "Xrm.Sdk WSDL" ) ;
372- #endif
373366 }
374367 }
368+ #endif
375369
376- private static bool IsEndpointSupported ( ServiceEndpoint endpoint )
370+ private static bool IsEndpointSupported ( ServiceEndpoint endpoint )
377371 {
378372 if ( endpoint != null )
379373 {
@@ -389,6 +383,7 @@ private static bool IsEndpointSupported(ServiceEndpoint endpoint)
389383
390384 internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata ( Type contractType , Uri serviceUri , bool checkForSecondary )
391385 {
386+ #if NETFRAMEWORK // WebInfra; MetadataSet and CreateMetadataClient are NETFRAMEWORK-ONLY
392387 ServiceEndpointMetadata serviceEndpointMetadata = new ServiceEndpointMetadata ( ) ;
393388
394389 serviceEndpointMetadata . ServiceUrls = ServiceConfiguration < IOrganizationService > . CalculateEndpoints ( serviceUri ) ;
@@ -398,25 +393,17 @@ internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type con
398393 serviceEndpointMetadata . ServiceUrls . AlternateEndpoint = null ;
399394 }
400395
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-
406396 // Get version of current assembly which is the version of the SDK
407- #pragma warning disable CS0162 // Unreachable code detected
408397 Version sdkVersion = GetSDKVersionNumberFromAssembly ( ) ;
409- #pragma warning restore CS0162 // Unreachable code detected
410398 var wsdlUri = new Uri ( string . Format ( CultureInfo . InvariantCulture , "{0}{1}&sdkversion={2}" , serviceUri . AbsoluteUri , "?wsdl" , sdkVersion . ToString ( 2 ) ) ) ;
411399
412400 var mcli = CreateMetadataClient ( wsdlUri . Scheme ) ;
413401 if ( mcli != null )
414402 {
415- #if NETFRAMEWORK
416403 try
417404 {
418405 serviceEndpointMetadata . ServiceMetadata = mcli . GetMetadata ( wsdlUri , MetadataExchangeClientMode . HttpGet ) ;
419- }
406+ }
420407 catch ( InvalidOperationException ioexp )
421408 {
422409 bool rethrow = true ;
@@ -447,29 +434,14 @@ internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type con
447434 throw ;
448435 }
449436 }
450- #else
451- throw new PlatformNotSupportedException ( "Xrm.Sdk WSDL" ) ;
452- #endif
453437 }
454- else
455- {
456- #if ! NETFRAMEWORK
457438
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" ) ;
467440
468441 var contracts = CreateContractCollection ( contractType ) ;
469442
470443 if ( contracts != null )
471444 {
472- #if NETFRAMEWORK
473445 // The following code inserts a custom WsdlImporter without removing the other
474446 // importers already in the collection.
475447 var importer = new WsdlImporter ( serviceEndpointMetadata . ServiceMetadata ) ;
@@ -497,34 +469,15 @@ internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type con
497469 }
498470
499471 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
522472 }
523473
524474 return serviceEndpointMetadata ;
475+ #else
476+ throw new NotImplementedException ( "ServiceModel metadata support is limited for this target framework" ) ;
477+ #endif
525478 }
526479
527- private static Version GetSDKVersionNumberFromAssembly ( )
480+ private static Version GetSDKVersionNumberFromAssembly ( )
528481 {
529482 string fileVersion = OrganizationServiceProxy . GetXrmSdkAssemblyFileVersion ( ) ;
530483
@@ -536,8 +489,9 @@ private static Version GetSDKVersionNumberFromAssembly()
536489 }
537490
538491 return parsedVersion ;
539- }
492+ }
540493
494+ #if NETFRAMEWORK
541495 /// <summary>
542496 /// Returns a list of policy import extensions in the importer parameter and adds a SecurityBindingElementImporter if not already present in the list.
543497 /// </summary>
@@ -546,7 +500,7 @@ private static Version GetSDKVersionNumberFromAssembly()
546500 private static List < IPolicyImportExtension > AddSecurityBindingToPolicyImporter ( WsdlImporter importer )
547501 {
548502 List < IPolicyImportExtension > newExts = new List < IPolicyImportExtension > ( ) ;
549- #if NETFRAMEWORK
503+
550504
551505 KeyedByTypeCollection < IPolicyImportExtension > policyExtensions = importer . PolicyImportExtensions ;
552506 SecurityBindingElementImporter securityBindingElementImporter = policyExtensions . Find < SecurityBindingElementImporter > ( ) ;
@@ -564,18 +518,14 @@ private static List<IPolicyImportExtension> AddSecurityBindingToPolicyImporter(W
564518 newExts . AddRange ( policyExtensions ) ;
565519
566520 return newExts ;
567- #else
568-
569- newExts . Add ( new AuthenticationPolicyImporter ( new SecurityBindingElementImporter ( ) ) ) ;
570- return newExts ;
571- //throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
572- #endif
573521 }
522+ #endif
574523
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." ) ]
576526 private static bool TryRetrieveMetadata ( MetadataExchangeClient mcli , Uri serviceEndpoint , ServiceEndpointMetadata serviceEndpointMetadata )
577527 {
578- #if NETFRAMEWORK
528+
579529 bool rethrow = true ;
580530 try
581531 {
@@ -589,24 +539,22 @@ private static bool TryRetrieveMetadata(MetadataExchangeClient mcli, Uri service
589539 }
590540
591541 return rethrow ;
592- #else
593- throw new PlatformNotSupportedException ( "Xrm.Sdk WSDL" ) ;
594- #endif
595542 }
543+ #endif
596544
597- private static XmlQualifiedName GetPortTypeQName ( ContractDescription contract )
545+ private static XmlQualifiedName GetPortTypeQName ( ContractDescription contract )
598546 {
599547 return new XmlQualifiedName ( contract . Name , contract . Namespace ) ;
600548 }
601549
602550 private static Collection < ContractDescription > CreateContractCollection ( Type contract )
603551 {
604552 return new Collection < ContractDescription > { ContractDescription . GetContract ( contract ) } ;
605- }
553+ }
606554
555+ #if NETFRAMEWORK
607556 private static MetadataExchangeClient CreateMetadataClient ( string scheme )
608557 {
609- #if NETFRAMEWORK
610558 WSHttpBinding mexBinding = null ;
611559
612560 if ( string . Compare ( scheme , "https" , StringComparison . OrdinalIgnoreCase ) == 0 )
@@ -628,13 +576,10 @@ private static MetadataExchangeClient CreateMetadataClient(string scheme)
628576 mcli . MaximumResolvedReferences = 100 ;
629577
630578 return mcli ;
631- #else
632- return null ;
633- //throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
634- #endif
635579 }
580+ #endif
636581
637- public static void ReplaceEndpointAddress ( ServiceEndpoint endpoint , Uri adddress )
582+ public static void ReplaceEndpointAddress ( ServiceEndpoint endpoint , Uri adddress )
638583 {
639584 var addressBuilder = new EndpointAddressBuilder ( endpoint . Address ) ;
640585 addressBuilder . Uri = adddress ;
0 commit comments