Beispiel #1
0
        private static FindCriteria CreateSearchCriteria(Type contract, DiscoveredEndpointModel model)
        {
            var searchContract = model.SearchContract ?? contract;
            var criteria       = model.SearchAnyContract ? new FindCriteria() : new FindCriteria(searchContract);

            criteria.Duration   = model.Duration.GetValueOrDefault(DefaultDuration);
            criteria.MaxResults = model.MaxResults;

            var discovery = model.Extensions.OfType <WcfInstanceExtension>()
                            .Select(extension => extension.Instance)
                            .OfType <WcfEndpointDiscoveryMetadata>()
                            .FirstOrDefault();

            if (discovery != null)
            {
                criteria.Scopes.AddAll(discovery.Scopes);

                if (model.ScopeMatchBy != null)
                {
                    criteria.ScopeMatchBy = model.ScopeMatchBy;
                }

                criteria.Extensions.AddAll(discovery.Extensions);
            }

            return(criteria);
        }
Beispiel #2
0
        private FindCriteria CreateSearchCriteria(DiscoveredEndpointModel model)
        {
            var searchContract = model.SearchContract ?? contract;
            var criteria       = new FindCriteria(searchContract)
            {
                MaxResults = model.MaxResults
            };

            if (model.Duration.HasValue)
            {
                criteria.Duration = model.Duration.Value;
            }

            var discovery = model.Extensions.OfType <WcfInstanceExtension>()
                            .Select(extension => extension.Instance)
                            .OfType <EndpointDiscoveryBehavior>()
                            .FirstOrDefault();

            if (discovery != null)
            {
                criteria.Scopes.AddAll(discovery.Scopes);

                if (model.ScopeMatchBy != null)
                {
                    criteria.ScopeMatchBy = model.ScopeMatchBy;
                }

                criteria.Extensions.AddAll(discovery.Extensions);
            }

            return(criteria);
        }
Beispiel #3
0
        private void DiscoverEndpoint(DiscoveryEndpoint discoveryEndpoint, DiscoveredEndpointModel model)
        {
            using (var discover = new DiscoveryClient(discoveryEndpoint))
            {
                var criteria = CreateSearchCriteria(model);

                var discovered = discover.Find(criteria);
                if (discovered.Endpoints.Count > 0)
                {
                    var binding          = model.Binding;
                    var endpointMetadata = discovered.Endpoints[0];
                    if (discovered.Endpoints.Count > 1 && model.EndpointPreference != null)
                    {
                        endpointMetadata = model.EndpointPreference(discovered.Endpoints);
                        if (endpointMetadata == null)
                        {
                            throw new EndpointNotFoundException(string.Format(
                                                                    "More than one endpoint was discovered for contract {0}.  " +
                                                                    "However, an endpoint could be selected.  This is most likely " +
                                                                    "a bug with the user-defined endpoint prefeence.",
                                                                    contract.FullName));
                        }
                    }

                    if (binding == null && model.DeriveBinding == false)
                    {
                        binding = GetBindingFromMetadata(endpointMetadata);
                    }

                    var address = endpointMetadata.Address;
                    if (model.Identity != null)
                    {
                        address = new EndpointAddress(address.Uri, model.Identity, address.Headers);
                    }

                    binding = GetEffectiveBinding(binding, address.Uri);
                    var innerCreator = GetChannel(contract, binding, address);
                    channelCreator = () =>
                    {
                        var channel = (IChannel)innerCreator();
                        if (channel is IContextChannel)
                        {
                            var metadata = new DiscoveredEndpointMetadata(endpointMetadata);
                            ((IContextChannel)channel).Extensions.Add(metadata);
                        }
                        return(channel);
                    };
                }
                else
                {
                    throw new EndpointNotFoundException(string.Format(
                                                            "Unable to discover the endpoint for contract {0}.  " +
                                                            "Either no service exists or it does not support discovery.",
                                                            contract.FullName));
                }
            }
        }
