gpt4 book ai didi

c# - 在 WCF 的代码中将 IncludeExceptionDetailInFaults 设置为 true

转载 作者:IT王子 更新时间:2023-10-29 03:42:09 27 4
gpt4 key购买 nike

如何在不使用 App.Config 的情况下在代码中设置 IncludeExceptionDetailInFaults?

最佳答案

是的,当然 - 在服务器端,在您打开服务主机之前。但是,这需要您自行托管 WCF 服务 - 在 IIS 托管方案中不起作用:

ServiceHost host = new ServiceHost(typeof(MyWCFService));

ServiceDebugBehavior debug = host.Description.Behaviors.Find<ServiceDebugBehavior>();

// if not found - add behavior with setting turned on
if (debug == null)
{
host.Description.Behaviors.Add(
new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
else
{
// make sure setting is turned ON
if (!debug.IncludeExceptionDetailInFaults)
{
debug.IncludeExceptionDetailInFaults = true;
}
}

host.Open();

如果您需要在 IIS 托管中做同样的事情,您必须创建自己的自定义 MyServiceHost 后代和一个合适的 MyServiceHostFactory 来实例化这样的自定义服务主机,并在您的 *.svc 文件中引用此自定义服务主机工厂。

关于c# - 在 WCF 的代码中将 IncludeExceptionDetailInFaults 设置为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2483178/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com