使用代码添加WCF服务行为

24

我知道可以通过一些XML配置来添加服务行为,但我想用一段类似于添加终结点行为的C#代码来完成。不过我不确定该如何做。

换句话说,我该如何添加我已经实例化的调试行为?

var host = new ServiceHost(typeof(MyService));
var endpoint = host.AddServiceEndpoint(typeof (MysService), 
    new WebHttpBinding(), new Uri(myURL));
endpoint.Behaviors.Add(new WebHttpBehavior());
var debug = new ServiceDebugBehavior
{
    IncludeExceptionDetailInFaults = true
};
//WHAT DO I PUT HERE?
host.Open();
2个回答

35
host.Description.Behaviors.Add(debug);

6
谢谢!结果发现我不能使用.Add(),因为这个行为已经存在了。但是这个方法很有效:host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true - user24359
如果这个行为是在配置文件中定义的,但服务是在代码中定义的,该如何将其添加到 WCF 主机中? - FrenkyB

4
您可以为您的服务编写属性,例如:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class Service1 : IServiceChild

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接