public void DiscoveryVersions()
        {
            var versionedEndpoints = new List <Tuple <Version, Uri> >
            {
                new Tuple <Version, Uri>(
                    new Version(2, 0, 0, 0),
                    new Uri("http://localhost/v2")),
                new Tuple <Version, Uri>(
                    new Version(1, 0, 0, 0),
                    new Uri("http://localhost/v1")),
                new Tuple <Version, Uri>(
                    new Version(3, 0, 0, 0),
                    new Uri("http://localhost/v3")),
            };

            var endpoint = new BootstrapEndpoint(versionedEndpoints);
            var versions = endpoint.DiscoveryVersions();

            Assert.That(
                versions,
                Is.EquivalentTo(
                    versionedEndpoints
                    .Select(t => t.Item1)
                    .OrderBy(v => v)));
        }
Exemple #2
0
        /// <summary>
        /// Opens the channel.
        /// </summary>
        /// <param name="allowAutomaticChannelDiscovery">
        /// A flag that indicates whether or not the channel should provide automatic channel discovery.
        /// </param>
        public void OpenChannel(bool allowAutomaticChannelDiscovery)
        {
            var discoveryChannelsByVersion = new Dictionary <Version, Uri>();

            foreach (var version in DiscoveryVersions.SupportedVersions())
            {
                var host     = m_HostBuilder();
                var endpoint = m_VersionedEndpointBuilder(version, m_Template.ChannelTemplate);

                var localVersion = version;
                var type         = endpoint.Item1;
                Func <ServiceHost, ServiceEndpoint> endpointBuilder = h => m_Template.AttachVersionedDiscoveryEndpoint(h, type, localVersion);
                var uri = host.OpenChannel(endpoint.Item2, endpointBuilder);

                m_HostsByVersion.Add(version, host);
                discoveryChannelsByVersion.Add(version, uri);
            }

            // Open the channel that provides the discovery of the discovery channels
            m_BootstrapHost = m_HostBuilder();
            var bootstrapEndpoint = new BootstrapEndpoint(discoveryChannelsByVersion.Select(p => new Tuple <Version, Uri>(p.Key, p.Value)));

            Func <ServiceHost, ServiceEndpoint> bootstrapEndpointBuilder =
                h =>
            {
                var endpoint = m_Template.AttachDiscoveryEntryEndpoint(h, typeof(IBootstrapEndpoint), m_Id, allowAutomaticChannelDiscovery);
                m_EntryChannelStorage(endpoint.Address.Uri);

                return(endpoint);
            };

            m_BootstrapHost.OpenChannel(bootstrapEndpoint, bootstrapEndpointBuilder);
        }
        public void OpenChannel()
        {
            var id      = EndpointIdExtensions.CreateEndpointIdForCurrentProcess();
            var baseUri = new Uri("http://localhost/invalid");
            var versionedEndpointUri = new Uri("http://localhost/invalid/v1");
            var template             = new Mock <IDiscoveryChannelTemplate>();
            {
                template.Setup(
                    t => t.AttachDiscoveryEntryEndpoint(It.IsAny <ServiceHost>(), It.IsAny <Type>(), It.IsAny <EndpointId>(), It.IsAny <bool>()))
                .Returns(new ServiceEndpoint(new ContractDescription("a"), new NetNamedPipeBinding(), new EndpointAddress(baseUri)));
            }

            var versionedEndpointCounter = 0;
            var versionedEndpoint        = new Mock <IVersionedDiscoveryEndpoint>();
            Func <Version, ChannelTemplate, Tuple <Type, IVersionedDiscoveryEndpoint> > endpointBuilder =
                (v, t) =>
            {
                versionedEndpointCounter++;
                return(new Tuple <Type, IVersionedDiscoveryEndpoint>(versionedEndpoint.Object.GetType(), versionedEndpoint.Object));
            };

            BootstrapEndpoint baseEndpoint = null;
            var host = new Mock <IHoldServiceConnections>();
            {
                host.Setup(h => h.OpenChannel(It.IsAny <IReceiveInformationFromRemoteEndpoints>(), It.IsAny <Func <ServiceHost, ServiceEndpoint> >()))
                .Callback <IReceiveInformationFromRemoteEndpoints, Func <ServiceHost, ServiceEndpoint> >(
                    (h, e) =>
                {
                    e(null);
                    baseEndpoint = h as BootstrapEndpoint;
                })
                .Returns <IReceiveInformationFromRemoteEndpoints, Func <ServiceHost, ServiceEndpoint> >(
                    (h, e) => (h is IVersionedDiscoveryEndpoint) ? versionedEndpointUri : baseUri)
                .Verifiable();
            }

            Func <IHoldServiceConnections> hostBuilder = () => host.Object;

            Uri          entryAddress = null;
            Action <Uri> storage      = u => entryAddress = u;

            var channel = new BootstrapChannel(
                id,
                template.Object,
                endpointBuilder,
                hostBuilder,
                storage);

            channel.OpenChannel(true);

            host.Verify(
                h => h.OpenChannel(It.IsAny <IReceiveInformationFromRemoteEndpoints>(), It.IsAny <Func <ServiceHost, ServiceEndpoint> >()),
                Times.Exactly(2));
            Assert.AreEqual(1, versionedEndpointCounter);
            Assert.AreEqual(baseUri, entryAddress);
            Assert.IsNotNull(baseEndpoint);
            Assert.AreEqual(1, baseEndpoint.DiscoveryVersions().Length);
            Assert.AreEqual(versionedEndpointUri, baseEndpoint.UriForVersion(baseEndpoint.DiscoveryVersions()[0]));
        }
