コード例 #1
0
        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 UseHttpBinding()
        {
            var    ahost     = new ServiceHost(typeof(AnnouncementService));
            var    aendpoint = new AnnouncementEndpoint(new CustomBinding(new HttpTransportBindingElement()), new EndpointAddress("http://localhost:4989"));
            var    ib        = new InspectionBehavior();
            object state     = new object();

            ib.RequestReceived += delegate
            {
                InspectionBehavior.State = state;
                return(null);
            };
            aendpoint.Behaviors.Add(ib);
            ahost.AddServiceEndpoint(aendpoint);
            ahost.Open();
            try
            {
                Assert.IsTrue(ib.DispatchBehaviorApplied, "#1");
                var b = new ServiceDiscoveryBehavior();
                b.AnnouncementEndpoints.Add(new AnnouncementEndpoint(new CustomBinding(new HttpTransportBindingElement()), new EndpointAddress("http://localhost:4989")));
                IServiceBehavior sb = b;
                var host            = new ServiceHost(typeof(TestService));
                var se = host.AddServiceEndpoint(typeof(ITestService), new BasicHttpBinding(), new Uri("http://localhost:37564"));

                var bc = new BindingParameterCollection();
                sb.AddBindingParameters(host.Description, host, host.Description.Endpoints, bc);
                sb.Validate(host.Description, host);
                // ... should "validate" not "apply dispatch behavior" do "add host extension" job? I doubt that.
                var dse = host.Extensions.Find <DiscoveryServiceExtension> ();
                Assert.IsNotNull(dse, "#2");
                sb.ApplyDispatchBehavior(host.Description, host);

                // The IEndpointBehavior from ServiceDiscoveryBehavior, when ApplyDispatchBehavior() is invoked, publishes an endpoint.
                se.Behaviors [0].ApplyDispatchBehavior(se, new EndpointDispatcher(new EndpointAddress("http://localhost:37564"), "ITestService", "http://tempuri.org/"));

                host.Open();
                try
                {
                    Assert.AreEqual(state, InspectionBehavior.State, "#3");
                }
                finally
                {
                    host.Close();
                }
            }
            finally
            {
                ahost.Close();
            }
        }
コード例 #3
0
        public void Use()
        {
            var b = new ServiceDiscoveryBehavior();

            b.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint());
            IServiceBehavior sb = b;
            var host            = new ServiceHost(new Uri("http://localhost:37564"));

            var bc = new BindingParameterCollection();

            sb.AddBindingParameters(host.Description, host, host.Description.Endpoints, bc);
            Assert.AreEqual(0, bc.Count, "#1");

            Assert.AreEqual(0, host.Extensions.Count, "#2-1");
            sb.Validate(host.Description, host);
            // ... 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(0, host.ChannelDispatchers.Count, "#3-1");
            sb.ApplyDispatchBehavior(host.Description, host);
            Assert.AreEqual(0, host.Description.Endpoints.Count, "#3-2");
            Assert.AreEqual(2, host.ChannelDispatchers.Count, "#3-3");             // for online and offline announcements
            Assert.AreEqual(0, dse.PublishedEndpoints.Count, "#3-4");              // discovery endpoints are not "published"
            int idx = 0;

            foreach (var cdisb in host.ChannelDispatchers)
            {
                var    cdis = cdisb as ChannelDispatcher;
                string head = "#4." + idx + ".";
                Assert.IsNull(cdis, head + "dispatcher");
                if (cdisb.Listener != null)
                {
                    Assert.AreEqual("urn:schemas-microsoft-org:ws:2008:07:discovery", cdisb.Listener.Uri.ToString(), head + "uri");
                }
                // else ... WHOA! .NET "OnlineAnnouncementChannelDispatcher" type does not seem to provide the listener.
                idx++;
            }
        }
