Skip to content

Commit c0cf11f

Browse files
author
Release-Agent
committed
''
1 parent 0bf4724 commit c0cf11f

36 files changed

+3328
-224
lines changed

src/GeneralTools/DataverseClient/Client/Builder/AbstractClientRequestBuilder.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ public T WithCorrelationId(Guid correlationId)
6767
/// <returns></returns>
6868
public T WithHeader(string key, string value)
6969
{
70-
_headers.Add(key, value);
70+
if ( _headers.ContainsKey(key))
71+
{
72+
_headers[key] = value;
73+
}
74+
else
75+
{
76+
_headers.Add(key, value);
77+
}
7178
return (T)this;
7279
}
7380

@@ -79,7 +86,16 @@ public T WithHeader(string key, string value)
7986
public T WithHeaders(IDictionary<string, string> headers)
8087
{
8188
foreach (var itm in headers)
82-
_headers.Add(itm.Key, itm.Value);
89+
{
90+
if( _headers.ContainsKey(itm.Key))
91+
{
92+
_headers[itm.Key] = itm.Value;
93+
}
94+
else
95+
{
96+
_headers.Add(itm.Key, itm.Value);
97+
}
98+
}
8399
return (T)this;
84100
}
85101

src/GeneralTools/DataverseClient/Client/ConnectionService.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ internal System.Net.NetworkCredential DataverseServiceAccessCredential
394394
/// <summary>
395395
/// Type of protocol to use
396396
/// </summary>
397-
internal string InternetProtocalToUse { get { return _InternetProtocalToUse; } set { _InternetProtocalToUse = value; } }
397+
internal string InternetProtocolToUse { get { return _InternetProtocalToUse; } set { _InternetProtocalToUse = value; } }
398398

399399
/// <summary>
400400
/// returns the connected organization detail object.
@@ -2337,6 +2337,8 @@ internal async Task<HttpResponseMessage> Command_WebExecuteAsync(string queryStr
23372337
string requestIdLogSegement = logEntry.GetFormatedRequestSessionIdString(requestTrackingId, SessionTrackingId);
23382338
do
23392339
{
2340+
retry = false; // Set intial state.
2341+
23402342
// Add authorization header. - Here to catch the situation where a token expires during retry.
23412343
if (!customHeaders.ContainsKey(Utilities.RequestHeaders.AUTHORIZATION_HEADER))
23422344
customHeaders.Add(Utilities.RequestHeaders.AUTHORIZATION_HEADER, new List<string>() { string.Format("Bearer {0}", await RefreshClientTokenAsync().ConfigureAwait(false)) });

src/GeneralTools/DataverseClient/Client/Connector/OnPremises/ServiceInformation.cs

+17-15
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using System.IdentityModel.Tokens;
55
using System.Linq;
66
using System.Security;
7-
using System.Security.Permissions;
87
using System.ServiceModel;
98
using System.ServiceModel.Description;
109
using System.ServiceModel.Security;
1110
using System.Text;
11+
1212
using Microsoft.Xrm.Sdk;
1313
using Microsoft.Xrm.Sdk.Client;
1414
using Microsoft.Xrm.Sdk.Common;
@@ -52,6 +52,7 @@ internal ServiceConfiguration(Uri serviceUri, bool checkForSecondary)
5252

5353
ClientExceptionHelper.ThrowIfNull(ServiceEndpointMetadata, "ServiceEndpointMetadata");
5454

55+
#if NETFRAMEWORK
5556
if (ServiceEndpointMetadata.ServiceEndpoints.Count == 0)
5657
{
5758
StringBuilder errorBuilder = new StringBuilder();
@@ -65,6 +66,7 @@ internal ServiceConfiguration(Uri serviceUri, bool checkForSecondary)
6566

6667
throw new InvalidOperationException(ClientExceptionHelper.FormatMessage(0, "The provided uri did not return any Service Endpoints!\n{0}", errorBuilder.ToString()));
6768
}
69+
#endif
6870

6971
ServiceEndpoints = ServiceEndpointMetadata.ServiceEndpoints;
7072

@@ -665,20 +667,20 @@ private SecurityTokenResponse AuthenticateInternal(AuthenticationCredentials aut
665667
{
666668
return Issue(authenticationCredentials);
667669
}
668-
catch (SecurityTokenValidationException)
669-
{
670-
retry = false;
671-
672-
// Fall back to windows integrated.
673-
if (authenticationCredentials.IssuerEndpoints.ContainsKey(TokenServiceCredentialType.Windows.ToString()))
674-
{
675-
authenticationCredentials.EndpointType = TokenServiceCredentialType.Windows;
676-
retry = ++retryCount < 2;
677-
}
678-
679-
// We don't care, we just want to return null. The reason why we are are catching this one is because in pure Kerberos mode, this
680-
// will throw a very bad exception that will crash VS.
681-
}
670+
//catch (SecurityTokenValidationException) // Removed due to a type conflict with DV Server
671+
//{
672+
// retry = false;
673+
674+
// // Fall back to windows integrated.
675+
// if (authenticationCredentials.IssuerEndpoints.ContainsKey(TokenServiceCredentialType.Windows.ToString()))
676+
// {
677+
// authenticationCredentials.EndpointType = TokenServiceCredentialType.Windows;
678+
// retry = ++retryCount < 2;
679+
// }
680+
681+
// // We don't care, we just want to return null. The reason why we are are catching this one is because in pure Kerberos mode, this
682+
// // will throw a very bad exception that will crash VS.
683+
//}
682684
catch (SecurityNegotiationException)
683685
{
684686
// This is the exception with Integrated Windows Auth.

src/GeneralTools/DataverseClient/Client/Connector/OnPremises/ServiceMetadataUtility.cs

+28-83
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using System.Text;
1717
using System.Xml;
1818
using Microsoft.PowerPlatform.Dataverse.Client.Utils;
19-
//using Microsoft.Crm.Protocols.WSTrust.Bindings;
2019
using Microsoft.Xrm.Sdk;
2120
using Microsoft.Xrm.Sdk.Client;
2221
using 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;

src/GeneralTools/DataverseClient/Client/DataverseTelemetryBehaviors.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ public object BeforeSendRequest(ref Message request, IClientChannel channel)
369369
{
370370
using (OperationContextScope scope = new OperationContextScope((IContextChannel)channel))
371371
{
372-
var ForceConsistencytHeader = new MessageHeader<string>("Strong").GetUntypedHeader(Utilities.RequestHeaders.FORCE_CONSISTENCY, "http://schemas.microsoft.com/xrm/2011/Contracts");
373-
request.Headers.Add(ForceConsistencytHeader);
372+
var ForceConsistencyHeader = new MessageHeader<string>("Strong").GetUntypedHeader(Utilities.RequestHeaders.FORCE_CONSISTENCY, "http://schemas.microsoft.com/xrm/2011/Contracts");
373+
request.Headers.Add(ForceConsistencyHeader);
374374
}
375375
}
376376
return null;

0 commit comments

Comments
 (0)