Skip to content

Commit a45920b

Browse files
authored
Avoid setting DefaultRequestHeaders when request is in flight. (#1604)
* Avoid setting DefaultRequestHeaders when request is in flight. * Bump version number.
1 parent 217262b commit a45920b

File tree

45 files changed

+81
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+81
-59
lines changed

config/ModuleMetadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
],
2626
"releaseNotes": "See https://aka.ms/GraphPowerShell-Release.",
2727
"assemblyOriginatorKeyFile": "35MSSharedLib1024.snk",
28-
"version": "1.14.0"
28+
"version": "1.14.1"
2929
}

src/Applications/Applications/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ directive:
7878
### Versioning
7979
8080
``` yaml
81-
module-version: 1.14.0
81+
module-version: 1.14.1
8282
release-notes: See https://aka.ms/GraphPowerShell-Release.
8383
```

src/Authentication/Authentication.Test/Helpers/HttpHelpersTests.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
namespace Microsoft.Graph.Authentication.Test.Helpers
55
{
66
using System;
7+
using System.Collections.Generic;
8+
using System.Management.Automation;
79
using System.Net.Http;
10+
using System.Threading.Tasks;
811
using Microsoft.Graph.Authentication.Core;
912
using Microsoft.Graph.PowerShell.Authentication;
1013
using Microsoft.Graph.PowerShell.Authentication.Helpers;
1114
using Microsoft.Graph.PowerShell.Authentication.Models;
1215
using Xunit;
13-
1416
public class HttpHelpersTests
1517
{
1618
[Fact]
@@ -184,5 +186,30 @@ public void GetGraphHttpClientShouldReturnNewHttpClientSignOutThenSignIn()
184186
// reset static instance.
185187
GraphSession.Reset();
186188
}
189+
190+
[Fact]
191+
public async void GetGraphHttpClientShouldBeThreadSafeAsync()
192+
{
193+
GraphSession.Initialize(() => new GraphSession());
194+
GraphSession.Instance.RequestContext = new RequestContext();
195+
var authContext = new AuthContext
196+
{
197+
AuthType = AuthenticationType.UserProvidedAccessToken,
198+
ContextScope = ContextScope.Process,
199+
PSHostVersion = new Version("7.2.7")
200+
};
201+
202+
var tasks = new List<Task<HttpClient>>();
203+
for (int i = 0; i < 100; i++)
204+
{
205+
tasks.Add(Task.Factory.StartNew(() => HttpHelpers.GetGraphHttpClient(authContext)));
206+
}
207+
var exception = await Record.ExceptionAsync(() => Task.WhenAll(tasks));
208+
Assert.Null(exception);
209+
210+
// reset static instance.
211+
GraphSession.Reset();
212+
}
213+
187214
}
188215
}

src/Authentication/Authentication/Helpers/HttpHelpers.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace Microsoft.Graph.PowerShell.Authentication.Helpers
1111
using System.Security.Authentication;
1212
using Microsoft.Graph.PowerShell.Authentication.Handlers;
1313
using System.Management.Automation;
14-
using System;
1514
using Microsoft.Graph.PowerShell.Authentication.Core.Interfaces;
1615

1716
/// <summary>
@@ -54,16 +53,6 @@ private static void ReplaceSDKHeader(HttpClient httpClient, string headerName, s
5453
}
5554
}
5655

57-
public static HttpClient GetGraphHttpClient(InvocationInfo invocationInfo, IAuthContext authContext = null)
58-
{
59-
authContext = authContext ?? GraphSession.Instance.AuthContext;
60-
if (authContext is null) { throw new AuthenticationException(Core.ErrorConstants.Message.MissingAuthContext); }
61-
var httpClient = GetGraphHttpClient(authContext);
62-
var requestUserAgent = new RequestUserAgent(authContext.PSHostVersion, invocationInfo);
63-
ReplaceSDKHeader(httpClient, HttpKnownHeaderNames.UserAgent, requestUserAgent.UserAgent);
64-
return httpClient;
65-
}
66-
6756
/// <summary>
6857
/// Creates a pre-configured Microsoft Graph <see cref="HttpClient"/>.
6958
/// </summary>
@@ -81,6 +70,8 @@ public static HttpClient GetGraphHttpClient(IAuthContext authContext = null)
8170

