Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Security.SecurityException:该程序集不允许部分受信任的调用方_C#_Sql Server - Fatal编程技术网

C# System.Security.SecurityException:该程序集不允许部分受信任的调用方

C# System.Security.SecurityException:该程序集不允许部分受信任的调用方,c#,sql-server,C#,Sql Server,我正在尝试使用集成在SQLServer2008中的clr存储过程创建一个简单的msmq消息传递应用程序 遵循以下步骤 由defualt System.Messaging引用在sql server中不可用,因此创建了此程序集 将ALTER DATABASE[AdventureWorksLT2008]设置为可信 创建程序集消息传递 授权dbo 来自“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Messaging.dll” 具有权限设置=外

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

遵循以下步骤

由defualt System.Messaging引用在sql server中不可用,因此创建了此程序集

将ALTER DATABASE[AdventureWorksLT2008]设置为可信 创建程序集消息传递 授权dbo 来自“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Messaging.dll” 具有权限设置=外部访问权限 去

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);
                }
            }
         }     
     }
已在sql server中注册该程序集 从“E:\CCA Parctise\SqlMSMQ.dll”创建程序集MSMQApp SqlMSMQ.dll是sql server应用程序dll

创建存储过程 创建过程[dbo]。[Proc\U Send\U Queue] @队列nvarchar, @味精nvarchar 以EXECUTE作为调用方 像 外部名称[MSMQApp]。[CLRProdure.StoredProcess]。[Proc\u Send\u Queue] 在执行存储过程时 使用[AdventureWorksLT2008] 去 声明@return\u值int EXEC@return\u value=[dbo].[Proc\u Send\u Queue] @队列=N'SampleQ',-msmq名称 @msg=N'Simpel队列消息'-消息 选择“返回值”=@返回值 去

抛下误差

Msg 6522,第16级,状态1,过程进程发送队列,第0行 在执行用户定义例程或聚合过程发送队列期间发生.NET Framework错误: System.Security.SecurityException:该程序集不允许部分受信任的调用方。 System.Security.SecurityException: 在CLRProcedure.StoredProcedure.Proc_Send_QueueSqlString queue,SqlString msg

感谢您帮助解决此问题

提前感谢

声明:

建议在SQL Server中注册所有程序集,但 添加到全局程序集缓存的那些程序集将标记为 AllowPartiallyTrustedCallers属性,以便加载程序集 通过SQL Server可以相互访问

你完全按照推荐书上说的做了,你得到了错误。发生了什么事?见:

当程序集B调用强命名程序集A时

A必须具有AllowPartiallyTrustedCallers属性,或者B必须 设置了不安全的FullTrust权限

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

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

您的邮件的收件人是什么?如果是另一个由SQL Server支持的应用程序,那么您有一个更好的选择。

您好,Remus Rusanu,是的,在使用权限\u set=unsafe注册程序集B后,它工作了。谢谢你的帮助。