public async Task <Session?> CreateSessionAsync(Solution solution, object?callbackTarget = null, CancellationToken cancellationToken = default) { if (solution == null) { // keep old behavior for Razor return(null); } var connection = await _client.CreateConnectionAsync(_serviceName, callbackTarget : null, cancellationToken).ConfigureAwait(false); SessionWithSolution?session = null; try { // transfer ownership of the connection to the session object: session = await SessionWithSolution.CreateAsync(connection, solution, cancellationToken).ConfigureAwait(false); } finally { if (session == null) { connection.Dispose(); } } return(new Session(session)); }
public async Task TestCancellationOnSessionWithSolution() { var code = @"class Test { void Method() { } }"; using (var workspace = CreateWorkspace(LanguageNames.CSharp, code)) { var solution = workspace.CurrentSolution; var solutionChecksum = await solution.State.GetChecksumAsync(CancellationToken.None); var service = solution.Workspace.Services.GetService <IRemotableDataService>(); var source = new CancellationTokenSource(); using var session = new InvokeThrowsCancellationConnection(source); var exception = await Assert.ThrowsAnyAsync <OperationCanceledException>(() => SessionWithSolution.CreateAsync(session, solution, source.Token)); Assert.Equal(exception.CancellationToken, source.Token); // make sure things that should have been cleaned up are cleaned up Assert.Null(await((RemotableDataService)service).TestOnly_GetRemotableDataAsync(solutionChecksum, CancellationToken.None).ConfigureAwait(false)); } }