Exemplo n.º 1
0
        static void Main(string[] args)
        {
            IPHostEntry entry = Dns.GetHostEntry("");
            string addr = "localhost";

            for (int i = 0; i < entry.AddressList.Length; i++)
            {
                if (entry.AddressList[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    addr = entry.AddressList[i].ToString();
                    break;
                }
            }

            Uri baseAddress = new Uri("http://" + addr + ":8084/319D0A4D-2253-47DC-AC4A-C1951FF6667D");

            ServiceHost serviceHost = new ServiceHost(typeof(ServiceHelloWCF), baseAddress);
            try
            {
                ServiceEndpoint wsEndpoint = serviceHost.AddServiceEndpoint(typeof(IServiceHelloWCF), new WSHttpBinding(SecurityMode.None), string.Empty);
                EndpointDiscoveryBehavior endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();

                // Add the discovery behavior to the endpoint.
                wsEndpoint.Behaviors.Add(endpointDiscoveryBehavior);
                
                // Make the service discoverable over UDP multicast
                serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
                serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint(DiscoveryVersion.WSDiscovery11));

                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.HttpGetUrl = baseAddress;
                serviceHost.Description.Behaviors.Add(smb);

                serviceHost.Open();

                Console.WriteLine("Hello World WCF Service started at {0}", baseAddress);
                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();

                serviceHost.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
            }

            if (serviceHost.State != CommunicationState.Closed)
            {
                Console.WriteLine("Aborting service...");
                serviceHost.Abort();
            }

        }
		protected override void Opening(ServiceHost serviceHost)
		{
			var serviceDiscovery = serviceHost.Description.Behaviors.Find<ServiceDiscoveryBehavior>();
			if (serviceDiscovery == null)
			{
				serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
			}

			foreach (var endpoint in serviceHost.Description.NonSystemEndpoints())
			{
				var discovery = endpoint.Behaviors.Find<EndpointDiscoveryBehavior>();
				if (discovery == null)
				{
					discovery = new EndpointDiscoveryBehavior();
					endpoint.Behaviors.Add(discovery);
				}

				discovery.Scopes.AddAll(scopes);

				discovery.Extensions.AddAll(metadata);

				AddAdditionalMetadata(serviceHost, discovery);

				if (strict == false)
				{
					ExportMetadata(endpoint, discovery);
				}
			}

			AddDiscoveryEndpoint(serviceHost);
		}
Exemplo n.º 3
0
        /// <summary>
        /// Starts the module for the given host.
        /// </summary>
        /// <param name="host">The host.</param>
        public void Start(AgentHost host)
        {
            discoveryBehavior = host.Endpoint.Behaviors.Find<EndpointDiscoveryBehavior>();
            if (discoveryBehavior == null)
            {
                discoveryBehavior = new EndpointDiscoveryBehavior();
                host.Endpoint.Behaviors.Add(discoveryBehavior);
            }
            discoveryBehavior.Scopes.Add(scope);

            host.TestRunnerHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
            host.TestRunnerHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

            host.TestRunner.CommunicationStarted += (sender, args) =>
                                                        {
                                                            lock (this)
                                                            {
                                                                counter++;
                                                                discoveryBehavior.Enabled = false;
                                                            }
                                                        };
            host.TestRunner.CommunicationFinished += (sender, args) =>
                                                         {
                                                             lock (this)
                                                             {
                                                                 counter--;
                                                                 if (counter == 0)
                                                                     discoveryBehavior.Enabled = true;
                                                             }
                                                         };
        }
