src/Client/Set-XrmClientTimeout.ps1
|
<# .SYNOPSIS Change the timeout of an existing connection. .DESCRIPTION Set the maximum wait for the response of the requests sent with this client, until it is changed again or put back with Revert. Every command that uses the client benefits from it. For one long request, Invoke-XrmRequest -TimeoutInMinutes leaves the client unchanged. At connection, use New-XrmClient -MaxCrmConnectionTimeOutMinutes. A clone of the client (Invoke-XrmParallelRequests) gets the default timeout. .PARAMETER XrmClient Xrm connector initialized to target instance. Use latest one by default. (Dataverse ServiceClient) .PARAMETER DurationInMinutes Timeout duration in minutes. .PARAMETER Revert Restore default timeout value. (4 minutes) .OUTPUTS System.Void. .EXAMPLE $xrmClient | Set-XrmClientTimeout -DurationInMinutes 30; # Long synchronous commands $xrmClient | Set-XrmClientTimeout -Revert; .LINK https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Set-XrmClientTimeout.md #> function Set-XrmClientTimeout { [CmdletBinding(DefaultParameterSetName = "Duration")] [OutputType([System.Void])] param ( [Parameter(Mandatory = $false, ValueFromPipeline, Position = 0)] [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient] $XrmClient = $Global:XrmClient, [Parameter(Mandatory = $true, ParameterSetName = "Revert")] [Switch] $Revert, [Parameter(Mandatory = $true, ParameterSetName = "Duration")] [ValidateRange(1, 1440)] [int] $DurationInMinutes ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { $timeout = [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient]::MaxConnectionTimeout; if ($PSBoundParameters.ContainsKey('DurationInMinutes')) { $timeout = [TimeSpan]::FromMinutes($DurationInMinutes); } # The SDK reads its public (static) timeout at connection only: the one in use is on the channel of the client, # reached through SDK members that are not public. The SDK version is the one shipped with the module (checked by Client\Timeouts.Tests.ps1) $bindingFlags = [System.Reflection.BindingFlags]"Instance, Public, NonPublic"; $connectionField = [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient].GetField("_connectionSvc", $bindingFlags); $webClientProperty = $(if ($connectionField) { $connectionField.FieldType.GetProperty("WebClient", $bindingFlags) } else { $null }); $webClient = $(if ($webClientProperty) { $webClientProperty.GetValue($connectionField.GetValue($XrmClient)) } else { $null }); if ($null -eq $webClient) { throw "The timeout of this client cannot be changed (not connected, or unexpected SDK version). Use Invoke-XrmRequest -TimeoutInMinutes for one request, or New-XrmClient -MaxCrmConnectionTimeOutMinutes."; } # Through the interface: in Windows PowerShell the channel is a transparent proxy, whose properties PowerShell does not see [System.ServiceModel.IContextChannel].GetProperty("OperationTimeout").SetValue($webClient.InnerChannel, $timeout, $null); } end { $StopWatch.Stop(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function Set-XrmClientTimeout -Alias *; |