这是我为使事情正常工作所做的。
我开始在我的开发机器(Win8/Studio2012)和我的服务器(Win2k8R2)上创建相同的本地用户帐户。我导出了上面引用的客户端证书(实际上是 2 或 3 次)并根据需要导入。一旦我建立了良好的连接工作,我就开始退出。删除了我的 Win8 机器上的本地帐户等。然后一切都开始神奇地工作。
关于我周围的一些事情。我正在使用 VMWare Fusion 完成所有这些工作。(是的融合)。VM 机器都在网络上配置了 NAT,没有静态 IP。
不知道如何,但它正在工作。我只是先发消息。我想如果我可以让消息正常工作,那么我可以关注 Pub/Sub 主题……接下来就是。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Diagnostics;
using System.Security;
namespace Test_Service_Bus
{
public static class Util
{
public static void SetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
}
private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
System.Console.WriteLine("Warning, trust any certificate");
return true;
}
}
class Program
{
static string ServerFQDN = "SVRNICK"; //System.Net.Dns.GetHostEntry(string.Empty).HostName;
static int HttpPort = 9355;
static int TcpPort = 9354;
static string ServiceNameSpace = "ServiceBusDefaultNamespace";
const string QueueName = "ServiceBusQueueSample";
static void Main(string[] args)
{
SDKVersion();
}
static void SDKVersion()
{
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);
TokenProvider localUserTokenProvider = WindowsTokenProvider.CreateWindowsTokenProvider(connBuilder.StsEndpoints, new System.Net.NetworkCredential("ServerLocalAcct", "ServerLocalPassword"));
MessagingFactory messageFactory = MessagingFactory.Create(connBuilder.GetAbsoluteRuntimeEndpoints(), localUserTokenProvider);
NamespaceManager namespaceManager = new NamespaceManager(connBuilder.GetAbsoluteManagementEndpoints(), localUserTokenProvider);
NamespaceManager.CreateFromConnectionString(connBuilder.ToString());
if (namespaceManager == null)
{
Console.WriteLine("\nUnexpected error: NamespaceManager is NULL");
return;
}
Console.WriteLine("Checking if queue exists");
if (namespaceManager.QueueExists(QueueName))
{
Console.WriteLine("Queue exists, let's delete it so we can start over again");
namespaceManager.DeleteQueue(QueueName);
}
Console.WriteLine("And create a new queue");
namespaceManager.CreateQueue(QueueName);
// create a message client
QueueClient myQueueClient = messageFactory.CreateQueueClient(QueueName);
Console.WriteLine("Feeding messages");
for (int i = 0; i < 1000; i++)
{
if (i % 100 == 0)
Console.WriteLine("Messages sent: {0}", i.ToString());
// send to message broker
using (BrokeredMessage sendMessage = new BrokeredMessage("Hello world. " + i.ToString()))
{
sendMessage.Label = "Label_" + i.ToString(); ;
myQueueClient.Send(sendMessage);
}
}
BrokeredMessage receivedMessage = myQueueClient.Receive(TimeSpan.FromSeconds(5));
while (receivedMessage != null)
{
Console.WriteLine(string.Format("Message Received: Body={0}", receivedMessage.GetBody<string>()));
receivedMessage.Complete();
receivedMessage = myQueueClient.Receive(TimeSpan.FromSeconds(5));
}
// Close things down.
Console.WriteLine("Closing down message");
if (messageFactory != null)
{
messageFactory.Close();
}
}
}
}