Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 7e51779

Browse files
committed
Fix null reference exception in Visual Studio 2015
ServiceProviderExports.GetService<TippingService>() was returning null on Visual Studio 2015 so, fall back to using new TippingService(...).
1 parent f895926 commit 7e51779

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ async Task EnsurePackageLoaded(Guid packageGuid)
128128
[PartCreationPolicy(CreationPolicy.Shared)]
129129
public class ServiceProviderExports
130130
{
131+
static readonly ILogger log = LogManager.ForContext<ServiceProviderExports>();
131132
readonly IServiceProvider serviceProvider;
132133

133134
[ImportingConstructor]
@@ -152,9 +153,33 @@ public ServiceProviderExports([Import(typeof(SVsServiceProvider))] IServiceProvi
152153
public IPackageSettings PackageSettings => GetService<IPackageSettings>();
153154

154155
[ExportForVisualStudioProcess]
155-
public ITippingService TippingService => GetService<ITippingService>();
156+
public ITippingService TippingService
157+
{
158+
get
159+
{
160+
var tippingService = GetService<ITippingService>();
161+
if (tippingService == null)
162+
{
163+
// GetService<TippingService>() was returning null on Visual Studio 2015, so fall back to using new TippingService(...)
164+
log.Warning("Couldn't find service of type {Type}, using new TippingService(...) instead", typeof(ITippingService));
165+
tippingService = new TippingService(serviceProvider);
166+
}
167+
168+
return tippingService;
169+
}
170+
}
156171

157-
T GetService<T>() => (T)serviceProvider.GetService(typeof(T));
172+
T GetService<T>() where T : class
173+
{
174+
var service = (T)serviceProvider.GetService(typeof(T));
175+
if (service == null)
176+
{
177+
log.Error("Couldn't find service of type {Type}", typeof(T));
178+
return null;
179+
}
180+
181+
return service;
182+
}
158183
}
159184

160185
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]

0 commit comments

Comments
 (0)