I’d like to have an ASP.NET application write to a message queue using RabbitMQ’s native client. To do this I’ve put the RabbitMQ dll in the /bin directory of my web application, and created an .aspx test page.
When I test the script I get the following exception System.Security.SecurityException: Request for the permission of type ‘System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.
Looking into the problem further I believe this is being caused because the dll is physically stored on a shared directory on another computer. Research led me to adjusting the Code Access Security policy by doing:
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
After restarting IIS that doesn’t seem to have worked.
I’ve also tried:
- Setting the application-pool identity to Network Service
- Setting the application-pool “Load User Profile” to True
- Adding to the web.config
Does anyone have any insight which could help me?
Full Stack Trace:
[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
Code for my test script:
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();
Okay having struggled with this problem for the better part of two hours I came up with a simple and obvious solution:
Use both the 32 and 64 bit versions of caspol.exe
In order to properly set the trust for this share I had to run caspol.exe twice.
And then Run
I restarted IIS and lo, everything worked like a champ… (well aside from some other bugs).