Exemple #1
0
        public async Task <IEnumerable <DirectiveDescriptor> > GetRazorEngineDirectivesAsync(Workspace workspace, Project project, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var client = await RazorLanguageServiceClientFactory.CreateAsync(workspace, cancellationToken);

                using (var session = await client.CreateSessionAsync(project.Solution))
                {
                    var directives = await session.InvokeAsync <IEnumerable <DirectiveDescriptor> >(
                        "GetDirectivesAsync",
                        new object[] { project.Id.Id, "Foo", },
                        cancellationToken)
                                     .ConfigureAwait(false);

                    return(directives);
                }
            }
            catch (Exception exception)
            {
                throw new RazorLanguageServiceException(
                          typeof(DefaultRazorEngineDirectiveResolver).FullName,
                          nameof(GetRazorEngineDirectivesAsync),
                          exception);
            }
        }
Exemple #2
0
        public async Task <RazorEngineDocument> GenerateDocumentAsync(Workspace workspace, Project project, string filePath, string text, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var client = await RazorLanguageServiceClientFactory.CreateAsync(workspace, cancellationToken);

                using (var session = await client.CreateSessionAsync(project.Solution))
                {
                    var document = await session.InvokeAsync <RazorEngineDocument>(
                        "GenerateDocumentAsync",
                        new object[] { project.Id.Id, "Foo", filePath, text },
                        cancellationToken)
                                   .ConfigureAwait(false);

                    return(document);
                }
            }
            catch (Exception exception)
            {
                throw new RazorLanguageServiceException(
                          typeof(DefaultRazorEngineDocumentGenerator).FullName,
                          nameof(GenerateDocumentAsync),
                          exception);
            }
        }
        public async Task <TagHelperResolutionResult> GetTagHelpersAsync(Project project)
        {
            try
            {
                TagHelperResolutionResult result;

                // We're being overly defensive here because the OOP host can return null for the client/session/operation
                // when it's disconnected (user stops the process).
                //
                // This will change in the future to an easier to consume API but for VS RTM this is what we have.
                var client = await RazorLanguageServiceClientFactory.CreateAsync(Workspace, CancellationToken.None);

                if (client != null)
                {
                    using (var session = await client.CreateSessionAsync(project.Solution))
                    {
                        if (session != null)
                        {
                            var jsonObject = await session.InvokeAsync <JObject>(
                                "GetTagHelpersAsync",
                                new object[] { project.Id.Id, "Foo", },
                                CancellationToken.None
                                ).ConfigureAwait(false);

                            result = GetTagHelperResolutionResult(jsonObject);

                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                }

                // The OOP host is turned off, so let's do this in process.
                var compilation = await project.GetCompilationAsync(CancellationToken.None).ConfigureAwait(false);

                result = GetTagHelpers(compilation, designTime: true);
                return(result);
            }
            catch (Exception exception)
            {
                var log = GetActivityLog();
                if (log != null)
                {
                    var hr = log.LogEntry(
                        (uint)__ACTIVITYLOG_ENTRYTYPE.ALE_ERROR,
                        "Razor Language Services",
                        $"Error discovering TagHelpers:{Environment.NewLine}{exception}");
                    ErrorHandler.ThrowOnFailure(hr);
                }

                throw new RazorLanguageServiceException(
                          typeof(DefaultTagHelperResolver).FullName,
                          nameof(GetTagHelpersAsync),
                          exception);
            }
        }
Exemple #4
0
        public async Task <TagHelperResolutionResult> GetTagHelpersAsync(Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            try
            {
                TagHelperResolutionResult result;

                // We're being overly defensive here because the OOP host can return null for the client/session/operation
                // when it's disconnected (user stops the process).
                //
                // This will change in the future to an easier to consume API but for VS RTM this is what we have.
                var client = await RazorLanguageServiceClientFactory.CreateAsync(_workspace, CancellationToken.None);

                if (client != null)
                {
                    using (var session = await client.CreateSessionAsync(project.Solution))
                    {
                        if (session != null)
                        {
                            var jsonObject = await session.InvokeAsync <JObject>(
                                "GetTagHelpersAsync",
                                new object[] { project.Id.Id, "Foo", },
                                CancellationToken.None).ConfigureAwait(false);

                            result = GetTagHelperResolutionResult(jsonObject);

                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                }

                // The OOP host is turned off, so let's do this in process.
                var compilation = await project.GetCompilationAsync(CancellationToken.None).ConfigureAwait(false);

                result = GetTagHelpers(compilation);
                return(result);
            }
            catch (Exception exception)
            {
                _errorReporter.ReportError(exception, project);

                throw new InvalidOperationException(
                          Resources.FormatUnexpectedException(
                              typeof(DefaultTagHelperResolver).FullName,
                              nameof(GetTagHelpersAsync)),
                          exception);
            }
        }
        public override async Task <TagHelperResolutionResult> GetTagHelpersAsync(Project project, CancellationToken cancellationToken)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            try
            {
                TagHelperResolutionResult result = null;

                // We're being defensive here because the OOP host can return null for the client/session/operation
                // when it's disconnected (user stops the process).
                var client = await RazorLanguageServiceClientFactory.CreateAsync(_workspace, cancellationToken);

                if (client != null)
                {
                    using (var session = await client.CreateSessionAsync(project.Solution))
                    {
                        if (session != null)
                        {
                            var jsonObject = await session.InvokeAsync <JObject>(
                                "GetTagHelpersAsync",
                                new object[] { project.Id.Id, "Foo", },
                                cancellationToken).ConfigureAwait(false);

                            result = GetTagHelperResolutionResult(jsonObject);
                        }
                    }
                }

                if (result == null)
                {
                    // Was unable to get tag helpers OOP, fallback to default behavior.
                    result = await _defaultResolver.GetTagHelpersAsync(project, cancellationToken);
                }

                return(result);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException(
                          Resources.FormatUnexpectedException(
                              typeof(DefaultTagHelperResolver).FullName,
                              nameof(GetTagHelpersAsync)),
                          exception);
            }
        }
        protected virtual async Task <TagHelperResolutionResult> ResolveTagHelpersOutOfProcessAsync(IProjectEngineFactory factory, ProjectSnapshot project)
        {
            // We're being overly defensive here because the OOP host can return null for the client/session/operation
            // when it's disconnected (user stops the process).
            //
            // This will change in the future to an easier to consume API but for VS RTM this is what we have.
            try
            {
                var client = await RazorLanguageServiceClientFactory.CreateAsync(_workspace, CancellationToken.None);

                if (client != null)
                {
                    using (var session = await client.CreateSessionAsync(project.WorkspaceProject.Solution))
                    {
                        if (session != null)
                        {
                            var args = new object[]
                            {
                                Serialize(project),
                                factory == null ? null : factory.GetType().AssemblyQualifiedName,
                            };

                            var json = await session.InvokeAsync <JObject>("GetTagHelpersAsync", args, CancellationToken.None).ConfigureAwait(false);

                            return(Deserialize(json));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // We silence exceptions from the OOP host because we don't want to bring down VS for an OOP failure.
                // We will retry all failures in process anyway, so if there's a real problem that isn't unique to OOP
                // then it will report a crash in VS.
                _errorReporter.ReportError(ex, project);
            }

            return(null);
        }