Exemplo n.º 4
0
        public static void Main()
        {
            Uri baseAddress = new Uri("http://localhost:8000/" + Guid.NewGuid().ToString());

            ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);

            try
            {
                // Add an endpoint to the service
                ServiceEndpoint discoverableCalculatorEndpoint = serviceHost.AddServiceEndpoint(
                    typeof(ICalculatorService), 
                    new WSHttpBinding(), 
                    "/DiscoverableEndpoint");

                // Add a Scope to the endpoint
                EndpointDiscoveryBehavior discoverableEndpointBehavior = new EndpointDiscoveryBehavior();
                discoverableEndpointBehavior.Scopes.Add(new Uri("ldap:///ou=engineering,o=exampleorg,c=us"));
                discoverableCalculatorEndpoint.Behaviors.Add(discoverableEndpointBehavior);

                // Add an endpoint to the service
                ServiceEndpoint nonDiscoverableCalculatorEndpoint = serviceHost.AddServiceEndpoint
                    (typeof(ICalculatorService), 
                    new WSHttpBinding(), 
                    "/NonDiscoverableEndpoint");

                // Disable discoverability of the endpoint
                EndpointDiscoveryBehavior nonDiscoverableEndpointBehavior = new EndpointDiscoveryBehavior();
                nonDiscoverableEndpointBehavior.Scopes.Add(new Uri("ldap:///ou=engineering,o=exampleorg,c=us"));
                nonDiscoverableEndpointBehavior.Enabled = false;
                nonDiscoverableCalculatorEndpoint.Behaviors.Add(nonDiscoverableEndpointBehavior);

                // Make the service discoverable over UDP multicast
                serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
                serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

                serviceHost.Open();

                Console.WriteLine("Calculator Service started at {0}", baseAddress);
                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();

                serviceHost.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
            }

            if (serviceHost.State != CommunicationState.Closed)
            {
                Console.WriteLine("Aborting the service...");
                serviceHost.Abort();
            }
        }
        static EndpointDiscoveryMetadata GetEndpointDiscoveryMetadata(ServiceEndpoint endpoint, Uri listenUri)
        {
            EndpointDiscoveryMetadata endpointDiscoveryMetadata = new EndpointDiscoveryMetadata();

            endpointDiscoveryMetadata.Address = endpoint.Address;
            endpointDiscoveryMetadata.ListenUris.Add(listenUri);

            EndpointDiscoveryBehavior endpointDiscoveryBehavior = endpoint.Behaviors.Find <EndpointDiscoveryBehavior>();

            if (endpointDiscoveryBehavior != null)
            {
                if (!endpointDiscoveryBehavior.Enabled)
                {
                    if (TD.EndpointDiscoverabilityDisabledIsEnabled())
                    {
                        TD.EndpointDiscoverabilityDisabled(endpoint.Address.ToString(), listenUri.ToString());
                    }
                    return(null);
                }

                if (TD.EndpointDiscoverabilityEnabledIsEnabled())
                {
                    TD.EndpointDiscoverabilityEnabled(endpoint.Address.ToString(), listenUri.ToString());
                }

                if (endpointDiscoveryBehavior.InternalContractTypeNames != null)
                {
                    foreach (XmlQualifiedName contractTypeName in endpointDiscoveryBehavior.InternalContractTypeNames)
                    {
                        endpointDiscoveryMetadata.ContractTypeNames.Add(contractTypeName);
                    }
                }

                if (endpointDiscoveryBehavior.InternalScopes != null)
                {
                    foreach (Uri scope in endpointDiscoveryBehavior.InternalScopes)
                    {
                        endpointDiscoveryMetadata.Scopes.Add(scope);
                    }
                }
                if (endpointDiscoveryBehavior.InternalExtensions != null)
                {
                    foreach (XElement xElement in endpointDiscoveryBehavior.InternalExtensions)
                    {
                        endpointDiscoveryMetadata.Extensions.Add(xElement);
                    }
                }
            }

            XmlQualifiedName defaultContractTypeName = new XmlQualifiedName(endpoint.Contract.Name, endpoint.Contract.Namespace);

            if (!endpointDiscoveryMetadata.ContractTypeNames.Contains(defaultContractTypeName))
            {
                endpointDiscoveryMetadata.ContractTypeNames.Add(defaultContractTypeName);
            }

            return(endpointDiscoveryMetadata);
        }
