public async Task <DotNetCompilationResult> EmitAsync(CancellationToken cancellationToken)
        {
            try
            {
                using (var assemblyStream = new MemoryStream())
                    using (var pdbStream = new MemoryStream())
                    {
                        var compilationWithAnalyzers = _compilation.WithAnalyzers(GetAnalyzers());
                        var diagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync();

                        var emitOptions = new EmitOptions().WithDebugInformationFormat(PlatformHelper.IsWindows ? DebugInformationFormat.Pdb : DebugInformationFormat.PortablePdb);
                        var emitResult  = compilationWithAnalyzers.Compilation.Emit(assemblyStream, pdbStream, options: emitOptions, cancellationToken: cancellationToken);

                        diagnostics = diagnostics.AddRange(emitResult.Diagnostics);

                        if (diagnostics.Any(di => di.Severity == DiagnosticSeverity.Error))
                        {
                            throw new CompilationErrorException("Script compilation failed.", diagnostics);
                        }

                        // Check if cancellation was requested while we were compiling,
                        // and if so quit here.
                        cancellationToken.ThrowIfCancellationRequested();

                        return(DotNetCompilationResult.FromBytes(assemblyStream.GetBuffer(), pdbStream.GetBuffer()));
                    }
            }
            catch (Exception exc) when(!(exc is CompilationErrorException))
            {
                throw new CompilationServiceException($"C# compilation service error: {exc.Message}", exc);
            }
        }
        public Task <DotNetCompilationResult> EmitAsync(CancellationToken cancellationToken)
        {
            DotNetCompilationResult result = null;

            if (DependencyHelper.TryGetAssemblyReference(_assemblyFilePath, out string assemblyName))
            {
                result = DotNetCompilationResult.FromAssemblyName(assemblyName);
            }
            else
            {
                result = DotNetCompilationResult.FromPath(_assemblyFilePath);
            }

            return(Task.FromResult(result));
        }
Example #3
0
        public Task <DotNetCompilationResult> EmitAsync(CancellationToken cancellationToken)
        {
            DotNetCompilationResult result = null;

            if (_assemblyFilePath != null && _assemblyFilePath.StartsWith(AssemblyNamePrefix))
            {
                result = DotNetCompilationResult.FromAssemblyName(_assemblyFilePath.Substring(AssemblyNamePrefix.Length));
            }
            else
            {
                result = DotNetCompilationResult.FromPath(_assemblyFilePath);
            }

            return(Task.FromResult(result));
        }
        private async Task<MethodInfo> CreateFunctionTarget(CancellationToken cancellationToken)
        {
            try
            {
                await VerifyPackageReferencesAsync();

                string eventName = string.Format(MetricEventNames.FunctionCompileLatencyByLanguageFormat, _compilationService.Language);
                using (_metricsLogger.LatencyEvent(eventName, _functionMetadata.Name))
                {
                    IDotNetCompilation compilation = await _compilationService.GetFunctionCompilationAsync(Metadata);

                    DotNetCompilationResult compilationResult = await compilation.EmitAsync(cancellationToken);
                    Assembly assembly = compilationResult.Load(Metadata, _metadataResolver, FunctionLogger);

                    FunctionSignature functionSignature = compilation.GetEntryPointSignature(_functionEntryPointResolver, assembly);

                    ImmutableArray<Diagnostic> bindingDiagnostics = ValidateFunctionBindingArguments(functionSignature, _triggerInputName, _inputBindings, _outputBindings, throwIfFailed: true);
                    TraceCompilationDiagnostics(bindingDiagnostics);

                    _compilerErrorCount = 0;

                    // Set our function entry point signature
                    _functionSignature = functionSignature;

                    var methodInfo = _functionSignature.GetMethod(assembly);
                    _methodInvoker = MethodInvoker.Create(methodInfo);

                    return methodInfo;
                }
            }
            catch (CompilationErrorException exc)
            {
                ImmutableArray<Diagnostic> diagnostics = AddFunctionDiagnostics(exc.Diagnostics);

                // Here we only need to trace to system logs
                TraceCompilationDiagnostics(diagnostics, LogTargets.System);

                throw new CompilationErrorException(exc.Message, diagnostics);
            }
        }
        public Task <DotNetCompilationResult> EmitAsync(CancellationToken cancellationToken)
        {
            EnsureAssemblyOption();

            return(Task.FromResult(DotNetCompilationResult.FromBytes(_assemblyBytes, _pdbBytes)));
        }
Example #6
0
 public Task <DotNetCompilationResult> EmitAsync(CancellationToken cancellationToken)
 {
     return(Task.FromResult(DotNetCompilationResult.FromPath(_assemblyFilePath)));
 }