static EndpointDispatcher BuildDispatcher(ServiceHostBase service,
                                                  ServiceDescription serviceDescription,
                                                  ServiceEndpoint endpoint,
                                                  ContractDescription contractDescription,
                                                  EndpointFilterProvider provider)
        {
            if (service == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("service");
            }
            if (serviceDescription == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceDescription");
            }
            if (contractDescription == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contractDescription");
            }

            EndpointAddress address = endpoint.Address;
            EndpointDispatcher dispatcher = new EndpointDispatcher(address, contractDescription.Name, contractDescription.Namespace, endpoint.Id, endpoint.InternalIsSystemEndpoint(serviceDescription));

            DispatchRuntime dispatch = dispatcher.DispatchRuntime;
            if (contractDescription.CallbackContractType != null)
            {
                dispatch.CallbackClientRuntime.CallbackClientType = contractDescription.CallbackContractType;
                dispatch.CallbackClientRuntime.ContractClientType = contractDescription.ContractType;
            }

            for (int i = 0; i < contractDescription.Operations.Count; i++)
            {
                OperationDescription operation = contractDescription.Operations[i];

                if (!operation.IsServerInitiated())
                {
                    DispatcherBuilder.BuildDispatchOperation(operation, dispatch, provider);
                }
                else
                {
                    DispatcherBuilder.BuildProxyOperation(operation, dispatch.CallbackClientRuntime);
                }
            }

            //dispatcher.SetSupportedChannels(DispatcherBuilder.GetSupportedChannelTypes(contractDescription));
            int filterPriority = 0;
            dispatcher.ContractFilter = provider.CreateFilter(out filterPriority);
            dispatcher.FilterPriority = filterPriority;

            return dispatcher;
        }
        internal static void SetContractFilterToIncludeAllOperations(EndpointDispatcher dispatcher, ContractDescription contract)
        {
            if (dispatcher == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("dispatcher");
            }
            if (contract == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contract");
            }

            if (contract.SessionMode == SessionMode.Required)
            {
                EndpointFilterProvider provider = new EndpointFilterProvider();
                foreach (OperationDescription operation in contract.Operations)
                {
                    if (!operation.IsServerInitiated())
                    {
                        provider.InitiatingActions.Add(operation.Messages[0].Action);
                    }
                }
                int priority;
                dispatcher.ContractFilter = provider.CreateFilter(out priority);
                dispatcher.FilterPriority = priority;
            }
        }