Exemplo n.º 6
0
        public static void Main()
        {
            Uri baseAddress = new Uri("net.tcp://localhost:8000/" + Guid.NewGuid().ToString());

            ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);
            try
            {
                ServiceEndpoint netTcpEndpoint = serviceHost.AddServiceEndpoint(typeof(ICalculatorService), new NetTcpBinding(), string.Empty);
                EndpointDiscoveryBehavior endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();

                // Create XML metadata to add to the service endpoint
                XElement endpointMetadata = new XElement(
                    "Root",
                    new XElement("Information", "This endpoint is for a service of type: ICalculatorService."),
                    new XElement("Time", System.DateTime.Now.ToString("MM/dd/yyyy HH:mm")));

                // Add the XML metadata to the endpoint discovery behavior.
                endpointDiscoveryBehavior.Extensions.Add(endpointMetadata);

                // Add the discovery behavior to the endpoint.
                netTcpEndpoint.Behaviors.Add(endpointDiscoveryBehavior);

                // Make the service discoverable over UDP multicast
                serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
                serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

                serviceHost.Open();

                Console.WriteLine("Calculator Service started at {0}", baseAddress);
                Console.WriteLine();
                Console.WriteLine("Added the following metadata to the endpoint: \n{0}", endpointMetadata);
                Console.WriteLine("Press <ENTER> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();

                serviceHost.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
            }

            if (serviceHost.State != CommunicationState.Closed)
            {
                Console.WriteLine("Aborting service...");
                serviceHost.Abort();
            }
        }
Exemplo n.º 7
0
        public static void Main()
        {
            Uri baseAddress = new Uri("net.tcp://localhost:8000/CalculatorSvc/");
            Uri redmondScope = new Uri("net.tcp://Microsoft.Samples.Discovery/RedmondLocation");

            ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);

            try
            {                
                ServiceEndpoint tcpEndpoint = serviceHost.AddServiceEndpoint(typeof(ICalculatorService), new NetTcpBinding(), "TCPEndpoint");

                // Add a scope
                EndpointDiscoveryBehavior tcpEndpointBehavior = new EndpointDiscoveryBehavior();
                tcpEndpointBehavior.Scopes.Add(redmondScope);
                tcpEndpoint.Behaviors.Add(tcpEndpointBehavior);                

                // Make the service discoverable
                serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());

                // Listen for discovery messages over UDP multicast
                serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

                // Hook up a custom discovery service implementation through extensions
                serviceHost.Extensions.Add(new CustomDiscoveryExtension());

                serviceHost.Open();

                Console.WriteLine("Calculator Service started at: \n {0}", baseAddress);
                Console.WriteLine("Endpoint is decorated with the following scope: \n {0}", redmondScope);
                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();

                serviceHost.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
            }   

            if (serviceHost.State != CommunicationState.Closed)
            {
                Console.WriteLine("Aborting the service...");
                serviceHost.Abort();
            }
        }
		public void Use2 ()
		{
			// This time with ServiceDiscoveryBehavior.
			var b = new EndpointDiscoveryBehavior ();
			IEndpointBehavior eb = b;
			var host = new ServiceHost (typeof (TestService));
			var se = host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), new Uri ("http://localhost:37564"));
			var sdb = new ServiceDiscoveryBehavior ();
			sdb.AnnouncementEndpoints.Add (new UdpAnnouncementEndpoint ());
			IServiceBehavior sb = sdb;
			se.Behaviors.Add (b);

			var bc = new BindingParameterCollection ();
			sb.AddBindingParameters (host.Description, host, host.Description.Endpoints, bc);
			eb.AddBindingParameters (se, bc);
			Assert.AreEqual (0, bc.Count, "#1");
			Assert.AreEqual (0, host.Extensions.Count, "#1-2");

			sb.Validate (host.Description, host);
			eb.Validate (se);
			// ... should "validate" not "apply dispatch behavior" do "add host extension" job? I doubt that.
			Assert.AreEqual (1, host.Extensions.Count, "#2-2");
			var dse = host.Extensions.Find<DiscoveryServiceExtension> ();

			Assert.IsNotNull (dse, "#2-3");
			Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#2-4");
			Assert.AreEqual (2, se.Behaviors.Count, "#2-5"); // EndpointDiscoveryBehavior + discovery initializer.

			Assert.AreEqual (0, host.ChannelDispatchers.Count, "#3-1");
			Assert.AreEqual (1, host.Description.Endpoints.Count, "#3-2");
			Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-4");

			// The IEndpointBehavior from EndpointDiscoveryBehavior, when ApplyDispatchBehavior() is invoked, publishes an endpoint.
			sb.ApplyDispatchBehavior (host.Description, host);
			Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-5"); // not yet published
			eb.ApplyDispatchBehavior (se, new EndpointDispatcher (new EndpointAddress ("http://localhost:37564"), "ITestService", "http://tempuri.org/"));
			Assert.AreEqual (2, host.ChannelDispatchers.Count, "#3-6-1"); // for online and offline announcements
			Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-6-2"); // still not published.

			host.Open ();
			try {
				Assert.AreEqual (3, host.ChannelDispatchers.Count, "#4-1"); // for online and offline announcements
				Assert.AreEqual (1, dse.PublishedEndpoints.Count, "#4-2"); // The endpoint is published again. (Not sure if it's worthy of testing.)
			} finally {
				host.Close ();
			}
		}
		public void Use ()
		{
			// Without ServiceDiscoveryBehavior.
			var b = new EndpointDiscoveryBehavior ();
			IEndpointBehavior eb = b;
			var host = new ServiceHost (typeof (TestService));
			var se = host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), new Uri ("http://localhost:37564"));
			se.Behaviors.Add (b);

			var bc = new BindingParameterCollection ();
			eb.AddBindingParameters (se, bc);
			Assert.AreEqual (0, bc.Count, "#1");
			Assert.AreEqual (0, host.Extensions.Count, "#1-2");

			eb.Validate (se);
			// This behavior itself does not populate discovery extension. It just configures the extension.
			Assert.AreEqual (0, host.Extensions.Count, "#2-2");
		}
