【问题标题】:Configuring WCF Services in Code WCF 4.5在代码 WCF 4.5 中配置 WCF 服务
【发布时间】: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 服务的简单方法。也就是说,在服务主机启动后,您无法添加端点。但是每次激活服务时都会调用这个方法。

标签: asp.net wcf c#-4.0


【解决方案1】:

.NET 4.5 中的新功能可能适用于您的情况:

注意:configure方法是在服务主机打开之前被WCF调用的。

Configure 方法采用一个 ServiceConfiguration 实例,使开发人员能够添加端点和行为。此方法在服务主机打开之前由 WCF 调用。定义后,将忽略 app.config 或 web.config 文件中指定的任何服务配置设置。 以下代码sn-p说明了如何定义Configure方法并添加服务端点、端点行为和服务行为:

public class Service1 : IService1
{
    public static void Configure(ServiceConfiguration config)
    {
        ServiceEndpoint se = new ServiceEndpoint(new ContractDescription("IService1"), new BasicHttpBinding(), new EndpointAddress("basic"));
        se.Behaviors.Add(new MyEndpointBehavior());
        config.AddServiceEndpoint(se);

        config.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
        config.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true });
    }

    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

完整示例请参考:https://msdn.microsoft.com/en-us/library/hh205277(v=vs.110).aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多