在项目中嵌入SCCM 2012 SDK程序集.dll后出现未处理的ArgumentException

本文关键字:dll 未处理 ArgumentException 程序集 SDK 项目 2012 SCCM | 更新日期: 2023-09-27 18:27:36

我的项目实际上需要不少于7.DLL(来自Microsoft System Center 2012 R2 Configuration Manager SDK),我真的很想提供一个.EXE,而不是一大堆文件。到目前为止,代码运行得很顺利。。。然后我把我的程序集嵌入到项目中,混乱开始了!

我想知道我做错了什么。。。让我们回顾一下我实现这一目标所遵循的步骤:

1-program.cs使用AssemblyResolve事件处理程序加载嵌入式程序集是相当简单的:

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        public static Dictionary<string, Assembly> _libs = new Dictionary<string, Assembly>();
        /// <summary>
        /// Point d'entrée principal de l'application.
        /// </summary>        
        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                lock (_libs)
                {
                    string name = args.Name.Split(',')[0];
                    string culture = args.Name.Split(',')[2];
                    if (name.EndsWith(".resources") && !culture.EndsWith("neutral")) return null;
                    String resourceName = "WindowsFormsApplication1." + new AssemblyName(args.Name).Name + ".dll";
                    if (_libs.ContainsKey(resourceName)) return _libs[resourceName];
                    using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                    {
                        Byte[] assemblyData = new Byte[stream.Length];
                        stream.Read(assemblyData, 0, assemblyData.Length);
                        Assembly assembly = Assembly.Load(assemblyData);
                        _libs[resourceName] = assembly;
                        return Assembly.Load(assemblyData);
                    }
                }
            };
            System.Windows.Forms.Application.EnableVisualStyles();
            System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
            System.Windows.Forms.Application.Run(new Form1());
        }
    }   
}

2-表格1.cs
注意,我在演示中只使用了7个DLL中的2个:

using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string SCCM_SERVER = "sccm.server.fqdn.com";
        private static WqlConnectionManager sccmConnection = new WqlConnectionManager();                

        public Form1()
        {
            try
            {
                sccmConnection.Connect(SCCM_SERVER);
            }
            catch (SmsException ex)
            {
                MessageBox.Show("Unable to connect to server. " + ex.Message);
            }
            catch (System.UnauthorizedAccessException ex2)
            {
                MessageBox.Show("Unable to authenticate. " + ex2.Message);
            }
            InitializeComponent();
        }
    }
}

代码编译,正在运行。。。砰!mscorlib引发的未处理的System.ArgumentException:上的"路径不是合法形式"

sccmConnection.Connect(SCCM_SERVER);

再一次,没有嵌入.dll,代码运行良好。。。

3-嵌入.DLL(屏幕截图)->两个引用"复制本地"属性都设置为"False"->Embedded.DLL"Build Action"answers"Copy to Output Dir"都设置为"Embedded resource"answers"Do not Copy"

谢谢你的帮助!

在项目中嵌入SCCM 2012 SDK程序集.dll后出现未处理的ArgumentException

查看异常的堆栈:
àSystem.IO.Path.NormalizePath(字符串路径,布尔值fullCheck,Int32 maxPathLength)
àSystem.IO.Path.GetDirectoryName(字符串路径)
àMicrosoft.ConfigurationManagement.AdminConsole.SmsSqmManager.LoadAndInitializeSqmSdk()
àMicrosoft.ConfigurationManagement.AdminConsole.SmsSqmManager.ctor()
àMicrosoft.ConfigurationManagement.ManagementProvider.ConnectionManagerBase.get_SqmManager()
àMicrosoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine.WqlConnectionManager.Connect(字符串configMgrServerPath)
。。。

现在让我们回顾一下LoadAndInitializeSqmSdk():

// Microsoft.ConfigurationManagement.AdminConsole.SmsSqmManager
private void LoadAndInitializeSqmSdk()
{
    string text = Path.GetDirectoryName(typeof(SmsSqmManager).Assembly.Location);
    text += "''I386''SqmApi.dll";

SmsSqmManager位于Microsoft.ConfigurationManagement.ManagementProvider.dll中,该文件嵌入在我的代码中,因此Assembly.Location返回null并触发Exception。。。

TL;DR:不要嵌入Microsoft.ConfigurationManagement.ManagementProvider.dll…

相关文章: