Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IServiceBrokerProvider available in all layers #76767

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
<PackageReference Include="Microsoft.VisualStudio.ImageCatalog" />
<PackageReference Include="Microsoft.VisualStudio.Language" />
<PackageReference Include="Microsoft.VisualStudio.LanguageServer.Client" PrivateAssets="all" />
<PackageReference Include="Microsoft.ServiceHub.Client" />
<PackageReference Include="Microsoft.VisualStudio.Debugger.Contracts" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\Compilers\Core\Portable\InternalUtilities\IsExternalInit.cs" Link="Utilities\IsExternalInit.cs" />
<Compile Include="..\..\LanguageServer\Microsoft.CodeAnalysis.LanguageServer\BrokeredServices\ServiceBrokerProvider.cs" Link="BrokeredServices\ServiceBrokerProvider.cs" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" Key="$(MoqPublicKey)" LoadsWithinVisualStudio="false" />
Expand Down Expand Up @@ -101,4 +101,7 @@
<PublicAPI Include="PublicAPI.Shipped.txt" />
<PublicAPI Include="PublicAPI.Unshipped.txt" />
</ItemGroup>
<ItemGroup>
<Folder Include="BrokeredServices\" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#pragma warning disable RS0030 // Do not use banned APIs (MEFv1)
tmat marked this conversation as resolved.
Show resolved Hide resolved

using System;
using System.ComponentModel.Composition;
using Microsoft.CodeAnalysis.Host.Mef;
Expand All @@ -10,11 +12,6 @@

namespace Microsoft.CodeAnalysis.BrokeredServices;

internal interface IServiceBrokerProvider
{
IServiceBroker ServiceBroker { get; }
}

/// <summary>
/// MEF service that can be used to fetch an <see cref="IServiceBroker"/> instance without having to use legacy MEF imports.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@

<ItemGroup>
<ProjectReference Include="..\..\..\LanguageServer\Microsoft.CodeAnalysis.LanguageServer\Microsoft.CodeAnalysis.LanguageServer.csproj" PrivateAssets="all" />
<ProjectReference Include="..\..\..\Features\Core\Portable\Microsoft.CodeAnalysis.Features.csproj" PrivateAssets="all" />
<ProjectReference Include="..\..\..\Workspaces\Core\Portable\Microsoft.CodeAnalysis.Workspaces.csproj" PrivateAssets="all" />
<ProjectReference Include="..\..\..\Workspaces\Remote\Core\Microsoft.CodeAnalysis.Remote.Workspaces.csproj" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Composition" />
<PackageReference Include="Microsoft.ServiceHub.Framework" />

<!--
Additional runtime dependencies that we must include in the NuGet package as they are not a part of the main language server package.
Do not remove GeneratePathProperty="True" as it is required below to add the package to the package we generate.
Expand Down
12 changes: 12 additions & 0 deletions src/Workspaces/Remote/Core/IServiceBrokerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ServiceHub.Framework;

namespace Microsoft.CodeAnalysis.BrokeredServices;

internal interface IServiceBrokerProvider
{
IServiceBroker ServiceBroker { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,30 @@
using System;
using System.Composition;
using System.Threading;
using Microsoft.CodeAnalysis.BrokeredServices;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.ServiceHub.Framework;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Remote.Host;

internal interface IGlobalServiceBroker
{
IServiceBroker Instance { get; }
}

/// <summary>
/// Hacky way to expose a <see cref="IServiceBroker"/> to workspace services that expect there to be a global
/// singleton (like in visual studio). Effectively the first service that gets called into will record its
/// broker here for these services to use.
/// Exposes a <see cref="IServiceBroker"/> to services that expect there to be a global singleton.
/// The first remote service that gets called into will record its broker here.
/// </summary>
// Note: this Export is only so MEF picks up the exported member internally.
[Export(typeof(IGlobalServiceBroker)), Shared]
internal class GlobalServiceBroker : IGlobalServiceBroker
[Export(typeof(IServiceBrokerProvider)), Shared]
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class RemoteServiceBrokerProvider() : IServiceBrokerProvider
{
private static IServiceBroker? s_instance;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public GlobalServiceBroker()
{
}

public static void RegisterServiceBroker(IServiceBroker serviceBroker)
{
Interlocked.CompareExchange(ref s_instance, serviceBroker, null);
}

public IServiceBroker Instance
public IServiceBroker ServiceBroker
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Telemetry" />
<PackageReference Include="Microsoft.VisualStudio.RpcContracts" />

<!-- TODO: remove https://github.com/dotnet/roslyn/issues/71374 -->
<PackageReference Include="Microsoft.ServiceHub.Framework" />
<PackageReference Include="Microsoft.ServiceHub.Client" />
</ItemGroup>
<ItemGroup>
<PublicAPI Include="PublicAPI.Shipped.txt" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal TService Create(
{
// Register this service broker globally (if it's the first we encounter) so it can be used by other
// global services that need it.
GlobalServiceBroker.RegisterServiceBroker(serviceBroker);
RemoteServiceBrokerProvider.RegisterServiceBroker(serviceBroker);

var descriptor = ServiceDescriptors.Instance.GetServiceDescriptorForServiceFactory(typeof(TService));
var serviceHubTraceSource = (TraceSource?)hostProvidedServices.GetService(typeof(TraceSource));
Expand Down
Loading