首页 > 解决方案 > 无法通过 Azure Function App 连接到 Azure Analysis Services - 无法加载类型“System.Security.Principal.WindowsImpersonationContext”

问题描述

按照指南,我创建了一个可编译并运行的 Azure Function App:

#r "Microsoft.AnalysisServices.Tabular.DLL"
#r "Microsoft.AnalysisServices.Core.DLL"
#r "System.Configuration"

using System;
using System.Configuration;
using Microsoft.AnalysisServices.Tabular;

// req is a value passed from function.json which is currently ignored
public static void Run(String req, ILogger log)
{
    log.LogInformation($"C# Timer trigger function started at: {DateTime.Now}");

    try
            {
                Microsoft.AnalysisServices.Tabular.Server asSrv = new Microsoft.AnalysisServices.Tabular.Server();

                // Pulls the connection string from the Connection Strings collection within the Application Settings
                //var connStr = ConfigurationManager.ConnectionStrings["AASDev"].ConnectionString;

                // Pulls the connection string from an Environment Variable held within the Application Settings collection
                //var connStr = GetEnvironmentVariable("Conn");

                // Simply hard coding the connection string (Anonymised)
                var connStr = @"Data Source=asazure://<roll out>.asazure.windows.net/<My AAS Instance>;Initial Catalog=<My AAS Database>;User ID=<My User>;Password=<My Pass>";

                asSrv.Connect(connStr);
                Database db = asSrv.Databases["<My AAS Database>"];
                Model m = db.Model;
                m.Tables["<My AAS Table>"].RequestRefresh(RefreshType.Full);     // Mark only one table for refresh
                db.Model.SaveChanges();     //commit  which will execute the refresh
                asSrv.Disconnect();
            }
            catch (Exception e)
            {
                log.LogInformation($"C# Timer trigger function exception: {e.ToString()}");
            }
    log.LogInformation($"C# Timer trigger function finished at: {DateTime.Now}"); 
}

asSrv但是,在运行该函数时,当它尝试连接到变量中保存的服务器对象时会引发异常:

2018-11-12T13:45:40.273 [Information] C# Timer trigger function exception: 
System.TypeLoadException:
  Could not load type 'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=<removed>'.  
    at Microsoft.AnalysisServices.IdentityResolver.Dispose()  
    at Microsoft.AnalysisServices.XmlaClient.Connect(ConnectionInfo connectionInfo, Boolean beginSession)  
    at Microsoft.AnalysisServices.Core.Server.Connect(String connectionString, String sessionId, ObjectExpansion expansionType)  
    at Submission#0.Run(TimerInfo myTimer, TraceWriter log)

连接字符串根据示例设置了User IDand Password,无论我如何尝试格式化或检索此连接字符串,它都会引发上述错误或抱怨没有用户或密码省略。

我在这里想念什么?


编辑运行时〜1:

当前收到以下错误消息的许多实例:

2018-11-13T10:02:42.817 [Warning] Unable to find assembly 'Microsoft.VisualStudio, PublicKeyToken=xxx'. Are you missing a private assembly file?
2018-11-13T10:02:42.817 [Warning] Exception during runtime resolution of assembly 'Microsoft.VisualStudio, PublicKeyToken=xxx': 'System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

   at System.AppDomain.nApplyPolicy(AssemblyName an)

   at Microsoft.Azure.WebJobs.Script.Description.FunctionAssemblyLoader.ResolveAssemblyCore(Object sender, ResolveEventArgs args) in C:\projects\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\FunctionAssemblyLoader.cs:line 88'
2018-11-13T10:02:42.817 [Warning] Unable to find assembly 'Microsoft.VisualStudio, PublicKeyToken=xxx'. Are you missing a private assembly file?
2018-11-13T10:02:43.676 [Warning] Unable to find assembly 'Microsoft.AnalysisServices.Tabular.resources, Version=15.0.0.0, Culture=en-US, PublicKeyToken=xxx'. Are you missing a private assembly file?
2018-11-13T10:02:43.676 [Warning] Unable to find assembly 'Microsoft.AnalysisServices.Tabular.resources, Version=15.0.0.0, Culture=en, PublicKeyToken=xxx'. Are you missing a private assembly file?

标签: c#azuressasazure-functionsazure-analysis-services

解决方案


本教程适用于针对 .Net Framework 的运行时 ~1 上的函数。虽然现在 Function 应用程序默认在 .Net Core 运行时 ~2 上。

转到门户,平台功能> 应用程序设置,在应用程序设置部分下,设置FUNCTIONS_EXTENSION_VERSION为 ~1。请注意,之前创建的所有函数都会受到影响。

更新

如果没有 .Net Core SDK 支持,我们可能无法使其在 Funtion 2.x 运行时中运行。那些在 ~1 运行时中缺少程序集的警告应该是无害的。

如果遇到错误On-Premise Gateway is required to access the data source. Please install a unified gateway for the server,请按照本教程进行安装。


推荐阅读