【问题标题】:System.Security.SecurityException: That assembly does not allow partially trusted callersSystem.Security.SecurityException:该程序集不允许部分受信任的调用者
【发布时间】:2012-08-05 06:33:18
【问题描述】:

我正在尝试使用集成在 sql server 2008 中的 clr 存储过程创建一个简单的 msmq 消息传递应用程序。

按照以下步骤进行

  1. 默认System.Messaging引用在sql server中不可用,所以创建了这个程序集

    ALTER DATABASE [AdventureWorksLT2008] 设置信任开启 创建组装消息 授权 dbo FROM 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Messaging.dll' WITH PERMISSION_SET = EXTERNAL_ACCESS 去

2.创建带有存储过程的sql server应用程序

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using System.Messaging;
using System.Security;

    [assembly: AllowPartiallyTrustedCallers]
    namespace CLRProcedure
    {

        public class StoredProcedure
        {
            /// <summary>
            /// Sends message to queue
            /// </summary>
            /// <param name="queue">Queue path</param>
            /// <param name="msg">Message</param>
            [SqlProcedure]

            public static void Proc_Send_Queue(SqlString queue, SqlString msg)
            {
                using (MessageQueue msgQueue = new MessageQueue(queue.ToString(), QueueAccessMode.Send))
                {
                    msgQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                    msgQueue.Send(msg.Value);
                }
            }
         }     
     }
  1. 在 sql server 中注册了该程序集 从“E:\CCA Parctise\SqlMSMQ.dll”创建程序集 MSMQApp

SqlMSMQ.dll 是 sql server 应用程序 dll。

  1. 创建存储过程 创建过程 [dbo].[Proc_Send_Queue] (@queue nvarchar, @msg nvarchar) 以调用者身份执行 作为 外部名称 [MSMQApp].[CLRProcedure.StoredProcedure].[Proc_Send_Queue]
  2. 在执行存储过程时 使用 [AdventureWorksLT2008] 走 声明 @return_value int 执行@return_value = [dbo].[Proc_Send_Queue] @queue = N'SampleQ', --msmq 名称 @msg = N'Simpel 队列消息' -- 消息 选择“返回值”=@return_value 去

    抛出错误

    消息 6522,级别 16,状态 1,过程 Proc_Send_Queue,第 0 行 执行用户定义的例程或聚合“Proc_Send_Queue”期间发生 .NET Framework 错误: System.Security.SecurityException:该程序集不允许部分受信任的调用者。 System.Security.SecurityException: 在 CLRProcedure.StoredProcedure.Proc_Send_Queue(SqlString queue, SqlString msg)

感谢您帮助解决此问题。

提前致谢

【问题讨论】:

    标签: c# sql-server


    【解决方案1】:

    Allowing Partially Trusted Callers 状态:

    我们建议所有在 SQL Server 中注册的程序集,除了 那些添加到全局程序集缓存的程序集,用 AllowPartiallyTrustedCallers 属性,以便加载程序集 通过 SQL Server 可以互相访问。

    你完全按照建议所说的做了,但你得到了错误。发生什么了?见Strong named assemblies and AllowPartiallyTrustedCallers:

    当程序集 B 调用强名称程序集 A 时,

    要么 A 必须具有 AllowPartiallyTrustedCallers 属性,要么 B 必须 具有不安全 (FullTrust) 权限集。

    在您的情况下,“B”是SqlMSMQ.dll,强名称“A”是System.Messaging.dll。由于您无法将“A”修改为具有 APTC 属性,因此唯一的解决方案是将“B”修改为完全受信任(即 UNSAFE):

     create assembly MSMQApp from 'E:\CCA Parctise\SqlMSMQ.dll'
       with permission_set = unsafe;
    

    您的邮件的收件人是什么?如果是另一个由 SQL Server 支持的应用程序,那么您可以在 Service Broker 中找到更好的选择。

    【讨论】:

    • 嗨 Remus Rusanu,是的,它在使用 permission_set = unsafe 注册程序集 B 后工作。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多