是否想在运行时将装配体加载到装配体中?

时间:2018-10-11 12:31:44

标签: c# c#-4.0 ssis ssis-2012 system.reflection

我想使用 ApplicationTokenProvider.LoginSilentAsync(tenantId,clientCredential).Result ,它引用了 Microsoft.Rest.Azure.Authentication 命名空间。此Microsoft.Rest.Azure.Authentication命名空间位于 Microsoft.Rest.ClientRuntime.Azure.Authentication 名称空间(dll)中。

我想在SSIS脚本任务中使用它,这就是为什么我必须在运行时使用 System.Reflection.Assembly.LoadFile(“ path”)从本地文件存储中加载该名称空间的原因。

>

由于Microsoft.Rest.Azure.Authentication没有dll文件,因此它在运行时给我一个错误。

请说明如何在运行时加载此名称空间。

using Microsoft.IdentityModel.Clients.ActiveDirectory;

using Microsoft.Azure.Management.DataLake.Store; using Microsoft.Rest.Azure.Authentication; using System.Net.Http;

public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
    static ScriptMain()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    }
    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
       if (args.Name.Contains("Newtonsoft.Json"))
        {
            return System.Reflection.Assembly.LoadFile(@"D:\Azure_DLL\Newtonsoft.Json.dll");
        }

        if (args.Name.Contains("Microsoft.Azure.DataLake.Store"))
        {
            return System.Reflection.Assembly.LoadFile(@"D:\Azure_DLL\Microsoft.Azure.DataLake.Store.dll");
        }

        if (args.Name.Contains("Microsoft.Rest.ClientRuntime.Azure.Authentication"))
        {
            return System.Reflection.Assembly.LoadFile(@"D:\Azure_DLL\Microsoft.Rest.ClientRuntime.Azure.Authentication.dll");
        }

        if (args.Name.Contains("Microsoft.Rest.ClientRuntime"))
        {
            return System.Reflection.Assembly.LoadFile(@"D:\Azure_DLL\Microsoft.Rest.ClientRuntime.dll");
        }

        if (args.Name.Contains("System.Net.Http"))
        {
            return System.Reflection.Assembly.LoadFile(@"D:\Azure_DLL\System.Net.Http.dll");
        }

        return null;
    }
   public void Main()
    {
        // TODO: Add your code here
        // 1. Set Synchronization Context

        DataLakeStoreFileSystemManagementClient adlsFileSystemClient;

        // Portal > Azure AD > App Registrations > App > Application ID (aka Client ID)
        string clientId = "0e91";

        // Portal > Azure AD > App Registrations > App > Settings > Keys (aka Client Secret)
        string clientSecret = "6sHH8s7eT18=";

        // Portal > Azure AD > Properties > Directory ID (aka Tenant ID)
        string tenantId = "17788a7";

        // Name of the Azure Data Lake Store
        string adlsAccountName = "sjmadls08";


        string connectionString = "Data Source=SAGARM-PC;Initial Catalog=Destinantion;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;";

        var bytes = datared("Emp", connectionString);
        MemoryStream stream = new MemoryStream(bytes);

        // 1. Set Synchronization Context
        SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());

        // 2. Create credentials to authenticate requests as an Active Directory application
        var clientCredential = new ClientCredential(clientId, clientSecret);
        var creds = ApplicationTokenProvider.LoginSilentAsync(tenantId, clientCredential).Result;

        //// 2. Initialise Data Lake Store File System Client
        //adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds);

        //// 3. Upload a file to the Data Lake Store
        //var destinationfilepath = "/shared/Emp_data_CurrentDate.txt";
        //   adlsFileSystemClient.FileSystem.Create(adlsAccountName, destinationfilepath, overwrite: true);
        //  adlsFileSystemClient.FileSystem.Append(adlsAccountName, destinationfilepath, stream);

        Dts.TaskResult = (int)ScriptResults.Success;
    }}

错误- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

我也尝试过,但是-

var DLL = Assembly.LoadFrom(@“ D:\ Azure_DLL \ Microsoft.Rest.ClientRuntime.Azure.Authentication.dll”);

 Type t = DLL.GetType("Microsoft.Rest.Azure.Authentication.ApplicationTokenProvider");

var methodInfo = t.GetMethod(“ LoginSilentAsync”,new Type [] {typeof(string),typeof(ClientCredential)});

        var domain = "1dfgf88a7";
        var clientId = "0e499fdfgdf2b7131ee91";
        var clientSecret = "6vagtMMPAaZdfgdfs7eT18=";
        var clientCredential = new ClientCredential(clientId, clientSecret);


        // Gives status wwaiting for activation. 
        var a = methodInfo.Invoke(null, new object[] { domain, clientCredential });

        //Gives me the error No parameter less constructor
        object instance = Activator.CreateInstance(t);

`

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,但是经过数小时的各种解决方案爬行后,我设法找到了一个稍有不同的实现here

所以我添加了以下代码:

NULL

并使用以下内容在Main()中对其进行引用:

private static ServiceClientCredentials AuthenticateAzure
    (string domainName, string clientID, string clientSecret)
    {
        SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());

        var clientCredential = new ClientCredential(clientID, clientSecret);
        return ApplicationTokenProvider.LoginSilentAsync(domainName, clientCredential).Result;
    }

哪个返回了实际错误: System.IO.FileNotFoundException:无法加载文件或程序集'Microsoft.IdentityModel.Clients.ActiveDirectory,版本= 2.28.3.860

我最初使用NuGet安装的版本是5.2.1.0。

我知道它有点黑,但是我下载了Version = 2.28.3.860,并替换了解决此问题的较新版本。

此外,似乎Newtonsoft.Json程序集也存在问题,该版本必须为版本6。

对于在运行时进行调试,似乎这些dll需要复制到“ C:\ Users \\ AppData \ Local \ Microsoft \ Microsoft SQL Server \ 140 \ SSIS \ 130 \ x64”

当然,这取决于您的版本。