Exemplo n.º 10
0
        public static void Main()
        {
            Uri baseAddress = new Uri("net.tcp://localhost:5555/CalculatorService/" + Guid.NewGuid().ToString());

            ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);
            try
            {                
                ServiceEndpoint netTcpEndpoint = serviceHost.AddServiceEndpoint(typeof(ICalculatorService), new NetTcpBinding(), string.Empty);

                // Add a scope to the endpoint
                EndpointDiscoveryBehavior netTcpEndpointBehavior = new EndpointDiscoveryBehavior();
                netTcpEndpointBehavior.Scopes.Add(new Uri("ldap:///ou=engineering,o=exampleorg,c=us"));
                netTcpEndpoint.Behaviors.Add(netTcpEndpointBehavior);

                // Service discovery is enabled through configuration

                serviceHost.Open();

                Console.WriteLine("Calculator Service started at: \n {0}", baseAddress);
                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();

                serviceHost.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
            }   

            if (serviceHost.State != CommunicationState.Closed)
            {
                Console.WriteLine("Aborting the service...");
                serviceHost.Abort();
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Start new broadcast service
        /// </summary>
        /// <param name="type">Type of discovery</param>
        /// <param name="nameToBroadcast">The name of the service that needs to be broadcasted</param>
        /// <param name="physicalLocation">The physical location of the service that needs to be broadcasted</param>
        /// <param name="code">code to be broadcasted (e.g. device id)</param>
        /// <param name="addressToBroadcast">The address of the service that needs to be broadcasted</param>
        public void Start( DiscoveryType type, string nameToBroadcast, string physicalLocation, string code, Uri addressToBroadcast)
        {
            DiscoveryType = type;

            switch ( DiscoveryType )
            {
                case DiscoveryType.WsDiscovery:
                {
                    Ip = Net.GetIp( IpType.All );
                    Port = 7985;
                    Address = "http://" + Ip + ":" + Port + "/";

                    _discoveryHost = new ServiceHost( new DiscoveyService() );

                    var serviceEndpoint = _discoveryHost.AddServiceEndpoint( typeof( IDiscovery ), new WebHttpBinding(),
                                                                             Net.GetUrl( Ip, Port, "" ) );
                    serviceEndpoint.Behaviors.Add( new WebHttpBehavior() );

                    var broadcaster = new EndpointDiscoveryBehavior();

                    broadcaster.Extensions.Add( nameToBroadcast.ToXElement<string>() );
                    broadcaster.Extensions.Add( physicalLocation.ToXElement<string>() );
                    broadcaster.Extensions.Add( addressToBroadcast.ToString().ToXElement<string>() );
                    broadcaster.Extensions.Add( code.ToXElement<string>() );

                    serviceEndpoint.Behaviors.Add( broadcaster );
                    _discoveryHost.Description.Behaviors.Add( new ServiceDiscoveryBehavior() );
                    _discoveryHost.Description.Endpoints.Add( new UdpDiscoveryEndpoint() );
                    _discoveryHost.Open();

                    IsRunning = true;
                    Debug.WriteLine( DiscoveryType.ToString() + " is started" );
                }
                    break;
                case DiscoveryType.Zeroconf:
                {
                    _service = new RegisterService { Name = nameToBroadcast, RegType = "_am._tcp", ReplyDomain = "local", Port = 3689 };

                    // TxtRecords are optional
                    var txtRecord = new TxtRecord
                    {
                        { "name", nameToBroadcast },
                        { "addr", addressToBroadcast.ToString() },
                        { "loc", physicalLocation },
                        { "code", code }
                    };
                    _service.TxtRecord = txtRecord;
                    _service.Response += service_Response;
                    _service.Register();
                    Debug.WriteLine(DiscoveryType.ToString() + " is started");
                }
                    break;
            }
        }
Exemplo n.º 12
0
 public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
 {
     var disServiceEndpoints = new List<PluginContext.DiscoverableServiceEndpoint>();
     // apply the service discovery behavior opertaion
     _serviceDiscoveryBehavior.ApplyDispatchBehavior(serviceDescription, serviceHostBase);
     // add the additional information into the endpoint metadata extension through the endpoint discovery binding for each endpoints
     foreach (var endpoint in serviceDescription.Endpoints)
     {
         var endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();
         // check if the contract or endpoint should be ignored
         // set the extensions
         endpoint.SetExtensions(endpointDiscoveryBehavior, behavior => behavior.Extensions, _ignoredContractTypeNames, _ignoredEndpoints);
         endpoint.Behaviors.Add(endpointDiscoveryBehavior);
         // save the plugin context
         disServiceEndpoints.Add(new PluginContext.DiscoverableServiceEndpoint(endpoint, endpointDiscoveryBehavior.Extensions));
     }
     // invoke the plugins
     var context = new PluginContext(disServiceEndpoints, _announcementEndpoint);
     foreach (var plugin in _plugins)
     {
         plugin.Apply(context);
     }
 }
Exemplo n.º 13
0
        public void Open(string announcementConnectionUri, int announcementTimerSec, bool enableAnnouncement)
        {
            try
            {
                
                ServiceDiscoveryBehavior serviceDiscoveryBehavior = new ServiceDiscoveryBehavior();

                EndpointDiscoveryBehavior endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();

                XElement endpointMetadata = new XElement("Root", new XElement(_metaDataElement, _metaDataElementValue));
                XElement aliasMetadata = new XElement("Root", new XElement(_metaDataAlias, _metaDataAliasValue));
                XElement LoginMetadata = new XElement("Root", new XElement(_metaDataLogIn, _metaDataLogInValue));

                endpointDiscoveryBehavior.Extensions.Add(endpointMetadata);
                endpointDiscoveryBehavior.Extensions.Add(aliasMetadata);
                endpointDiscoveryBehavior.Extensions.Add(LoginMetadata);

                _netTcpEndpoint.Behaviors.Add(endpointDiscoveryBehavior);

                _serviceHost.Description.Behaviors.Add(serviceDiscoveryBehavior);
                _serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

                _serviceHost.Open();

                if (enableAnnouncement)
                {
                    Uri announcementEndpointUri = new Uri(announcementConnectionUri);
                    EndpointAddress announcementEndpointAddress = new EndpointAddress(announcementEndpointUri);
                    NetTcpBinding binding = new NetTcpBinding();
                    binding.Security.Mode = SecurityMode.None;
                    _announcementEndpoint = new AnnouncementEndpoint(binding, announcementEndpointAddress);

                    _announcementClient = new AnnouncementClient(_announcementEndpoint);
                    _endpointDiscoveryMetadata = EndpointDiscoveryMetadata.FromServiceEndpoint(_netTcpEndpoint);

                    //Start a timer that send announcement message
                    _announcementTimer = new Timer(announcementTimerSec * 1000);
                    _announcementTimer.AutoReset = true;
                    _announcementTimer.Elapsed += new ElapsedEventHandler(_announcementTimer_Elapsed);
                    _announcementTimer.Start();

                    _announcementClient.Open();
                }

                IsRunning = true;
               
            }
            catch (EndpointNotFoundException ex)
            {
                //this error occurs when announcement endpoint is not on the network
            }
            catch (CommunicationException ex)
            {
                _serviceHost.Abort();
                // customize this exception to be more specific
                throw;
            }
        }
		private static void ExportMetadata(ServiceEndpoint endpoint, EndpointDiscoveryBehavior discovery)
		{
			var exporter = new WsdlExporter();
			exporter.ExportEndpoint(endpoint);
			var metadata = exporter.GetGeneratedMetadata();

			var document = new XDocument();
			using (var xmlWriter = document.CreateWriter())
			{
				xmlWriter.WriteStartElement(WcfConstants.EndpointMetadata.LocalName, 
											WcfConstants.EndpointMetadata.Namespace.NamespaceName);
				metadata.WriteTo(xmlWriter);
				xmlWriter.WriteEndElement();
				xmlWriter.Flush();
			}

			discovery.Extensions.Add(document.Element(WcfConstants.EndpointMetadata));
		}
		private static void AddAdditionalMetadata(ServiceHost serviceHost, EndpointDiscoveryBehavior discovery)
		{
			var meatadata = serviceHost.Extensions.FindAll<IWcfMetadataProvider>();

			discovery.Scopes.AddAll(meatadata.SelectMany(meta => meta.Scopes));

			discovery.Extensions.AddAll(meatadata.SelectMany(meta => meta.Extensions));
		}
Exemplo n.º 16
0
        private void OpenServices()
        {
            // Create a singleton instance for the host
            ChatService chatService = new ChatService(this);
            chatServiceHost = new ServiceHost(chatService, this.localAddress);

            // Create a discovery behavior
            EndpointDiscoveryBehavior endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();

            // Add an extension element with the username
            endpointDiscoveryBehavior.Extensions.Add(
                new XElement(
                    "root",
                    new XElement("Name", this.userName)));

            // Find the endpoint
            ServiceEndpoint simpleEndpoint =
                this.chatServiceHost.Description.Endpoints.Find(typeof(ISimpleChatService));

            // Add our behavior to the endpoint before opening it
            simpleEndpoint.Behaviors.Add(endpointDiscoveryBehavior);

            ShowStatus("Opening chat service...");
            chatServiceHost.BeginOpen(
                (result) =>
                {
                    chatServiceHost.EndOpen(result);
                    ShowStatus("Chat service ready");
                },
                null);
            OpenAnnouncementService();
        }
Exemplo n.º 17
0
        static void Main(string[] args)
        {
            IPHostEntry entry = Dns.GetHostEntry("");
            string addr = "localhost";

            for (int i = 0; i < entry.AddressList.Length; i++)
            {
                if (entry.AddressList[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    addr = entry.AddressList[i].ToString();
                    break;
                }
            }

            Uri baseAddress = new Uri("http://" + addr + ":8084/319D0A4D-2253-47DC-AC4A-C1951FF6667D");

            ServiceHost serviceHost = new ServiceHost(typeof(ServiceHelloWCF), baseAddress);
            try
            {
                Binding binding;
                DiscoveryVersion ver;

                ver = DiscoveryVersion.WSDiscovery11;
                binding = new WSHttpBinding(SecurityMode.None);

                // To enable WSDiscoveryApril2005 and Soap12WSAddressingAugust2004
                //ver = DiscoveryVersion.WSDiscoveryApril2005;
                //binding = new CustomBinding(new TextMessageEncodingBindingElement(MessageVersion.Soap12WSAddressingAugust2004, Encoding.UTF8), new HttpTransportBindingElement());
   
                ServiceEndpoint wsEndpoint = serviceHost.AddServiceEndpoint(typeof(IServiceHelloWCF), binding, "");
                EndpointDiscoveryBehavior endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();

                // Add the discovery behavior to the endpoint.
                wsEndpoint.Behaviors.Add(endpointDiscoveryBehavior);
                
                // Make the service discoverable over UDP multicast
                ServiceDiscoveryBehavior serviceDiscoveryBehavior = new ServiceDiscoveryBehavior();
                // Announce the availability of the service over UDP multicast
                serviceDiscoveryBehavior.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint(ver));
                // Make the service discoverable over UDP multicast.
                serviceHost.Description.Behaviors.Add(serviceDiscoveryBehavior);
                serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint(ver));
                // Announce the availability of the service over UDP multicast


                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.HttpGetUrl = baseAddress;
                serviceHost.Description.Behaviors.Add(smb);

                serviceHost.Open();

                Console.WriteLine("Hello World WCF Service started at {0}", baseAddress);
                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();

                serviceHost.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
            }

            if (serviceHost.State != CommunicationState.Closed)
            {
                Console.WriteLine("Aborting service...");
                serviceHost.Abort();
            }

        }
