大网数据广告活动
Web服务器与PHP

Troubleshoot brokered services

· 52CE 我爱测

Learn how to troubleshoot several common problems that can occur when you try to get a brokered service in the Visual Studio SDK.

Applies to: Visual Studio 2019 and later versions

This article introduces troubleshooting suggestions and possible solutions for several common problems that may occur when you try to get a brokered service in the Visual Studio SDK.

Brokered services may fail in various ways. A useful technique to start your investigation with is to check the Visual Studio Activity Log, which often logs errors or warnings when things go awry with brokered services.

Issues when requesting a service

Perhaps the most common challenge with brokered services is understanding the result or exception they get from a call to xref:Microsoft.ServiceHub.Framework.IServiceBroker.GetProxyAsync*?displayProperty=nameWithType or xref:Microsoft.ServiceHub.Framework.IServiceBroker.GetPipeAsync*?displayProperty=nameWithType. An xref:Microsoft.ServiceHub.Framework.IServiceBroker intentionally abstracts away concerns about where and how a brokered service may be activated. But when things go wrong it becomes necessary to drill deeper to diagnose and correct the problem.

No service

The result of a service request may be null when any of the following conditions are true:

The service request throws an exception

A service request may throw a xref:Microsoft.ServiceHub.Framework.ServiceCompositionException when the service factory throws an exception. This means all the problems listed above that would lead to a null result don't apply. Look at the exception details (including inner exceptions) to understand what went wrong and make any necessary corrections to the client or the service factory.

You get a local service while a remote one was expected

A request may be fulfilled locally or remotely, depending on the service registration and the current state of Visual Studio. The default scope is xref:Microsoft.VisualStudio.Shell.ServiceBroker.ServiceAudience.Process?displayProperty=nameWithType, which means the brokered service can only be activated when it's proffered from the same process as the client.

When a brokered service should be exposed from a Live Share host to a connected guest but the xref:Microsoft.VisualStudio.Shell.ServiceBroker.ServiceAudience is limited to local scopes, a request from a Live Share guest will activate the brokered service from that same machine rather than the host. Update registration to include xref:Microsoft.VisualStudio.Shell.ServiceBroker.ServiceAudience.LiveShareGuest?displayProperty=nameWithType to expose your brokered services over Live Share. You may also need to set xref:Microsoft.VisualStudio.Shell.ServiceBroker.ProvideBrokeredServiceAttribute.AllowTransitiveGuestClients?displayProperty=nameWithType to true as well.

Important: Registration for the brokered service must exist on both the Live Share guest and the host to support the guest in activating the brokered service from the host.

When exposing a brokered service over Live Share, please review How to secure a brokered service.

Issues when proffering a service

A brokered service must be proffered from an xref:Microsoft.VisualStudio.Shell.AsyncPackage class unless the brokered service is exported via MEF, as described in How to provide a brokered service.

An attempt to proffer a brokered service will throw an exception if any of these conditions are true:

  • The moniker for the service being proffered doesn't exactly match (name and version) a service that has been registered.
  • A factory has already been proffered for the same service moniker.

The result of a call to xref:Microsoft.VisualStudio.Shell.ServiceBroker.IBrokeredServiceContainer.Proffer*?displayProperty=nameWithType is an xref:System.IDisposable. A brokered service becomes unavailable to new requests after this value is disposed. When your brokered service has specific affinity to some context such as an open solution, it may be appropriate to only proffer the brokered service when that context is active. It isn't necessary to retain and dispose of this value when your package is disposed.

Tracing RPC between client and service

Once a connection between client and service is established, tracing their communication can be useful, particularly when they are in different processes.

By default, traces of communications between brokered services that span processes (such that RPC applies) are recorded in .svclog files found in the %TEMP%\VSLogs directory. These xml files are best viewed with the Service Trace Viewer. This tool can open many .svclog files at once and stitch them together to form a multi-party graph to make understanding the RPC between client and service much easier.

A brokered service itself can trace directly to add to these .svclog trace files, further aiding diagnostics of brokered service behavior. Any traces saved to %TEMP%\VSLogs might be collected when the "Report a Problem" command is invoked and the user elects to share logs.

To trace your own messages such that they can be easily discovered and combined with other .svclog traces, your code (whether a brokered service or not) can do something like this:

// Define your log's ID, a namespace-like fully qualified name.
// In general it is expected that you follow you team's assembly namespace.
// Also an optional parameter, the ServiceMoniker for your service
var myLogId = new LogId("Microsoft.SomeTeam.MyLogName", serviceId: null);

var requestedLevel = new LoggingLevelSettings(SourceLevels.Warning | SourceLevels.ActivityTracing);
var myLogOptions = new LoggerOptions(requestedLevel, PrivacyFlags.MayContainPrivateInformation);

TraceSource myTraceSource;
using (TraceConfiguration traceConfig = await TraceConfiguration.CreateTraceConfigurationInstanceAsync(serviceBroker, ownsServiceBroker: false, cancellationToken))
{
    myTraceSource = await traceConfig.RegisterLogSourceAsync(myLogId, myLogOptions, traceSource: null, cancellationToken);
}

The myTraceSource can now be used for tracing, as it has the appropriate listeners added to write your traces to a .svclog file. If you already have a xref:System.Diagnostics.TraceSource you wish to use, pass it to the xref:Microsoft.VisualStudio.LogHub.TraceConfiguration.RegisterLogSourceAsync* method and discard the result as the listeners will be added to your existing xref:System.Diagnostics.TraceSource.

When tracing from a brokered service that serves a remote client, an activity is automatically assigned to the xref:System.Threading.ExecutionContext that your code runs within that allows the svclog to be stitched together with the client's svclog in order to see a holistic view using the Service Trace Viewer.

References

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors (upstream author metadata: aartig13). Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 02/18/2022
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。