Example #1
0
        internal static WcfChannelWrapper <T> CreateServiceClient <T>() where T : class
        {
            try
            {
                string         typeName = typeof(T).FullName;
                ChannelFactory cf;

                if (!channelFactoryCache.TryGetValue(typeName, out cf))
                {
                    lock (cacheLocker)
                    {
                        if (!channelFactoryCache.TryGetValue(typeName, out cf))
                        {
                            if (typeof(T) == typeof(IWcfConfigService))
                            {
                                var configServiceAddress = CommonConfiguration.GetConfig().WcfConfigServiceAddress;
                                var binding = new NetTcpBinding();
                                binding.Security.Mode = SecurityMode.None;
                                var address = string.Format("net.tcp://{0}", configServiceAddress);
                                cf = new ChannelFactory <IWcfConfigService>(binding, address);
                            }
                            else if (typeof(T) == typeof(IConfigServer))
                            {
                                var configServiceAddress = CommonConfiguration.GetConfig().ConfigServiceAddress;
                                var binding = new NetTcpBinding();
                                binding.Security.Mode          = SecurityMode.None;
                                binding.MaxReceivedMessageSize = 6553600;
                                //var address = string.Format("net.tcp://{0}", configServiceAddress);
                                cf = new ChannelFactory <IConfigServer>(binding, configServiceAddress);
                            }
                            else
                            {
                                var endpoint = GetWcfClientEndpointConfiguration(typeof(T));
                                cf = CreateChannelFactory <T>(endpoint);

                                WcfSettingManager.Init <T>();
                            }

                            if (cf != null)
                            {
                                channelFactoryCache[typeName] = cf;
                            }
                        }
                    }
                }

                return(new WcfChannelWrapper <T>((cf as ChannelFactory <T>).CreateChannel()));
            }
            catch (Exception ex)
            {
                ex.Handle(WcfLogProvider.ModuleName, "WcfServiceClientFactory", "CreateServiceClient");
                throw;
            }
        }
Example #2
0
        public static ServiceHost CreateServiceHost(Type serviceType)
        {
            var serviceHost = new ServiceHost(serviceType);

            if (!typeof(IWcfConfigService).IsAssignableFrom(serviceType) && !typeof(IConfigServer).IsAssignableFrom(serviceType))
            {
                WcfSettingManager.Init(serviceType);

                if (serviceHost.Description.Behaviors.Find <ServiceErrorBehavior>() == null)
                {
                    serviceHost.Description.Behaviors.Add(new ServiceErrorBehavior());
                }
                if (serviceHost.Description.Behaviors.Find <ActionInterceptBehavior>() == null)
                {
                    serviceHost.Description.Behaviors.Add(new ActionInterceptBehavior());
                }

                if (WcfSettingManager.CurrentServerSetting(serviceType).WcfCoreSetting.EnableUnity)
                {
                    if (serviceHost.Description.Behaviors.Find <UnityServiceBehavior>() == null)
                    {
                        serviceHost.Description.Behaviors.Add(new UnityServiceBehavior());
                    }
                }

                serviceHost.Description.Endpoints.Clear();
                var wcfService = GetWcfServiceConfiguration(serviceType);
                if (wcfService == null)
                {
                    throw new Exception("不能找到Wcf服务端配置,请检查配置数据库!");
                }

                var bindingCache = new Dictionary <string, Binding>();

                foreach (var ep in wcfService.Endpoints)
                {
                    string address = ConfigHelper.CreateAddress(
                        ep.EndpointProtocol,
                        Environment.MachineName,
                        ep.EndpointPort,
                        ep.EndpointName);

                    Binding binding;
                    if (!bindingCache.TryGetValue(address, out binding))
                    {
                        binding = ConfigHelper.CreateBinding(ep.EndpointBindingType, ep.EndpointBindingXml);
                        bindingCache[address] = binding;
                    }

                    serviceHost.AddServiceEndpoint(ep.ServiceContractType, binding, address);

                    if (!string.IsNullOrEmpty(ep.EndpointBehaviorXml))
                    {
                        AddEndPointBehavior(serviceHost.Description.Endpoints.Last(), ep.EndpointBehaviorXml);
                    }
                }

                foreach (var ep in serviceHost.Description.Endpoints)
                {
                    ep.Behaviors.Add(new MessageInspectorEndpointBehavior());
                }

                if (!string.IsNullOrEmpty(wcfService.ServiceBehaviorXml))
                {
                    AddServiceBehavior(serviceHost, wcfService.ServiceBehaviorXml);
                }

                serviceHost.Opened += (sender, o) =>
                {
                    if (WcfSettingManager.CurrentServerSetting(serviceType).WcfLogSetting.StartInfoSetting.Enabled)
                    {
                        var log = WcfLogProvider.GetServerStartInfo(serviceType.FullName, "WcfServiceHostFactory.CreateServiceHost", wcfService);
                        MongodbService.MongodbInsertService.Insert(log);
                    }
                };

                lock (performanceServices)
                {
                    if (!performanceServices.ContainsKey(serviceType.FullName))
                    {
                        var p = new WcfServerPerformanceService(serviceType.FullName, WcfSettingManager.CurrentServerSetting(serviceType).WcfPerformanceServiceSetting);
                        performanceServices.Add(serviceType.FullName, p);
                    }
                }
            }

            return(serviceHost);
        }