Exemplo n.º 18
0
		protected override void Opening(ServiceHost serviceHost)
		{
			var serviceDiscovery = serviceHost.Description.Behaviors.Find<ServiceDiscoveryBehavior>();
			if (serviceDiscovery == null)
			{
				serviceDiscovery = new ServiceDiscoveryBehavior();
				serviceHost.Description.Behaviors.Add(serviceDiscovery);
			}

			if (announceEndpoints != null)
			{
				serviceDiscovery.AnnouncementEndpoints.AddAll(announceEndpoints);
			}

			var serviceMetadata = serviceHost.Extensions.OfType<IWcfDiscoveryMetadata>().ToArray();

			foreach (var endpoint in serviceHost.Description.NonSystemEndpoints())
			{
				var discovery = endpoint.Behaviors.Find<EndpointDiscoveryBehavior>();
				if (discovery == null)
				{
					discovery = new EndpointDiscoveryBehavior();
					endpoint.Behaviors.Add(discovery);
				}

				discovery.Scopes.AddAll(scopes);
				discovery.Extensions.AddAll(metadata);
				var discoveryMetaadata = endpoint.Behaviors.OfType<IWcfDiscoveryMetadata>();
				AddDiscoveryMetadata(discoveryMetaadata, discovery);
				AddDiscoveryMetadata(serviceMetadata, discovery);

				if (strict == false)
				{
					ExportMetadata(endpoint, discovery);
				}
			}

			AddDiscoveryEndpoint(serviceHost);
		}
