2013-09-01 50 views
2

我正在使用Windows Service Bus 1.0的简单实现中断消息传递以跟踪用户与特定Web应用程序的交互。ServiceBus引发401未授权错误

每次的东西被保存到“敏感”表在数据库中,我已经设置了存储库层发送消息,像这样:

ServiceBus.MessageQueue<T>.PushAsync(entity); 

这将随后序列化实体,并创建一个消息出来的它。我的MessageQueue类是这样的。

public static class MessageQueue<T> 
{ 
    static string ServerFQDN; 
    static int HttpPort = 9355; 
    static int TcpPort = 9354; 
    static string ServiceNamespace = "ServiceBusDefaultNamespace"; 

    public static void PushAsync(T msg) 
    { 
     ServerFQDN = System.Net.Dns.GetHostEntry(string.Empty).HostName; 

     //Service Bus connection string 
     var connBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort }; 
     connBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace }.Uri); 
     connBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = ServerFQDN, Port = HttpPort, Path = ServiceNamespace}.Uri); 

     //Create a NamespaceManager instance (for management operations) and a MessagingFactory instance (for sending and receiving messages) 
     MessagingFactory messageFactory = MessagingFactory.CreateFromConnectionString(connBuilder.ToString()); 
     NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString()); 

     if (namespaceManager == null) 
     { 
      Console.WriteLine("\nUnexpected Error"); 
      return; 
     } 

     //create a new queue 
     string QueueName = "ServiceBusQueueSample"; 
     if (!namespaceManager.QueueExists(QueueName)) 
     { 
      namespaceManager.CreateQueue(QueueName); 
     } 

     try 
     {    
      QueueClient myQueueClient = messageFactory.CreateQueueClient(QueueName); 

      string aaa = JsonConvert.SerializeObject(msg, Formatting.Indented, 
          new JsonSerializerSettings() 
          { 
           ReferenceLoopHandling = ReferenceLoopHandling.Ignore, 
           ContractResolver = new NHibernateContractResolver() 
          }); 

      BrokeredMessage sendMessage1 = new BrokeredMessage(aaa); 
      sendMessage1.Properties.Add("UserName",Thread.CurrentPrincipal.Identity.Name); 
      sendMessage1.Properties.Add("TimeStamp", ApplicationDateTime.Now); 
      sendMessage1.Properties.Add("Type", msg.GetType().Name); 


      myQueueClient.Send(sendMessage1); 

     } 
     catch (Exception e) 
     { 
      var l = new Logger(); 
      l.Log(LogEventEnum.WebrequestFailure, e.Message); 
      Console.WriteLine("Unexpected exception {0}", e.ToString()); 
      throw; 
     } 

    } 
} 

当我在本地进行调试时,此功能完美无缺。但是,当我在IIS中发布站点并运行时,namespaceManager.QueueExists(QueueName)调用失败,并出现一个表示“401未授权错误”的异常。

当我将应用程序池标识(在IIS中)更改为管理员帐户时,不会发生此错误。但是,当我们去生产时,我可以做到这一点,这绝对是没有

我错过了什么吗?如果是这样,那是什么?任何帮助是极大的赞赏。

回答

3

您是否阅读过文档中的安全部分Chameera? http://msdn.microsoft.com/en-us/library/windowsazure/jj193003(v=azure.10).aspx

您似乎在使用默认安全设置运行,这意味着您只有管理员帐户授权。查看文档部分并授予您想要在产品中使用的帐户的必要权利。

+0

谢谢克莱门斯,我似乎完全错过了。我觉得这是一个合理的解决方案,所以我会将其标记为答案,即使我尚未对其进行测试。 –