コード例 #1
0
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            var serviceBehavior = serviceDescription.Behaviors.Find <ServiceBehaviorAttribute>();

            bool singleInstance = (serviceBehavior != null &&
                                   serviceBehavior.InstanceContextMode == InstanceContextMode.Single);

            var contractNameToContractType = new Dictionary <string, Type>();

#if DOTNET40
            var endpoints = serviceDescription.NonSystemEndpoints();
#else
            var endpoints = serviceDescription.Endpoints;
#endif

            foreach (var endpoint in endpoints)
            {
                contractNameToContractType[endpoint.Contract.Name] = endpoint.Contract.ContractType;
            }

            foreach (var cdb in serviceHostBase.ChannelDispatchers)
            {
                var cd = cdb as ChannelDispatcher;

                if (cd != null)
                {
                    foreach (var ed in cd.Endpoints)
                    {
                        if (contractNameToContractType.ContainsKey(ed.ContractName))
                        {
                            if (singleInstance)
                            {
                                ed.DispatchRuntime.SingletonInstanceContext =
                                    new InstanceContext(serviceHostBase);
                            }

                            ed.DispatchRuntime.InstanceProvider =
                                new WindsorInstanceProvider(kernel, model,
                                                            contractNameToContractType[ed.ContractName],
                                                            serviceDescription.ServiceType
                                                            );
                        }
                    }
                }
            }
        }