开发应用程序
应用到: Service Bus for Windows Server 1.0
在开始本教程之前,请确保你已根据安装和配置 Service Bus for Windows Server 中所述正确安装并配置了 Service Bus for Windows Server。
确保有 服务命名空间
在 PowerShell 命令提示符中运行以下命令,以验证你是否至少定义了一个服务命名空间:
get-SBNamespace
此命令的示例输出如下:
Name : myServiceNamespaceName AddressingScheme : Path CreatedTime : 2/29/2012 11:35:09 PM IssuerName : myServiceNamespaceName IssuerUri : myServiceNamespaceName ManageUsers : {list of user accounts} Uri : ServiceIdentifier : PrimarySymmetricKey : myKey SecondarySymmetricKey :
备注 有关服务命名空间的详细信息,请参阅服务命名空间。 在 PowerShell 命令提示符中运行以下命令,以导出 Service Bus for Windows Server 客户端配置文件:
get-sbClientConfiguration > sbclient.config
在 Visual Studio 中创建新项目
在 Visual Studio 2012 中创建新的控制台应用程序。
在解决方案资源管理器中,右键单击“引用”,然后单击“添加引用”。单击以添加 System.Runtime.Serialization 项。
添加对 Service Bus for Windows Server SDK 的引用。
在解决方案资源管理器中,右键单击“引用”,然后单击“管理 NuGet 包”。仅当安装了 NuGet 扩展,才显示此选项。
在“搜索”框中,键入 Service Bus 1.0。单击“Service Bus 1.0 (for Windows Server)”项。完成安装并关闭此对话框。
使用连接字符串和配置文件连接到 Service Bus for Windows Server
Service Bus for Windows Server 可让你在配置文件中指定连接字符串,并在代码中使用该连接字符串而无需指定其他参数。此选项受 Service Bus for Windows Server 的支持,可在部署中提供灵活性。使用连接字符串可以通过更改配置文件中的设置从 Service Bus for Windows Server(本地)切换到 Microsoft Azure Service Bus(云)。
在 PowerShell 命令提示符中运行以下命令,以检索到服务命名空间的连接字符串:
get-SBClientConfiguration
此命令的示例输出如下:
Endpoint=sb://yourHost/ServiceBusDefaultNamespace;StsEndpoint=https://yourHost:9355/ServiceBusDefaultNamespace;RuntimePort=9354;ManagementPort=9355
打开应用程序配置文件或创建一个配置文件,然后粘贴连接字符串。例如:
<configuration> <appSettings> <add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://yourHost/ServiceBusDefaultNamespace;StsEndpoint=https://yourHost:9355/ServiceBusDefaultNamespace;RuntimePort=9354;ManagementPort=9355" /> </appSettings> </configuration>
请注意,如果有多个服务命名空间,你只能选择并复制 PowerShell cmdlet 生成的一个终结点。
创建 NamespaceManager 和 MessagingFactory 对象,就像对 Microsoft Azure Service Bus 一样。
MessagingFactory messagingFactory = MessagingFactory.Create(); NamespaceManager namespaceManager = NamespaceManager.Create();
连接到 Service Bus 后,你可以使用命名空间管理器实例创建消息实体(队列、主题和订阅),或创建 QueueClient、TopicClient 或 SubscriptionClient 对象以发送和接收消息。
通过显式提供连接参数连接到 Service Bus for Windows Server
除了在配置文件中使用连接字符串连接到 Service Bus 以外,在某些情况下,你需要显式指定输入参数才能连接到 Service Bus。Service Bus 引入了一个帮助器类,使你能够在代码中生成连接字符串。
在代码中,标识连接参数(通过使用配置文件或某种其他方式)。
string ServerFQDN = "localhost"; int HttpPort = 9355; int TcpPort = 9354; string ServiceNamespace = "ServiceBusDefaultNamespace";
在代码中,使用 ServiceBusConnectionStringBuilder 帮助器类连接到 Service Bus。例如:
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); MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connBuilder.ToString()); NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());
有关 Service Bus for Windows Server 的连接字符串的详细信息,请参阅Configuration Connection Strings
另请参阅
概念
其他资源
Configuration Connection Strings
Queues, Topics, and Subscriptions