Beispiel #4
0
        internal object DiscoverChannel(DiscoveredEndpointModel model, IChannelBuilderScope scope)
        {
            var discovered = PerformEndpointSearch(scope.Contract, model);

            if (discovered == null || discovered.Endpoints.Count == 0)
            {
                throw new EndpointNotFoundException(string.Format(
                                                        "Unable to discover the endpoint for contract {0}.  " +
                                                        "Either no service exists or it does not support discovery.",
                                                        scope.Contract.FullName));
            }

            var binding          = model.Binding;
            var endpointMetadata = discovered.Endpoints[0];

            if (discovered.Endpoints.Count > 1 && model.EndpointPreference != null)
            {
                endpointMetadata = model.EndpointPreference(discovered.Endpoints);
                if (endpointMetadata == null)
                {
                    throw new EndpointNotFoundException(string.Format(
                                                            "More than one endpoint was discovered for contract {0}.  " +
                                                            "However, an endpoint was not selected.  This is most likely " +
                                                            "a bug with the user-defined endpoint preference.",
                                                            scope.Contract.FullName));
                }
            }

            if (binding == null && model.DeriveBinding == false)
            {
                binding = GetBindingFromMetadata(endpointMetadata);
            }

            var address = endpointMetadata.Address;

            if (model.Identity != null)
            {
                address = new EndpointAddress(address.Uri, model.Identity, address.Headers);
            }

            binding = GetEffectiveBinding(binding, address.Uri);

            var channel = scope.GetChannel(scope.Contract, binding, address)();

            if (channel is IContextChannel)
            {
                var metadata = new DiscoveredEndpointMetadata(endpointMetadata);
                ((IContextChannel)channel).Extensions.Add(metadata);
            }

            return(channel);
        }
Beispiel #5
0
        private FindResponse PerformEndpointSearch(Type contract, DiscoveredEndpointModel model)
        {
            var criteria = CreateSearchCriteria(contract, model);

            for (int i = 0; i < 2; ++i)
            {
                var discoveryEndpoint = GetDiscoveryEndpoint(model);
                WcfBindingUtils.ConfigureQuotas(discoveryEndpoint.Binding, int.MaxValue);
                var discoveryClient = new DiscoveryClient(discoveryEndpoint);

                try
                {
                    return(discoveryClient.Find(criteria));
                }
                catch
                {
                    // ignore failures
                }
                finally
                {
                    try
                    {
                        discoveryClient.Close();
                    }
                    catch
                    {
                        // Discovery client often fails on close
                    }
                }

                // Possible stale discovery proxy...

                if (i == 0)
                {
                    var discoveryEndpointFaulted = DiscoveryEndpointFaulted;
                    if (discoveryEndpointFaulted != null)
                    {
                        discoveryEndpointFaulted(this, new DiscoveryEndpointFaultEventArgs(discoveryEndpoint));
                    }
                }
            }

            return(null);
        }
Beispiel #6
0
        private DiscoveryEndpoint GetDiscoveryEndpoint(DiscoveredEndpointModel model)
        {
            if (model.DiscoveryEndpoint != null)
            {
                return(model.DiscoveryEndpoint);
            }

            var provider = model.DiscoveryEndpointProvider ?? DiscoveryEndpointProvider;

            if (provider != null)
            {
                var discoveryEndpoint = provider.GetDiscoveryEndpoint();
                if (discoveryEndpoint != null)
                {
                    return(discoveryEndpoint);
                }
            }

            return(new UdpDiscoveryEndpoint());
        }
Beispiel #7
0
        void IWcfEndpointVisitor.VisitBindingDiscoveredEndpoint(DiscoveredEndpointModel model)
        {
            var discoveryEndpoint = model.DiscoveryEndpoint ?? new UdpDiscoveryEndpoint();

            DiscoverEndpoint(discoveryEndpoint, model);
        }
 void IWcfEndpointVisitor.VisitBindingDiscoveredEndpoint(DiscoveredEndpointModel model)
 {
     throw new InvalidOperationException("Discovered endpoints can only by applied for clients.");
 }
 void IWcfEndpointVisitor.VisitBindingDiscoveredEndpoint(DiscoveredEndpointModel model)
 {
     channelCreator = () => builder.DiscoverChannel(model, this);
 }