GetContract() private method

private GetContract ( Type contractType ) : ContractDescription
contractType System.Type
return ContractDescription
Example #1
0
        // FIXME: distinguish HTTP and HTTPS in the Url properties.
        // FIXME: reject such ServiceDescription that has no HTTP(S) binding.
        // FIXME: it should not use the binding that is used in the ServiceEndpoint. For example, WSDL results have to be given as text, not binary.
        // FIXME: if the ServiceDescription has a base address (e.g. http://localhost:8080) and HttpGetUrl is empty, it returns UnknownDestination while it is expected to return the HTTP help page.
        internal void EnsureChannelDispatcher(bool isMex, string scheme, Uri uri, WCFBinding binding)
        {
            if (isMex)
            {
                instance.WsdlUrl = uri;
            }
            else
            {
                instance.HelpUrl = uri;
            }

            if (dispatchers == null)
            {
                dispatchers = new Dictionary <Uri, ChannelDispatcher> ();
            }
            else if (dispatchers.ContainsKey(uri))
            {
                var info = dispatchers [uri].Listener.GetProperty <MetadataPublishingInfo> ();
                if (isMex)
                {
                    info.SupportsMex = true;
                }
                else
                {
                    info.SupportsHelp = true;
                }
                return;
            }

            if (binding == null)
            {
                switch (scheme)
                {
                case "http":
                    binding = MetadataExchangeBindings.CreateMexHttpBinding();
                    break;

                case "https":
                    binding = MetadataExchangeBindings.CreateMexHttpsBinding();
                    break;

                case "net.tcp":
                    binding = MetadataExchangeBindings.CreateMexTcpBinding();
                    break;

                case "net.pipe":
                    binding = MetadataExchangeBindings.CreateMexNamedPipeBinding();
                    break;
                }
            }

            CustomBinding cb = new CustomBinding(binding)
            {
                Name = ServiceMetadataBehaviorHttpGetBinding
            };

            cb.Elements.Find <MessageEncodingBindingElement> ().MessageVersion = MessageVersion.None;

            ServiceEndpoint se = new ServiceEndpoint(ContractDescription.GetContract(typeof(IHttpGetHelpPageAndMetadataContract)), cb, new EndpointAddress(uri))
            {
                ListenUri = uri,
            };

            var channelDispatcher = new DispatcherBuilder().BuildChannelDispatcher(owner.Description.ServiceType, se, new BindingParameterCollection());
            // add HttpGetWsdl to indicate that the ChannelDispatcher is for mex or help.
            var listener = channelDispatcher.Listener as ChannelListenerBase;

            if (listener != null)
            {
                listener.Properties.Add(new MetadataPublishingInfo()
                {
                    SupportsMex = isMex, SupportsHelp = !isMex, Instance = instance
                });
            }
            channelDispatcher.Endpoints [0].DispatchRuntime.InstanceContextProvider = new SingletonInstanceContextProvider(new InstanceContext(owner, instance));

            dispatchers.Add(uri, channelDispatcher);
            owner.ChannelDispatchers.Add(channelDispatcher);
        }
        void AddMetadataEndpoint(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher, bool debugMode)
        {
            Uri baseAddress = endpoint.Address.Uri;

            if (baseAddress == null)
            {
                return;
            }

            ServiceHostBase host = endpointDispatcher.ChannelDispatcher.Host;

            UriBuilder builder = new UriBuilder(baseAddress);

            builder.Path += builder.Path.EndsWith("/", StringComparison.OrdinalIgnoreCase)
                ? (WebScriptClientGenerator.GetMetadataEndpointSuffix(debugMode))
                : ("/" + WebScriptClientGenerator.GetMetadataEndpointSuffix(debugMode));
            EndpointAddress metadataAddress = new EndpointAddress(builder.Uri);

            foreach (ServiceEndpoint serviceEndpoint in host.Description.Endpoints)
            {
                if (EndpointAddress.UriEquals(serviceEndpoint.Address.Uri, metadataAddress.Uri, true, false))//  ignoreCase //  includeHostNameInComparison
                {
                    throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              new InvalidOperationException(SR2.GetString(SR2.JsonNoEndpointAtMetadataAddress, this.GetType().ToString(), serviceEndpoint.Address, serviceEndpoint.Name, host.Description.Name)));
                }
            }

            HttpTransportBindingElement transportBindingElement;
            HttpTransportBindingElement existingTransportBindingElement = endpoint.Binding.CreateBindingElements().Find <HttpTransportBindingElement>();

            if (existingTransportBindingElement != null)
            {
                transportBindingElement = (HttpTransportBindingElement)existingTransportBindingElement.Clone();
            }
            else
            {
                if (baseAddress.Scheme == "https")
                {
                    transportBindingElement = new HttpsTransportBindingElement();
                }
                else
                {
                    transportBindingElement = new HttpTransportBindingElement();
                }
            }

            transportBindingElement.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            transportBindingElement.TransferMode           = TransferMode.Buffered;
            transportBindingElement.MaxBufferSize          = MaxMetadataEndpointBufferSize;
            transportBindingElement.MaxReceivedMessageSize = MaxMetadataEndpointBufferSize;
            Binding metadataBinding = new CustomBinding(
                new WebScriptMetadataMessageEncodingBindingElement(),
                transportBindingElement);
            BindingParameterCollection parameters = host.GetBindingParameters(endpoint);

            // build endpoint dispatcher
            ContractDescription  metadataContract           = ContractDescription.GetContract(typeof(ServiceMetadataExtension.IHttpGetMetadata));
            OperationDescription metadataOperation          = metadataContract.Operations[0];
            EndpointDispatcher   metadataEndpointDispatcher = new EndpointDispatcher(metadataAddress, metadataContract.Name, metadataContract.Namespace);
            DispatchOperation    dispatchOperation          = new DispatchOperation(metadataEndpointDispatcher.DispatchRuntime, metadataOperation.Name, metadataOperation.Messages[0].Action, metadataOperation.Messages[1].Action);

            dispatchOperation.Formatter = new WebScriptMetadataFormatter();
            dispatchOperation.Invoker   = new SyncMethodInvoker(metadataOperation.SyncMethod);
            metadataEndpointDispatcher.DispatchRuntime.Operations.Add(dispatchOperation);
            metadataEndpointDispatcher.DispatchRuntime.SingletonInstanceContext = new InstanceContext(host, new WebScriptClientGenerator(endpoint, debugMode, !String.IsNullOrEmpty(this.JavascriptCallbackParameterName)));
            metadataEndpointDispatcher.DispatchRuntime.InstanceContextProvider  = new SingletonInstanceContextProvider(metadataEndpointDispatcher.DispatchRuntime);

            // build channel dispatcher
            IChannelListener <IReplyChannel> listener = null;

            if (metadataBinding.CanBuildChannelListener <IReplyChannel>(parameters))
            {
                listener = metadataBinding.BuildChannelListener <IReplyChannel>(metadataAddress.Uri, parameters);
            }
            ChannelDispatcher metadataChannelDispatcher = new ChannelDispatcher(listener);

            metadataChannelDispatcher.MessageVersion = MessageVersion.None;
            metadataChannelDispatcher.Endpoints.Add(metadataEndpointDispatcher);

            host.ChannelDispatchers.Add(metadataChannelDispatcher);
        }