【发布时间】:2013-08-15 16:20:34
【问题描述】:
您好,我正在尝试使用后面的代码配置 wcf,下面是代码:
public static void Configure(ServiceConfiguration config)
{
string configPath = ConfigurationManager.AppSettings["wcfconfigDBPath"];
// Enable “Add Service Reference” support
config.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
// set up support for http, https, net.tcp, net.pipe
if (isEnabled(configPath, "enablehttp"))
config.EnableProtocol(new BasicHttpBinding());
if (isEnabled(configPath, "enablenettcp"))
config.EnableProtocol(new NetTcpBinding());
if (isEnabled(configPath, "enablepipe"))
config.EnableProtocol(new NetNamedPipeBinding());
}
private static bool isEnabled(string path, string elementName)
{
try
{
string elementValue = string.Empty;
bool returnVal = false;
using (XmlTextReader reader = new XmlTextReader(path))
{
reader.ReadToFollowing(elementName);
if (reader.Read())
elementValue = reader.Value;
}
if (!string.IsNullOrEmpty(elementValue))
{
bool.TryParse(elementValue, out returnVal);
}
return returnVal;
}
catch (Exception ex)
{
return false;
}
}
上面的代码不起作用。我不确定“静态无效配置”何时被触发。
我的问题是,有没有办法在不关闭服务的情况下根据 DB/xml 配置启用/禁用协议?
【问题讨论】:
-
这个配置方法会在服务主机打开前被调用一次。此功能旨在提供一种在代码中配置 Web 托管 WCF 服务的简单方法。也就是说,在服务主机启动后,您无法添加端点。但是每次激活服务时都会调用这个方法。