public void CreateRelay(string relayName) { NamespaceManager ns = NamespaceManager.CreateFromConnectionString(Connections.HybridRelayConnectionString); RelayDescription rd; if (!ns.RelayExists(relayName, out rd)) { ns.CreateRelay(relayName, RelayType.NetTcp); } }
/*********************************************************************** * The code below is not specific to Role Based Access Control. * The code below can work with any Microsoft.ServiceBus.TokenProvider. ***********************************************************************/ static async Task RunWcfSampleAsync(string hostAddress, string wcfRelayName, TokenProvider tokenProvider, Binding binding) { var namespaceManager = new NamespaceManager(hostAddress, tokenProvider); ServiceHost serviceHost = null; RelayDescription relayDescription = null; bool deleted = false; UriBuilder uriBuilder = new UriBuilder(hostAddress); uriBuilder.Scheme = binding.Scheme; uriBuilder.Path = wcfRelayName; Uri relayAddress = uriBuilder.Uri; try { // Cannot create NetOneWay and NetEvent (extends NetOneway) Relays, they must be created dynamically if (!(binding is NetOnewayRelayBinding)) { Console.WriteLine("Creating the WCF Relay..."); relayDescription = new RelayDescription(wcfRelayName, GetRelayType(binding)); namespaceManager.CreateRelay(relayDescription); Console.WriteLine($"Created the WCF Relay: {wcfRelayName}"); } Console.WriteLine("Creating and opening the listener for the WCF Relay..."); ServiceEndpoint endpoint = null; if (binding is NetTcpRelayBinding || binding is BasicHttpRelayBinding || binding is WSHttpRelayBinding) { serviceHost = new ServiceHost(typeof(MathService), relayAddress); endpoint = serviceHost.AddServiceEndpoint(typeof(IMathService), binding, string.Empty); } else if (binding is WebHttpRelayBinding) { serviceHost = new WebServiceHost(typeof(WebHttpService), relayAddress); endpoint = serviceHost.AddServiceEndpoint(typeof(IWebRequestResponse), binding, string.Empty); endpoint.Behaviors.Add(new WebHttpBehavior()); } else if (binding is NetOnewayRelayBinding) { serviceHost = new ServiceHost(new NotificationService(), relayAddress); endpoint = serviceHost.AddServiceEndpoint(typeof(INotificationService), binding, string.Empty); } endpoint.EndpointBehaviors.Add(new TransportClientEndpointBehavior(tokenProvider)); DisableHttpHelpPage(serviceHost); serviceHost.Open(); Console.WriteLine($"The listener to {wcfRelayName} was sucessfully opened"); Console.WriteLine($"Creating and sending with a channel to WCF Relay..."); CreateAndSendWithChannel(relayAddress, tokenProvider, binding); Console.WriteLine($"Created and sent with channel to WCF Relay {wcfRelayName}"); if (!(binding is NetOnewayRelayBinding)) { Console.WriteLine("Getting the WCF Relay..."); await namespaceManager.GetRelayAsync(relayDescription.Path); Console.WriteLine($"Got WCF Relay {relayDescription.Path}"); Console.WriteLine("Updating the WCF Relay..."); await namespaceManager.UpdateRelayAsync(relayDescription); Console.WriteLine($"Updated the WCF Relay {relayDescription.Path}"); Console.WriteLine("Deleting the WCF Relay..."); await namespaceManager.DeleteRelayAsync(relayDescription.Path); Console.WriteLine($"Deleted the WCF Relay {relayDescription.Path}"); deleted = true; } } catch (Exception e) { Console.WriteLine($"Exception: {e}"); } finally { SafeClose(serviceHost); if (!deleted && relayDescription != null) { await namespaceManager.DeleteRelayAsync(relayDescription.Path); } } }