我想让一个 ASP.NET 应用程序使用 RabbitMQ 的本机客户端写入消息队列。为此,我将 RabbitMQ dll 放在我的 Web 应用程序的 /bin 目录中,并创建了一个 .aspx 测试页面。
当我测试脚本时,我收到以下异常 System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed。
进一步调查问题,我相信这是因为 dll 物理存储在另一台计算机上的共享目录中。研究使我通过执行以下操作来调整代码访问安全策略:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\CasPol.exe -m -ag 1 -url "file://\\server\IISSharedContent\webfarm\stage\dev-stage\bin\*" FullTrust -exclusive on
重新启动 IIS 后似乎没有用。
我也试过:
- 将应用程序池标识设置为网络服务
- 将应用程序池“加载用户配置文件”设置为 True
- 添加到 web.config
有没有人有任何可以帮助我的见解?
全栈跟踪:
[SecurityException: Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessPermission.Demand() +54
System.Environment.GetEnvironmentVariable(String variable) +135
RabbitMQ.Client.Protocols.ReadEnvironmentVariable() +22
RabbitMQ.Client.Protocols.FromEnvironment(String appSettingsKey) +37
RabbitMQ.Client.ConnectionFactory..ctor() +176
ASP.api_messagesearch_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in \\server\iissharedcontent\webfarm\stage\dev-stage\api\MessageSearch.aspx:9
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +115
System.Web.UI.Page.Render(HtmlTextWriter writer) +38
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11070247
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11069786
System.Web.UI.Page.ProcessRequest() +91
System.Web.UI.Page.ProcessRequest(HttpContext context) +240
ASP.api_messagesearch_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\cb973a2c\79ccea1e\App_Web_x0q-uejx.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +599
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171
我的测试脚本的代码:
ConnectionFactory rabbitConFactory = new ConnectionFactory();
rabbitConFactory.Address = "hostname";
rabbitConFactory.VirtualHost = "/path";
rabbitConFactory.UserName = "user";
rabbitConFactory.Password = "password";
IConnection connection = rabbitConFactory.CreateConnection();
IModel channel = connection.CreateModel();
Dictionary<String, Object> args = new Dictionary<string, object>();
args.Add("x-ha-policy", "all");
String ExchangeName = "SearchInstructions";
channel.ExchangeDeclare(ExchangeName, "topic", true, false, args);
Guid myId = new Guid();
String RoutingKey = myId.ToString() + ".Instruction";
IBasicProperties MessageProperties = channel.CreateBasicProperties();
MessageProperties.SetPersistent(true);
MessageProperties.ContentEncoding = "UTF-8";
MessageProperties.ContentType = "text/xml";
MessageProperties.Headers.Add("x-origin",
Request.ServerVariables["ServerName"]);
String messageContent = "<foo />";
byte[] data = System.Text.Encoding.UTF8.GetBytes(messageContent);
channel.BasicPublish(ExchangeName,RoutingKey, MessageProperties, data);
channel.Close();
connection.Close();