我必须建立一个Azure服务,它必须可以访问其他平台(安卓,iOS)。这就是为什么我尝试使用http或https协议来设置它,而不是使用sb (服务总线)协议(ref:服务总线绑定,最后一段)。
不幸的是,服务在初始化时抛出异常:
"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“时,上述代码工作正常。
发布于 2015-02-03 09:23:25
经过几个小时的斗争,我终于使它与https协议一起工作。在服务主机创建线中进行了关键的更改:
_host = new ServiceHost(typeof(WorkerRoleService));而不是:
_host = new ServiceHost(typeof(WorkerRoleService), uri);我还将安全令牌从SAS更改为ACS。它需要使用Azure CLI重新创建我的服务总线,因为Azure门户不允许对先前创建的服务总线启用ACS。有关更多细节,请参见以下文章:如何通过powershell创建windows服务ACS? (请阅读所有评论,因为订阅选择的正确命令是subscription )。
我的最后代码是:
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缩放(几个附加到服务总线的工作人员角色实例)。
以上代码不适用于http协议。我把它放在这里是因为它在Azure仿真器上工作(将服务总线切换到本地服务总线for Windows)。
希望上面这些能帮上忙..。
https://stackoverflow.com/questions/28232854
复制相似问题