Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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# ServiceBus抛出未经授权的错误_C#_Asp.net Mvc 4_Iis 7.5_Servicebus - Fatal编程技术网

C# ServiceBus抛出未经授权的错误

C# ServiceBus抛出未经授权的错误,c#,asp.net-mvc-4,iis-7.5,servicebus,C#,Asp.net Mvc 4,Iis 7.5,Servicebus,我正在使用Windows Service Bus 1.0代理消息的一个简单实现来跟踪用户与特定web应用程序的交互 每次将某些内容保存到数据库中的“敏感”表时,我都会设置存储库层,并发送如下消息: ServiceBus.MessageQueue<T>.PushAsync(entity); 当我在本地调试它时,它可以完美地工作。但是,当我在IIS中发布站点并运行时,namespaceManager.QueueExists(QueueName)调用失败,异常显示为“401 Unauth

我正在使用Windows Service Bus 1.0代理消息的一个简单实现来跟踪用户与特定web应用程序的交互

每次将某些内容保存到数据库中的“敏感”表时,我都会设置存储库层,并发送如下消息:

ServiceBus.MessageQueue<T>.PushAsync(entity);
当我在本地调试它时,它可以完美地工作。但是,当我在IIS中发布站点并运行时,
namespaceManager.QueueExists(QueueName)
调用失败,异常显示为“401 Unauthorized error”

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


我错过什么了吗?如果是,是什么?非常感谢您的帮助

Chameera,你读过文档中的安全部分了吗


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

谢谢克莱门斯,我似乎完全忽略了这一点。我觉得这是合乎逻辑的解决方案,所以我会将其标记为答案,即使我还没有测试出来。
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;
        }

    }
}