8271
IAuthenticationProvider authProvider = AuthenticationHelpers.GetAuthProvider(authContext);
8372
var newHttpClient = GetGraphHttpClient(authProvider, GraphSession.Instance.RequestContext);
73+
var requestUserAgent = new RequestUserAgent(authContext.PSHostVersion, null);
74+
ReplaceSDKHeader(newHttpClient, HttpKnownHeaderNames.UserAgent, requestUserAgent.UserAgent);
8475
GraphSession.Instance.GraphHttpClient = newHttpClient;
8576
return newHttpClient;
8677
}

src/Authentication/Authentication/Helpers/RequestUserAgent.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ internal string App
4747
{
4848
get
4949
{
50-
var app = string.Format(CultureInfo.InvariantCulture,
51-
"PowerShell/{0} {1}", _psHostVersion, _invocationInfo.MyCommand.Name);
50+
var app = string.Empty;
51+
if (_psHostVersion != null)
52+
app = string.Format(CultureInfo.InvariantCulture, "PowerShell/{0} {1}", _psHostVersion, _invocationInfo?.MyCommand?.Name);
5253
return app;
5354
}
5455
}

src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: Microsoft
55
#
6-
# Generated on: 10/25/2022
6+
# Generated on: 10/31/2022
77
#
88

99
@{
@@ -12,7 +12,7 @@
1212
RootModule = './Microsoft.Graph.Authentication.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.14.0'
15+
ModuleVersion = '1.14.1'
1616

1717
# Supported PSEditions
1818
CompatiblePSEditions = 'Core', 'Desktop'
@@ -99,7 +99,9 @@ PrivateData = @{
9999
PSData = @{
100100

101101
# Tags applied to this module. These help with module discovery in online galleries.
102-
Tags = 'Microsoft','Office365','Graph','PowerShell','Teams','Outlook','OneDrive','AzureAD','GraphAPI','Productivity','SharePoint','Intune','SDK'
102+
Tags = 'Microsoft', 'Office365', 'Graph', 'PowerShell', 'Teams', 'Outlook',
103+
'OneDrive', 'AzureAD', 'GraphAPI', 'Productivity', 'SharePoint', 'Intune',
104+
'SDK'
103105

104106
# A URL to the license for this module.
105107
LicenseUri = 'https://aka.ms/devservicesagreement'
@@ -124,7 +126,8 @@ PrivateData = @{
124126

125127
} # End of PSData hashtable
126128

127-
} # End of PrivateData hashtable
129+
130+
} # End of PrivateData hashtable
128131

129132
# HelpInfo URI of this module
130133
# HelpInfoURI = ''

src/Bookings/Bookings/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ directive:
5050
### Versioning
5151
5252
``` yaml
53-
module-version: 1.14.0
53+
module-version: 1.14.1
5454
release-notes: See https://aka.ms/GraphPowerShell-Release.
5555
```

src/Calendar/Calendar/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ directive:
5252
### Versioning
5353

5454
``` yaml
55-
module-version: 1.14.0
55+
module-version: 1.14.1
5656
release-notes: See https://aka.ms/GraphPowerShell-Release.
5757
```

src/ChangeNotifications/ChangeNotifications/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ subject-prefix: ''
3434
### Versioning
3535
3636
``` yaml
37-
module-version: 1.14.0
37+
module-version: 1.14.1
3838
release-notes: See https://aka.ms/GraphPowerShell-Release.
3939
```

src/CloudCommunications/CloudCommunications/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ directive:
5959
### Versioning
6060
6161
``` yaml
62-
module-version: 1.14.0
62+
module-version: 1.14.1
6363
release-notes: See https://aka.ms/GraphPowerShell-Release.
6464
```

0 commit comments

Comments
 (0)