コード例 #4
0
        internal static void InitializeServiceHost(ServiceHostBase serviceHost)
        {
            var description = serviceHost.Description;

            if (serviceHost.ImplementedContracts != null && serviceHost.ImplementedContracts.Count > 0)
            {
                EnsureThereAreApplicationEndpoints(description);
            }

            ValidateDescription(serviceHost);

            var stuffPerListenUriInfo           = new Dictionary <ListenUriInfo, StuffPerListenUriInfo>();
            var endpointInfosPerEndpointAddress = new Dictionary <EndpointAddress, Collection <EndpointInfo> >();

            // Ensure ListenUri and group endpoints per ListenUri
            for (int i = 0; i < description.Endpoints.Count; i++)
            {
                ServiceEndpoint endpoint = description.Endpoints[i];

                ListenUriInfo listenUriInfo = GetListenUriInfoForEndpoint(serviceHost, endpoint);
                if (!stuffPerListenUriInfo.ContainsKey(listenUriInfo))
                {
                    stuffPerListenUriInfo.Add(listenUriInfo, new StuffPerListenUriInfo());
                }
                stuffPerListenUriInfo[listenUriInfo].Endpoints.Add(endpoint);
            }

            foreach (KeyValuePair <ListenUriInfo, StuffPerListenUriInfo> stuff in stuffPerListenUriInfo)
            {
                Uri           listenUri               = stuff.Key.ListenUri;
                ListenUriMode listenUriMode           = stuff.Key.ListenUriMode;
                BindingParameterCollection parameters = stuff.Value.Parameters;
                Binding          binding              = stuff.Value.Endpoints[0].Binding;
                EndpointIdentity identity             = stuff.Value.Endpoints[0].Address.Identity;
                // same EndpointAddressTable instance must be shared between channelDispatcher and parameters
                //ThreadSafeMessageFilterTable<EndpointAddress> endpointAddressTable = new ThreadSafeMessageFilterTable<EndpointAddress>();
                //parameters.Add(endpointAddressTable);

                // add service-level binding parameters
                foreach (IServiceBehavior behavior in description.Behaviors)
                {
                    behavior.AddBindingParameters(description, serviceHost, stuff.Value.Endpoints, parameters);
                }
                for (int i = 0; i < stuff.Value.Endpoints.Count; i++)
                {
                    ServiceEndpoint endpoint  = stuff.Value.Endpoints[i];
                    string          viaString = listenUri.AbsoluteUri;

                    // ensure all endpoints with this ListenUriInfo have same binding
                    if (endpoint.Binding != binding)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ABindingInstanceHasAlreadyBeenAssociatedTo1, viaString)));
                    }

                    // ensure all endpoints with this ListenUriInfo have same identity
                    if (!object.Equals(endpoint.Address.Identity, identity))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
                                                                                      SR.Format(SR.SFxWhenMultipleEndpointsShareAListenUriTheyMustHaveSameIdentity, viaString)));
                    }

                    // add binding parameters (endpoint scope and below)
                    AddBindingParametersForSecurityContractInformation(endpoint, parameters);
                    AddBindingParameters(endpoint, parameters);
                }

                List <Type> channelTypes = GetSupportedChannelTypes(stuff.Value);

                var bindingQname      = new XmlQualifiedName(binding.Name, binding.Namespace);
                var channelDispatcher = new ChannelDispatcher(listenUri, binding, bindingQname.ToString(), binding, channelTypes);
                //channelDispatcher.SetEndpointAddressTable(endpointAddressTable);
                stuff.Value.ChannelDispatcher = channelDispatcher;

                for (int i = 0; i < stuff.Value.Endpoints.Count; i++)
                {
                    ServiceEndpoint endpoint  = stuff.Value.Endpoints[i];
                    string          viaString = listenUri.AbsoluteUri;

                    //EndpointFilterProvider provider = new EndpointFilterProvider();
                    EndpointDispatcher dispatcher = BuildEndpointDispatcher(description, endpoint);

                    if (!endpointInfosPerEndpointAddress.ContainsKey(endpoint.Address))
                    {
                        endpointInfosPerEndpointAddress.Add(endpoint.Address, new Collection <EndpointInfo>());
                    }

                    endpointInfosPerEndpointAddress[endpoint.Address].Add(new EndpointInfo(endpoint, dispatcher, /*provider*/ null));
                    channelDispatcher.Endpoints.Add(dispatcher);
                } // end foreach "endpoint"

                serviceHost.ChannelDispatchers.Add(channelDispatcher);
            } // end foreach "ListenUri/ChannelDispatcher" group

            // run service behaviors
            for (int i = 0; i < description.Behaviors.Count; i++)
            {
                IServiceBehavior serviceBehavior = description.Behaviors[i];
                serviceBehavior.ApplyDispatchBehavior(description, serviceHost);
            }

            foreach (KeyValuePair <ListenUriInfo, StuffPerListenUriInfo> stuff in stuffPerListenUriInfo)
            {
                for (int i = 0; i < stuff.Value.Endpoints.Count; i++)
                {
                    ServiceEndpoint endpoint = stuff.Value.Endpoints[i];
                    // rediscover which dispatcher goes with this endpoint
                    Collection <EndpointInfo> infos = endpointInfosPerEndpointAddress[endpoint.Address];
                    EndpointInfo info = null;
                    foreach (EndpointInfo ei in infos)
                    {
                        if (ei.Endpoint == endpoint)
                        {
                            info = ei;
                            break;
                        }
                    }
                    EndpointDispatcher dispatcher = info.EndpointDispatcher;
                    // run contract behaviors
                    for (int k = 0; k < endpoint.Contract.Behaviors.Count; k++)
                    {
                        IContractBehavior behavior = endpoint.Contract.Behaviors[k];
                        behavior.ApplyDispatchBehavior(endpoint.Contract, endpoint, dispatcher.DispatchRuntime);
                    }
                    // run endpoint behaviors
                    ApplyBindingInformationFromEndpointToDispatcher(endpoint, dispatcher);
                    for (int j = 0; j < endpoint.Behaviors.Count; j++)
                    {
                        IEndpointBehavior eb = endpoint.Behaviors[j];
                        eb.ApplyDispatchBehavior(endpoint, dispatcher);
                    }
                    // run operation behaviors
                    BindOperations(endpoint.Contract, null, dispatcher.DispatchRuntime);
                }
            }

            EnsureRequiredRuntimeProperties(endpointInfosPerEndpointAddress);

            // Warn about obvious demux conflicts
            foreach (Collection <EndpointInfo> endpointInfos in endpointInfosPerEndpointAddress.Values)
            {
                // all elements of endpointInfos share the same Address (and thus EndpointListener.AddressFilter)
                if (endpointInfos.Count > 1)
                {
                    for (int i = 0; i < endpointInfos.Count; i++)
                    {
                        for (int j = i + 1; j < endpointInfos.Count; j++)
                        {
                            // if not same ListenUri, won't conflict
                            // if not same ChannelType, may not conflict (some transports demux based on this)
                            // if they share a ChannelDispatcher, this means same ListenUri and same ChannelType
                            if (endpointInfos[i].EndpointDispatcher.ChannelDispatcher ==
                                endpointInfos[j].EndpointDispatcher.ChannelDispatcher)
                            {
                                EndpointFilterProvider iProvider = endpointInfos[i].FilterProvider;
                                EndpointFilterProvider jProvider = endpointInfos[j].FilterProvider;
                                // if not default EndpointFilterProvider, we won't try to throw, you're on your own
                                string commonAction;
                                if (iProvider != null && jProvider != null &&
                                    HaveCommonInitiatingActions(iProvider, jProvider, out commonAction))
                                {
                                    // you will definitely get a MultipleFiltersMatchedException at runtime,
                                    // so let's go ahead and throw now
                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                              new InvalidOperationException(
                                                  SR.Format(SR.SFxDuplicateInitiatingActionAtSameVia, endpointInfos[i].Endpoint.ListenUri, commonAction)));
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #5
0
 /// <summary>
 /// Applies the dispatch behavior.
 /// </summary>
 /// <param name="serviceDescription">The description.</param>
 /// <param name="serviceHostBase">The service host base.</param>
 public void ApplyDispatchBehavior(
     ServiceDescription serviceDescription,
     ServiceHostBase serviceHostBase)
 {
     _serviceBehavior.ApplyDispatchBehavior(serviceDescription, serviceHostBase);
 }
コード例 #6
0
 public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
 {
     _instance.ApplyDispatchBehavior(serviceDescription, serviceHostBase);
 }