[英]Azure Service Bus Relay and http protocol
我必須設置一個Azure服務,該服務必須可用於其他平台(Android,iOS)。 這就是為什么我嘗試使用http或https協議而不是sb(服務總線)協議(參考: 服務總線綁定 ,最后一段)來設置它的原因。
不幸的是,服務在初始化時引發異常:
"HTTP could not register URL http://+:80/ServiceBusDefaultNamespace/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details)."
WorkerRole中的服務初始化代碼為:
private void InitailizeService()
{
Trace.WriteLine("Initializing service");
try
{
var serviceAddress = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ServiceAddress");
var protocol = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.Protocol");
string keyName = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ServiceKeyName");
string sharedAccessKey = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ServiceSharedAccessKey");
Uri uri = new Uri(protocol + "://" + serviceAddress + "/ServiceBusDefaultNamespace");
ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http;
_host = new ServiceHost(typeof(WorkerRoleService), uri);
TokenProvider tp = null;
if (!String.IsNullOrEmpty(keyName))
{
tp = TokenProvider.CreateSharedAccessSignatureTokenProvider(keyName, sharedAccessKey);
}
var sharedSecretServiceBusCredential = new TransportClientEndpointBehavior(tp);
ContractDescription contractDescription = ContractDescription.GetContract(typeof(IInstalSoftCloudService), typeof(WorkerRoleService));
ServiceEndpoint serviceEndPoint = new ServiceEndpoint(contractDescription);
serviceEndPoint.Address = new EndpointAddress(uri);
Binding binding;
switch (protocol)
{
case "sb":
binding = new NetTcpRelayBinding { TransferMode = TransferMode.Streamed, MaxReceivedMessageSize = 1048576000, MaxBufferSize = 10485760, MaxConnections = 200 };
break;
case "http":
case "https":
binding = new WebHttpRelayBinding { TransferMode = TransferMode.Streamed, MaxReceivedMessageSize = 1048576000, MaxBufferSize = 10485760 };
break;
default:
throw new NotSupportedException("Protocol not supported: " + protocol);
}
serviceEndPoint.Binding = binding;
serviceEndPoint.Behaviors.Add(sharedSecretServiceBusCredential);
_host.Description.Endpoints.Add(serviceEndPoint);
_host.Open();
Trace.WriteLine("Service initialization completed");
}
catch (Exception e)
{
Trace.WriteLine("Service initialization failed.\r\n" + e.Message);
throw;
}
}
ServiceConfiguration.Cloud.cscfg中的設置為:
<Setting name="Microsoft.ServiceBus.ServiceAddress" value="<my namespace here>.servicebus.windows.net" />
<Setting name="Microsoft.ServiceBus.ServiceKeyName" value="RootManageSharedAccessKey" />
<Setting name="Microsoft.ServiceBus.ServiceSharedAccessKey" value="<my key here>" />
<Setting name="Microsoft.ServiceBus.Protocol" value="http" />
將設置中的協議更改為“ sb”時,以上代碼可以正常工作。
經過數小時的努力,我最終使其與https協議一起使用。 關鍵更改是在服務主機創建行中進行的:
_host = new ServiceHost(typeof(WorkerRoleService));
代替:
_host = new ServiceHost(typeof(WorkerRoleService), uri);
我還將安全令牌從SAS更改為ACS。 盡管需要使用Azure CLI重新創建我的服務總線,但這是必需的,因為Azure門戶不允許將ACS啟用到先前創建的服務總線。 有關更多詳細信息,請參見以下文章: 如何通過Powershell創建Windows Service ACS? (閱讀所有注釋,因為正確的訂閱選擇命令是Select-AzureSubscription)。
我的最終代碼是:
private void InitailizeService()
{
try
{
var serviceAddress = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ServiceAddress");
var serviceNamespace = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ServiceNamespace");
var protocol = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.Protocol");
string issuerName = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ServiceIssuerName");
string issuerSecret = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ServiceIssuerSecret");
Uri uri = ServiceBusEnvironment.CreateServiceUri(protocol, serviceAddress, serviceNamespace);
ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http;
_host = new ServiceHost(typeof(WorkerRoleService));
TokenProvider tp = null;
if (!String.IsNullOrEmpty(issuerName))
{
tp = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret);
}
var sharedSecretServiceBusCredential = new TransportClientEndpointBehavior(tp);
Binding binding;
switch (protocol)
{
case "sb":
binding = new NetTcpRelayBinding { TransferMode = TransferMode.Streamed, MaxReceivedMessageSize = 1048576000, MaxBufferSize = 10485760, MaxConnections = 200 };
break;
case "http":
binding = new BasicHttpBinding { TransferMode = TransferMode.Streamed, MaxReceivedMessageSize = 1048576000, MaxBufferSize = 10485760 };
break;
case "https":
var wsbinding = new WS2007HttpRelayBinding { MaxReceivedMessageSize = 1048576000 };
wsbinding.Security.Mode = EndToEndSecurityMode.Transport;
wsbinding.Security.RelayClientAuthenticationType = RelayClientAuthenticationType.None;
binding = wsbinding;
break;
default:
throw new NotSupportedException("Protocol not supported: " + protocol);
}
var serviceEndPoint = _host.AddServiceEndpoint(typeof(IInstalSoftCloudService), binding, uri);
serviceEndPoint.Behaviors.Add(sharedSecretServiceBusCredential);
// Lines below are for MEX and publishing of the service
EnableMetadataExchange(uri, sharedSecretServiceBusCredential, binding);
ServiceRegistrySettings serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public) { DisplayName = "InstalSystemMobileEngine" };
foreach (ServiceEndpoint subscriberEndpoint in _host.Description.Endpoints)
{
subscriberEndpoint.Behaviors.Add(serviceRegistrySettings);
}
_host.Open();
Trace.WriteLine("Service initialization completed");
}
catch (Exception e)
{
Trace.WriteLine("Service initialization failed.\r\n" + e.Message);
throw;
}
}
private void EnableMetadataExchange(Uri aBaseUri, TransportClientEndpointBehavior aBehavior, Binding aBinding, bool aEnableHttpGet = true)
{
if (_host.State == CommunicationState.Opened)
throw new InvalidOperationException("Host already opened");
var metadataBehavior = _host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
_host.Description.Behaviors.Add(metadataBehavior);
Trace.WriteLine("_host.Description.Behaviors.Add(metadataBehavior)");
}
var mexEndpoint = _host.AddServiceEndpoint(typeof(IMetadataExchange), aBinding, new Uri(aBaseUri, "mex"));
mexEndpoint.Behaviors.Add(aBehavior);
}
以上代碼的配置:
<Setting name="Microsoft.ServiceBus.ServiceAddress" value="<service bus address - without .servicebus.windows.net>" />
<Setting name="Microsoft.ServiceBus.ServiceNamespace" value="ServiceBusDefaultNamespace/" />
<Setting name="Microsoft.ServiceBus.ServiceIssuerName" value="<issuer name>" />
<Setting name="Microsoft.ServiceBus.ServiceIssuerSecret" value="<issuer secret>" />
<Setting name="Microsoft.ServiceBus.Protocol" value="https" />
現在,我們將不得不嘗試從Android應用程序連接到該服務-我希望一切正常。 來自測試WCF應用程序的連接可以正常工作,並且非常重要:允許Azure擴展(連接到Service Bus的多個輔助角色實例)。
上面的代碼不適用於http協議。 我把它留在這里是因為它可以在Azure模擬器上運行(將服務總線切換到Windows的本地服務總線)。
希望以上內容能幫助某人...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.