Exemplo n.º 19
0
		private static void AddDiscoveryMetadata(IEnumerable<IWcfDiscoveryMetadata> metadata, EndpointDiscoveryBehavior discovery)
		{
			foreach (var meta in metadata)
			{
				discovery.Scopes.AddAll(meta.Scopes);
				discovery.Extensions.AddAll(meta.Extensions);
			}
		}
Exemplo n.º 20
0
        void EnableServiceBusDiscovery(Uri scope,bool enableMEX,Uri[] baseAddresses)
        {
            Debug.Assert(baseAddresses.Any(address=>address.Scheme == "sb"));

             if(Description.Endpoints.Count == 0)
             {
            this.AddServiceBusDefaultEndpoints(baseAddresses);
             }

             AddServiceEndpoint(new UdpDiscoveryEndpoint());

             ServiceDiscoveryBehavior discovery = new ServiceDiscoveryBehavior();
             discovery.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint());
             Description.Behaviors.Add(discovery);

             if(enableMEX == true)
             {
            Description.Behaviors.Add(new ServiceMetadataBehavior());

            foreach(Uri baseAddress in baseAddresses)
            {
               if(baseAddress.Scheme == "sb")
               {
                  string address = baseAddress.AbsoluteUri;

                  if(address.EndsWith("/") == false)
                  {
                     address += "/";
                  }
                  address += "MEX";
                  AddServiceEndpoint(typeof(IMetadataExchange),new NetTcpRelayBinding(),address);
                  break;
               }
            }
            if(scope != null)
            {
               EndpointDiscoveryBehavior behavior = new EndpointDiscoveryBehavior();
               behavior.Scopes.Add(scope);

               foreach(ServiceEndpoint endpoint in Description.Endpoints)
               {
                  if(endpoint.IsSystemEndpoint        ||
                     endpoint is DiscoveryEndpoint    ||
                     endpoint is AnnouncementEndpoint ||
                     endpoint is ServiceMetadataEndpoint)
                  {
                     continue;
                  }
                  endpoint.Behaviors.Add(behavior);
               }
            }
             }
        }