diff --git a/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineAssemblyResolver.cs b/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineAssemblyResolver.cs index df36dfa33b..6248495d04 100644 --- a/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineAssemblyResolver.cs +++ b/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineAssemblyResolver.cs @@ -15,15 +15,20 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor { public async Task> GetRazorEngineAssembliesAsync(Project project) { - var compilation = await project.GetCompilationAsync().ConfigureAwait(false); - var assemblies = GetRazorCustomizationAssemblies(compilation); - - if (assemblies.Count == 0) + try { - Console.WriteLine(); - } + var compilation = await project.GetCompilationAsync().ConfigureAwait(false); + var assemblies = GetRazorCustomizationAssemblies(compilation); - return assemblies; + return assemblies; + } + catch (Exception exception) + { + throw new RazorLanguageServiceException( + typeof(DefaultRazorEngineAssemblyResolver).FullName, + nameof(GetRazorEngineAssembliesAsync), + exception); + } } private static List GetRazorCustomizationAssemblies(Compilation compilation) diff --git a/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineDirectiveResolver.cs b/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineDirectiveResolver.cs index 94de54ab47..1b56f431be 100644 --- a/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineDirectiveResolver.cs +++ b/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineDirectiveResolver.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.Composition; using System.Threading; @@ -15,12 +16,22 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor { public async Task> GetRazorEngineDirectivesAsync(Workspace workspace, Project project, CancellationToken cancellationToken = default(CancellationToken)) { - var client = await RazorLanguageServiceClientFactory.CreateAsync(workspace, cancellationToken); - - using (var session = await client.CreateSessionAsync(project.Solution)) + try { - var directives = await session.InvokeAsync>("GetDirectivesAsync", new object[] { project.Id.Id, "Foo", }).ConfigureAwait(false); - return directives; + var client = await RazorLanguageServiceClientFactory.CreateAsync(workspace, cancellationToken); + + using (var session = await client.CreateSessionAsync(project.Solution)) + { + var directives = await session.InvokeAsync>("GetDirectivesAsync", new object[] { project.Id.Id, "Foo", }).ConfigureAwait(false); + return directives; + } + } + catch (Exception exception) + { + throw new RazorLanguageServiceException( + typeof(DefaultRazorEngineDirectiveResolver).FullName, + nameof(GetRazorEngineDirectivesAsync), + exception); } } } diff --git a/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineDocumentGenerator.cs b/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineDocumentGenerator.cs index 214650823b..76e43d789f 100644 --- a/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineDocumentGenerator.cs +++ b/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultRazorEngineDocumentGenerator.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Composition; using System.Threading; using System.Threading.Tasks; @@ -13,12 +14,22 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor { public async Task GenerateDocumentAsync(Workspace workspace, Project project, string filename, string text, CancellationToken cancellationToken = default(CancellationToken)) { - var client = await RazorLanguageServiceClientFactory.CreateAsync(workspace, cancellationToken); - - using (var session = await client.CreateSessionAsync(project.Solution)) + try { - var document = await session.InvokeAsync("GenerateDocumentAsync", new object[] { project.Id.Id, "Foo", filename, text }).ConfigureAwait(false); - return document; + var client = await RazorLanguageServiceClientFactory.CreateAsync(workspace, cancellationToken); + + using (var session = await client.CreateSessionAsync(project.Solution)) + { + var document = await session.InvokeAsync("GenerateDocumentAsync", new object[] { project.Id.Id, "Foo", filename, text }).ConfigureAwait(false); + return document; + } + } + catch (Exception exception) + { + throw new RazorLanguageServiceException( + typeof(DefaultRazorEngineDocumentGenerator).FullName, + nameof(GenerateDocumentAsync), + exception); } } } diff --git a/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultTagHelperResolver.cs b/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultTagHelperResolver.cs index 6b39c158d9..2cd272294d 100644 --- a/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultTagHelperResolver.cs +++ b/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultTagHelperResolver.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.Composition; using System.Threading; @@ -18,18 +19,28 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor public async Task> GetTagHelpersAsync(Project project) { - var client = await RazorLanguageServiceClientFactory.CreateAsync(Workspace, CancellationToken.None); - if (client == null) + try { - // The OOP host is turned off, so let's do this in process. - var resolver = new CodeAnalysis.Razor.DefaultTagHelperResolver(designTime: true); - return await resolver.GetTagHelpersAsync(project, CancellationToken.None).ConfigureAwait(false); - } + var client = await RazorLanguageServiceClientFactory.CreateAsync(Workspace, CancellationToken.None); + if (client == null) + { + // The OOP host is turned off, so let's do this in process. + var resolver = new CodeAnalysis.Razor.DefaultTagHelperResolver(designTime: true); + return await resolver.GetTagHelpersAsync(project, CancellationToken.None).ConfigureAwait(false); + } - using (var session = await client.CreateSessionAsync(project.Solution)) + using (var session = await client.CreateSessionAsync(project.Solution)) + { + var results = await session.InvokeAsync>("GetTagHelpersAsync", new object[] { project.Id.Id, "Foo", }).ConfigureAwait(false); + return results; + } + } + catch (Exception exception) { - var results = await session.InvokeAsync>("GetTagHelpersAsync", new object[] { project.Id.Id, "Foo", }).ConfigureAwait(false); - return results; + throw new RazorLanguageServiceException( + typeof(DefaultTagHelperResolver).FullName, + nameof(GetTagHelpersAsync), + exception); } } } diff --git a/src/Microsoft.VisualStudio.LanguageServices.Razor/Properties/Resources.Designer.cs b/src/Microsoft.VisualStudio.LanguageServices.Razor/Properties/Resources.Designer.cs new file mode 100644 index 0000000000..597444de46 --- /dev/null +++ b/src/Microsoft.VisualStudio.LanguageServices.Razor/Properties/Resources.Designer.cs @@ -0,0 +1,46 @@ +// +namespace Microsoft.VisualStudio.LanguageServices.Razor +{ + using System.Globalization; + using System.Reflection; + using System.Resources; + + internal static class Resources + { + private static readonly ResourceManager _resourceManager + = new ResourceManager("Microsoft.VisualStudio.LanguageServices.Razor.Resources", typeof(Resources).GetTypeInfo().Assembly); + + /// + /// An unexpected exception occurred when invoking '{0}.{1}' on the Razor language service. + /// + internal static string UnexpectedException + { + get { return GetString("UnexpectedException"); } + } + + /// + /// An unexpected exception occurred when invoking '{0}.{1}' on the Razor language service. + /// + internal static string FormatUnexpectedException(object p0, object p1) + { + return string.Format(CultureInfo.CurrentCulture, GetString("UnexpectedException"), p0, p1); + } + + private static string GetString(string name, params string[] formatterNames) + { + var value = _resourceManager.GetString(name); + + System.Diagnostics.Debug.Assert(value != null); + + if (formatterNames != null) + { + for (var i = 0; i < formatterNames.Length; i++) + { + value = value.Replace("{" + formatterNames[i] + "}", "{" + i + "}"); + } + } + + return value; + } + } +} diff --git a/src/Microsoft.VisualStudio.LanguageServices.Razor/RazorLanguageServiceException.cs b/src/Microsoft.VisualStudio.LanguageServices.Razor/RazorLanguageServiceException.cs new file mode 100644 index 0000000000..80c5d88a61 --- /dev/null +++ b/src/Microsoft.VisualStudio.LanguageServices.Razor/RazorLanguageServiceException.cs @@ -0,0 +1,15 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.VisualStudio.LanguageServices.Razor +{ + public sealed class RazorLanguageServiceException : Exception + { + internal RazorLanguageServiceException(string callerClass, string callerMethod, Exception innerException) + : base(Resources.FormatUnexpectedException(callerClass, callerMethod), innerException) + { + } + } +} diff --git a/src/Microsoft.VisualStudio.LanguageServices.Razor/Resources.resx b/src/Microsoft.VisualStudio.LanguageServices.Razor/Resources.resx new file mode 100644 index 0000000000..6783beb144 --- /dev/null +++ b/src/Microsoft.VisualStudio.LanguageServices.Razor/Resources.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + An unexpected exception occurred when invoking '{0}.{1}' on the Razor language service. + + \ No newline at end of file