Пример #1
0
        /// <summary>
        /// Load the assembly into another domain
        /// </summary>
        /// <param name="assemblyBytes"></param>
        /// <param name="isWorkflow"></param>
        /// <param name="assemblyFolderPath"></param>
        /// <returns></returns>
        public static AssemblyContainer LoadAssembly(byte[] assemblyBytes, bool isWorkflow, string assemblyFolderPath)
        {
            var containerAppDomain = AppDomain.CreateDomain(
                "AssemblyContainer",
                AppDomain.CurrentDomain.Evidence,
                new AppDomainSetup
            {
                ApplicationBase = assemblyFolderPath,
                ShadowCopyFiles = "true"
            },
                new PermissionSet(PermissionState.Unrestricted));

            AssemblyContainer assemblyContainer = new AssemblyContainer()
            {
                AssemblyData   = assemblyBytes,
                ReflectionOnly = false,
                Container      = containerAppDomain,
                IsWorkflow     = isWorkflow
            };

            containerAppDomain.DoCallBack(assemblyContainer.LoadAssembly);

            assemblyContainer.PluginDatas = containerAppDomain.GetData("PluginDatas") as List <PluginData>;

            return(assemblyContainer);
        }
        public Guid RegisterWorkflowActivities(string file, string solutionName)
        {
            SolutionUniqueName = solutionName;
            var assemblyFilePath = new FileInfo(file);

            if (_ignoredAssemblies.Contains(assemblyFilePath.Name))
            {
                return(Guid.Empty);
            }

            AssemblyContainer assemblyContainer = null;

            try
            {
                string assemblyFolderPath = assemblyFilePath.DirectoryName;

                //Load the assembly in its own AppDomain to prevent load errors & file locking
                var assemblyBytes = File.ReadAllBytes(file);
                assemblyContainer = AssemblyContainer.LoadAssembly(assemblyBytes, true, assemblyFolderPath);
                List <PluginData> pluginDatas = assemblyContainer.PluginDatas;

                if (pluginDatas.Count <= 0)
                {
                    return(Guid.Empty);
                }

                var plugin = RegisterAssembly(assemblyFilePath, pluginDatas.First().AssemblyName, pluginDatas.First().CrmPluginRegistrationAttributes);

                foreach (PluginData pluginData in pluginDatas)
                {
                    RegisterActivities(pluginData.CrmPluginRegistrationAttributes, plugin, pluginData.AssemblyFullName);
                }

                _ctx.SaveChanges();

                return(plugin.Id);
            }
            finally
            {
                assemblyContainer?.Unload();
            }
        }