![](/img/trans.png)
[英]Turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server
[英]How to programatically add ServiceDebug behavior with endpoint?
我正在Windows服务上托管各种项目的WCF服务。 使用以下app.config
片段:
<system.serviceModel>
<services>
<service name="MyWcfService" behaviorConfiguration="MetaDataBehavior">
<endpoint contract="MyWcfService" binding="wsHttpBinding" address="" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MetaDataBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</bindings>
</system.serviceModel>
我可以在服务中的同一URL上公开股票服务的“帮助”页面和WSHttpBinding
,如下所示:
ServiceHost myServiceHost = new ServiceHost(typeof(MyWcfService), new Uri(serviceAddress));
myServiceHost.Open()
然后,我可以打开Web浏览器并转到例如http://host:8001/services/MyWcfService
,然后看到标准的WCF帮助页面,因此我知道该服务正在运行。 (目前,让我们忽略暴露帮助页面的安全隐患。)我还可以在同一URL上访问WSHttpBinding
端点。
现在,我们已经可以在该服务中托管许多WCF服务,因此,我正在通过以编程方式将端点添加到ServiceHost
对象来简化配置。 使用myServiceHost.AddServiceEndpoint()
都可以正常工作。
我无法以编程方式获得的最后一部分是在与WSHttpBinding
相同的URL上启用ServiceDebug
行为。 我有以下几点:
ServiceDebugBehavior sdb = myServiceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
if (sdb == null)
{
sdb = new ServiceDebugBehavior()
{
HttpHelpPageEnabled = true,
IncludeExceptionDetailInFaults = true
};
myServiceHost.Description.Behaviors.Add(sdb);
}
else
{
sdb.HttpHelpPageEnabled = true;
sdb.IncludeExceptionDetailInFaults = true;
//sdb.HttpHelpPageUrl = new Uri(serviceAddress +"/help");
}
ServiceEndpoint endpoint = new ServiceEndpoint(contract, new WSHttpBinding, new EndpointAddress(new Uri(serviceAddress)));
myServiceHost.AddServiceEndpoint(endpoint);
只要我a)更改帮助页面的地址(如上所述),或b)更改WSHttpBinding
侦听的URI,该方法就WSHttpBinding
。 从概念上讲,这是有道理的:.NET不想让两个终结点侦听同一URI。
我的问题是,我必须使用此服务保持与现有应用程序的兼容性,这意味着我的端点地址无法更改。 如果我可以通过app.config
完成此操作,为什么不能以编程方式完成此操作?
您实际上无法将ServiceDebugBehavior添加到ServiceHost,您需要做的是修改现有的ServiceDebugBehavior(存在相同的问题)
例
svcHost = new ServiceHost(typeof(MyService), adrbase);
// Configure Your Service
// Now for the ServiceDebugBehavior you want to modify (Example disable HTTP Help Page)
svcHost.Description.Behaviors.Find<ServiceDebugBehavior>().HttpHelpPageEnabled = false;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.