Exemplo n.º 1
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;
		}
		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));
				}
			}
		}