Exemple #4
0
        public void RecentlyConnectedEndpointWithoutSuitableDiscoveryVersion()
        {
            var translator  = new Mock <ITranslateVersionedChannelInformation>();
            var translators = new[]
            {
                new Tuple <Version, ITranslateVersionedChannelInformation>(new Version(1, 0), translator.Object),
            };

            var configuration = new Mock <IConfiguration>();
            {
                configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>()))
                .Returns(false);
            }

            var template = new NamedPipeDiscoveryChannelTemplate(configuration.Object);
            Func <ChannelTemplate, IDiscoveryChannelTemplate> templateBuilder = t => template;
            var diagnostics = new SystemDiagnostics((l, s) => { }, null);
            var discovery   = new ManualDiscoverySource(
                translators,
                templateBuilder,
                diagnostics);

            discovery.OnEndpointBecomingAvailable += (s, e) => Assert.Fail();
            discovery.StartDiscovery();

            var uri      = new Uri("net.pipe://localhost/pipe/discovery");
            var channels = new[]
            {
                new Tuple <Version, Uri>(new Version(2, 0), new Uri(uri, new Uri("/2.0", UriKind.Relative))),
            };
            var receiver = new BootstrapEndpoint(channels);

            var host    = new ServiceHost(receiver, uri);
            var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
            {
                TransferMode = TransferMode.Buffered,
            };
            var address  = string.Format("{0}_{1}", "ThroughNamedPipe", Process.GetCurrentProcess().Id);
            var endpoint = host.AddServiceEndpoint(typeof(IBootstrapEndpoint), binding, address);

            host.Open();
            try
            {
                discovery.RecentlyConnectedEndpoint(
                    EndpointIdExtensions.CreateEndpointIdForCurrentProcess(),
                    endpoint.ListenUri);
            }
            finally
            {
                host.Close();
            }
        }
        public void UriForVersionWithNullVersion()
        {
            var versionedEndpoints = new List <Tuple <Version, Uri> >
            {
                new Tuple <Version, Uri>(
                    new Version(2, 0, 0, 0),
                    new Uri("http://localhost/v2")),
                new Tuple <Version, Uri>(
                    new Version(1, 0, 0, 0),
                    new Uri("http://localhost/v1")),
                new Tuple <Version, Uri>(
                    new Version(3, 0, 0, 0),
                    new Uri("http://localhost/v3")),
            };

            var endpoint = new BootstrapEndpoint(versionedEndpoints);

            Assert.IsNull(endpoint.UriForVersion(null));
        }
        public void UriForVersion()
        {
            var versionedEndpoints = new List <Tuple <Version, Uri> >
            {
                new Tuple <Version, Uri>(
                    new Version(2, 0, 0, 0),
                    new Uri("http://localhost/v2")),
                new Tuple <Version, Uri>(
                    new Version(1, 0, 0, 0),
                    new Uri("http://localhost/v1")),
                new Tuple <Version, Uri>(
                    new Version(3, 0, 0, 0),
                    new Uri("http://localhost/v3")),
            };

            var endpoint = new BootstrapEndpoint(versionedEndpoints);
            var address  = endpoint.UriForVersion(new Version(2, 0, 0, 0));

            Assert.AreEqual(versionedEndpoints[0].Item2, address);
        }