Exemple #1
0
        static ServiceDescription CreateDescription(Type serviceType)
        {
            ServiceDescription description = new ServiceDescription();

            //添加以特性方式应用的服务行为
            description.ServiceType = serviceType;
            var behaviors = (from attribute in serviceType.GetCustomAttributes(false)
                             where attribute is IServiceBehavior
                             select(IServiceBehavior) attribute).ToArray();

            Array.ForEach <IServiceBehavior>(behaviors, behavior => description.Behaviors.Add(behavior));

            //确保服务具有一个ServiceBehaviorAttribute行为
            ServiceBehaviorAttribute serviceBehaviorAttribute = description.Behaviors.Find <ServiceBehaviorAttribute>();

            if (null == serviceBehaviorAttribute)
            {
                serviceBehaviorAttribute = new ServiceBehaviorAttribute();
                description.Behaviors.Add(serviceBehaviorAttribute);
            }

            //初始化Name、Namespace和ConfigurationName
            description.Name              = serviceBehaviorAttribute.Name ?? serviceType.Name;
            description.Namespace         = serviceBehaviorAttribute.Namespace ?? "http://tempuri.org/";
            description.ConfigurationName = serviceBehaviorAttribute.ConfigurationName ?? serviceType.Namespace + "." + serviceType.Name;

            //添加以配置方式应用的服务行为
            ServiceElement serviceElement = ConfigLoader.GetServiceElement(description.ConfigurationName);

            if (!string.IsNullOrEmpty(serviceElement.BehaviorConfiguration))
            {
                ServiceBehaviorElement behaviorElement = ConfigLoader.GetServiceBehaviorElement(serviceElement.BehaviorConfiguration);
                foreach (BehaviorExtensionElement extensionElement in behaviorElement)
                {
                    IServiceBehavior serviceBehavior = (IServiceBehavior)extensionElement.CreateBehavior();
                    description.Behaviors.Add(serviceBehavior);
                }
            }

            //添加配置的终结点
            foreach (ServiceEndpointElement endpointElement in serviceElement.Endpoints)
            {
                description.Endpoints.Add(CreateServiceEndpoint(serviceType, endpointElement));
            }
            return(description);
        }
Exemple #2
0
        static ServiceEndpoint CreateServiceEndpoint(Type serviceType, ServiceEndpointElement endpointElement)
        {
            //创建ServiceEndpoint
            EndpointAddress     address  = new EndpointAddress(endpointElement.Address);
            Binding             binding  = ConfigLoader.CreateBinding(endpointElement.Binding);
            ContractDescription contract = CreateContractDescription(serviceType, endpointElement.Contract);
            ServiceEndpoint     endpoint = new ServiceEndpoint(contract, binding, address);

            //添加终结点行为
            if (!string.IsNullOrEmpty(endpointElement.BehaviorConfiguration))
            {
                EndpointBehaviorElement behaviorElement = ConfigLoader.GetEndpointBehaviorElement(endpointElement.BehaviorConfiguration);
                foreach (BehaviorExtensionElement extensionElement in behaviorElement)
                {
                    IEndpointBehavior endpointBehavior = (IEndpointBehavior)extensionElement.CreateBehavior();
                    endpoint.Behaviors.Add(endpointBehavior);
                }
            }
            return(endpoint);
        }