2012-08-08 61 views
3

我試圖使用集成在SQL Server CLR存儲過程創建一個簡單的MSMQ消息應用2008System.Security.SecurityException:該程序集不允許部分受信任的調用方

跟着下面的步驟

  1. 通過defualt System.Messaging參考不會是SQL Server可用,所以創造了這個組件

    ALTER DATABASE [AdventureWorksLT2008] SET TRUSTWORTHY上 創建ASSEM BLY消息 AUTHORIZATION DBO FROM 'C:\ Windows \ Microsoft.NET \框架\ V2.0.50727 \ System.Messaging.dll' WITH PERMISSION_SET = EXTERNAL_ACCESS GO

2.created SQL服務器應用程序與存儲過程

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創建組件MSMQApp:\ CCA Parctise \ SqlMSMQ。 dll'

SqlMSMQ.dll是sql server applicaiton dll。

  1. 創建存儲過程 CREATE PROCEDURE [DBO]。[Proc_Send_Queue] (@queue nvarchar的, @msg nvarchar的) WITH EXECUTE AS CALLER AS EXTERNAL NAME [MSMQApp]。[CLRProcedure.StoredProcedure] [Proc_Send_Queue]
  2. 在執行存儲過程 USE [AdventureWorksLT2008] GO DECLARE @return_value詮釋 EXEC @return_value = [DBO] [Proc_Send_Queue] @queue = N'SampleQ '--msmq名 @msg = N'Simpel隊列消息' - 消息 SELECT '返回值'= @return_value GO

    投擲下面誤差

    消息6522,級別16,狀態1,過程Proc_Send_Queue,行用戶定義例程或聚合 「Proc_Send_Queue」 的執行過程中出現0 一個.NET Framework錯誤: System.Security.SecurityException:該程序集不允許部分受信任的呼叫者。 System.Security.SecurityException: 在CLRProcedure.StoredProcedure.Proc_Send_Queue(的SqlString隊列的SqlString MSG)

欣賞你的幫助來解決這個問題。

在此先感謝

回答

2

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」是完全可信的(即不安全的。):

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

什麼是你的消息的接收方?如果是另一個由SQL Server支持的應用程序,那麼在Service Broker中有更好的選擇。

+0

嗨Remus Rusanu,是的,它在使用permission_set = unsafe註冊程序集B後工作。非常感謝您的善意幫助。 – ben1984 2012-08-08 09:13:00

相關問題