public void Open()
        {
            this.serviceHost = createServiceHost();

            statusBehavior             = new ConnectionStatusBehavior();
            statusBehavior.Online     += ConnectionStatusOnline;
            statusBehavior.Offline    += ConnectionStatusOffline;
            statusBehavior.Connecting += ConnectionStatusConnecting;

            // add the Service Bus credentials to all endpoints specified in configuration
            foreach (var endpoint in serviceHost.Description.Endpoints)
            {
                endpoint.Behaviors.Add(statusBehavior);
            }

            // Register for Service Host fault notification
            serviceHost.Faulted += ServiceHostFaulted;

            // Start the service
            // if Open throws any Exceptions the faulted event will be raised
            try
            {
                serviceHost.Open();
            }
            catch (Exception e)
            {
                // This is handled by the fault handler ServiceHostFaulted
                Console.WriteLine("Encountered exception \"{0}\"", e.Message);
            }
        }
        public void Open()
        {
            this.serviceHost = createServiceHost();

            statusBehavior = new ConnectionStatusBehavior();
            statusBehavior.Online += ConnectionStatusOnline;
            statusBehavior.Offline += ConnectionStatusOffline;
            statusBehavior.Connecting += ConnectionStatusConnecting;

            // add the Service Bus credentials to all endpoints specified in configuration
            foreach (var endpoint in serviceHost.Description.Endpoints)
            {
                endpoint.Behaviors.Add(statusBehavior);
            }

            // Register for Service Host fault notification
            serviceHost.Faulted += ServiceHostFaulted;

            // Start the service
            // if Open throws any Exceptions the faulted event will be raised
            try
            {
                serviceHost.Open();
            }
            catch (Exception e)
            {
                // This is handled by the fault handler ServiceHostFaulted
                Console.WriteLine("Encountered exception \"{0}\"", e.Message);
            }
        }
Esempio n. 3
0
        public static int VerifyListen(string connectionString, string path, Binding binding, ConnectivityMode connectivityMode, string response, ServiceThrottlingBehavior throttlingBehavior)
        {
            RelayTraceSource.TraceInfo($"Open relay listener using {binding.GetType().Name}, ConnectivityMode.{connectivityMode}...");
            ServiceHost serviceHost = null;

            try
            {
                var connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString);
                var tp = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessKeyName, connectionStringBuilder.SharedAccessKey);

                string relayNamespace = connectionStringBuilder.Endpoints.First().Host;
                ServiceBusEnvironment.SystemConnectivity.Mode = connectivityMode;
                EchoService.DefaultResponse = response;
                if (!(binding is WebHttpRelayBinding))
                {
                    serviceHost = new ServiceHost(typeof(EchoService));
                }
                else
                {
                    serviceHost = new WebServiceHost(typeof(EchoService));
                }

                Type            contractType       = IsOneWay(binding) ? typeof(ITestOneway) : typeof(IEcho);
                ServiceEndpoint endpoint           = serviceHost.AddServiceEndpoint(contractType, binding, new Uri($"{binding.Scheme}://{relayNamespace}/{path}"));
                var             listenerActivityId = Guid.NewGuid();
                RelayTraceSource.TraceVerbose($"Listener ActivityId:{listenerActivityId}");
                endpoint.EndpointBehaviors.Add(new TransportClientEndpointBehavior(tp)
                {
                    ActivityId = listenerActivityId
                });
                serviceHost.Description.Behaviors.Add(throttlingBehavior);

                // Trace status changes
                var connectionStatus = new ConnectionStatusBehavior();
                connectionStatus.Connecting += (s, e) => RelayTraceSource.TraceException(connectionStatus.LastError, TraceEventType.Warning, "Relay listener Re-Connecting");
                connectionStatus.Online     += (s, e) => RelayTraceSource.Instance.TraceEvent(TraceEventType.Information, (int)ConsoleColor.Green, "Relay Listener is online");
                EventHandler offlineHandler = (s, e) => RelayTraceSource.TraceException(connectionStatus.LastError, "Relay Listener is OFFLINE");
                connectionStatus.Offline += offlineHandler;
                endpoint.EndpointBehaviors.Add(connectionStatus);
                serviceHost.Faulted += (s, e) => RelayTraceSource.TraceException(connectionStatus.LastError, "Relay listener ServiceHost Faulted");

                serviceHost.Open();
                RelayTraceSource.TraceInfo("Relay listener \"" + endpoint.Address.Uri + "\" is open");
                RelayTraceSource.TraceInfo("Press <ENTER> to close the listener ");
                Console.ReadLine();

                RelayTraceSource.TraceInfo("Closing Connection...");
                connectionStatus.Offline -= offlineHandler; // Avoid a spurious trace on expected shutdown.
                serviceHost.Close();
                RelayTraceSource.TraceInfo("Closed");
                return(0);
            }
            catch (Exception)
            {
                serviceHost?.Abort();
                throw;
            }
        }
Esempio n. 4
0
        private void StartSignallingWorker(ServiceBusSignalingListenerEndpointSettings signallingEndpointSettings)
        {
            Uri             signallingURI;
            ServiceHost     host;
            ServiceEndpoint endpoint;
            TokenProvider   tokenProvider;
            TransportClientEndpointBehavior transportClientEndpointBehavior;

            string address = string.Format("{0}://{1}.{2}/{3}",
                                           signallingEndpointSettings.Scheme,
                                           signallingEndpointSettings.Namespace,
                                           signallingEndpointSettings.Domain,
                                           signallingEndpointSettings.ServicePath
                                           );

            if (!Uri.TryCreate(address, UriKind.Absolute, out signallingURI))
            {
                throw new BootstrapException(String.Format("Could not parse provided signalling URI: {0}", address));
            }

            Binding binding = new NetTcpRelayBinding
            {
                IsDynamic = false,
                HostNameComparisonMode = HostNameComparisonMode.Exact
            };

            ConnectionStatusBehavior statusBehavior = new ConnectionStatusBehavior();

            statusBehavior.Online += delegate(object o, EventArgs e)
            {
                Console.WriteLine("[:)] Listener for is now online.");
            };

            host          = new ServiceHost(typeof(ConnectorSignalingService), signallingURI);
            tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(signallingEndpointSettings.SharedAccessKeyName, signallingEndpointSettings.SharedAccessKey);
            endpoint      = host.AddServiceEndpoint(typeof(IConnectorSignalingService), binding, address);
            transportClientEndpointBehavior = new TransportClientEndpointBehavior(tokenProvider);

            endpoint.EndpointBehaviors.Add(statusBehavior);
            endpoint.EndpointBehaviors.Add(transportClientEndpointBehavior);

            host.Open();
        }