从 windows 服务访问服务总线 1.1

Accessing Service Bus 1.1 from windows Service

我已经为 windows 服务器 设置了 服务总线 1.1,并尝试使用以下代码访问它。

    var sbUriList = new List<Uri>() { new UriBuilder { Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace }.Uri };
    var httpsUriList = new List<Uri>() { new UriBuilder { Scheme = "https", Host = ServerFQDN, Path = ServiceNamespace, Port = HttpPort }.Uri };

    NetworkCredential credential = new NetworkCredential("<User Name>", "<Password>", "<Domain>");

    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((s, cert, chain, ssl) => { return true; });

    TokenProvider tokenProvider = TokenProvider.CreateOAuthTokenProvider(httpsUriList, credential);
    messageFactory = MessagingFactory.Create(sbUriList, tokenProvider);



    ServiceBusConnectionStringBuilder connBuilder = new ServiceBusConnectionStringBuilder();
    connBuilder.ManagementPort = HttpPort;
    connBuilder.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);

    namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());

if (!namespaceManager.QueueExists(queuename))
{
    namespaceManager.CreateQueue(queuename);
}

如果我 运行 我的代码来自控制台应用程序,这工作正常,但是如果我把它放在 windows 服务 和 运行 它在 本地服务 本地系统 下,代码在尝试检查队列是否存在于以下行时抛出以下异常 namespaceManager.QueueExists(queuename).

Unexpected exception : System.UnauthorizedAccessException: The remote server returned an error: (401) Unauthorized. Manage claim is required for this operation..TrackingId:5be1365e-b4ae-4555-b81b-dcbef96be9d0_GIE11LT32PD622,TimeStamp:4/19/2015 3:51:28 PM ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at Microsoft.ServiceBus.Messaging.ServiceBusResourceOperations.GetAsyncResult`1.<GetAsyncSteps>b__2d(GetAsyncResult`1 thisPtr, IAsyncResult r)
   at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
   --- End of inner exception stack trace ---

有人可以帮我理解我做错了什么吗?

终于在我的代码中找到了问题,我想我会分享它以防其他人遇到同样的问题。

我的问题是在命名空间中我没有设置令牌如下:

namespaceManager.Settings.TokenProvider = tokenProvider;

因此它使用了错误的连接令牌,因此出现了错误。