File: LiveShare\Guest\ProxyAccessor.cs
Web Access
Project: src\src\Razor\src\Razor\src\Microsoft.VisualStudio.LanguageServices.Razor\Microsoft.VisualStudio.LanguageServices.Razor.csproj (Microsoft.VisualStudio.LanguageServices.Razor)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System.ComponentModel.Composition;
using System.Threading;
using Microsoft.VisualStudio.Threading;
 
namespace Microsoft.VisualStudio.Razor.LiveShare.Guest;
 
[Export(typeof(IProxyAccessor))]
[method: ImportingConstructor]
internal class ProxyAccessor(
    ILiveShareSessionAccessor liveShareSessionAccessor,
    JoinableTaskContext joinableTaskContext) : IProxyAccessor
{
    private readonly ILiveShareSessionAccessor _liveShareSessionAccessor = liveShareSessionAccessor;
    private readonly JoinableTaskFactory _jtf = joinableTaskContext.Factory;
 
    private IProjectHierarchyProxy? _projectHierarchyProxy;
 
    public IProjectHierarchyProxy GetProjectHierarchyProxy()
        => _projectHierarchyProxy ??= CreateServiceProxy<IProjectHierarchyProxy>();
 
    private TProxy CreateServiceProxy<TProxy>() where TProxy : class
    {
        var session = _liveShareSessionAccessor.Session;
        Assumes.NotNull(session);
 
        var proxy = _jtf.Run(
            () => session.GetRemoteServiceAsync<TProxy>(typeof(TProxy).Name, CancellationToken.None));
        return proxy ?? throw new global::System.InvalidOperationException($"Unable to resolve Live Share proxy for {typeof(TProxy).Name}.");
    }
}