WCF 的Endpoints Configuration如果存在数据库表的某个字段而非配置文件,类型为string或xml。如果创建ServerHost?谢谢指教。

解决方案 »

  1.   

    所有写在app.config中与ENDPOINT有关的配置都可以用其他方式存储及获得
    例如:
    public CustomServiceHost(Uri uri, string configData){this.InitConfig(configData);this.InitializeDescription(new UriSchemeKeyedCollection(new Uri[] { uri }));
    }private void InitConfig(string configData){this._config = ConfigHelper.GetConfig(configData);ValidateConfig(configData);}private void ValidateConfig(string configData){//TODO:}System.Configuration.Configuration _config;public System.Configuration.Configuration Config{get { return _config; }set { _config = value; }}protected override System.ServiceModel.Description.ServiceDescription CreateDescription(out IDictionary<string, System.ServiceModel.Description.ContractDescription> implementedContracts){Dictionary<string, ContractDescription> contracts = new Dictionary<string, ContractDescription>();List<ServiceEndpoint> endpoints = new List<ServiceEndpoint>(); Configuration c = this.Config; ServicesSection ss = c.GetSection("system.serviceModel/services") as ServicesSection;ServiceElement service = ss.Services[0];ServiceEndpointElement epe = service.Endpoints[0];
    ContractDescription cd = ContractDescription.GetContract(ContractType(epe.Contract));contracts.Add(cd.Name, cd);foreach (OperationDescription od in cd.Operations)od.Behaviors.Add(new AdapterOperationBehavior());Binding b = GetBinding(epe.Binding);EndpointAddress ea = new EndpointAddress(epe.Address);ServiceEndpoint ep = new ServiceEndpoint(cd, b, ea);endpoints.Add(ep);ServiceDescription sd = new ServiceDescription(endpoints);
    ServiceBehaviorAttribute sb = new ServiceBehaviorAttribute();sb.SetWellKnownSingleton(this);sb.InstanceContextMode = InstanceContextMode.Single;sd.Behaviors.Add(sb);//add behaviors ServiceMetadataBehavior smb = new ServiceMetadataBehavior();smb.HttpGetEnabled = true;sd.Behaviors.Add(smb);implementedContracts = contracts;return sd;}private Binding GetBinding(string name){BindingsSection bindings = this.Config.GetSection("system.serviceModel/bindings") as BindingsSection;BindingCollectionElement bc = bindings[name];Binding b = Activator.CreateInstance(bc.BindingType) as Binding;return b;}Type ContractType(string type){Type t = null;t = Type.GetType(type);return t;}