简体   繁体   English

如何以编程方式添加带有端点的ServiceDebug行为?

[英]How to programatically add ServiceDebug behavior with endpoint?

I am working on a windows service to host WCF services for various projects. 我正在Windows服务上托管各种项目的WCF服务。 Using the following app.config fragment: 使用以下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>

I can expose the stock service "help" page and a WSHttpBinding on the same URL from within my service like so: 我可以在服务中的同一URL上公开股票服务的“帮助”页面和WSHttpBinding ,如下所示:

ServiceHost myServiceHost = new ServiceHost(typeof(MyWcfService), new Uri(serviceAddress));
myServiceHost.Open()

I can then open a web browser and go to, eg http://host:8001/services/MyWcfService , and I see the standard WCF help page so I know the service is working. 然后,我可以打开Web浏览器并转到例如http://host:8001/services/MyWcfService ,然后看到标准的WCF帮助页面,因此我知道该服务正在运行。 (For the moment, let's ignore the security implications of exposing the help page.) I can also access the WSHttpBinding endpoint on the same URL . (目前,让我们忽略暴露帮助页面的安全隐患。)我还可以在同一URL上访问WSHttpBinding端点。

We have now grown to host a lot of WCF services within this service, so I'm working to simplify the configuration by adding endpoints programatically to the ServiceHost object. 现在,我们已经可以在该服务中托管许多WCF服务,因此,我正在通过以编程方式将端点添加到ServiceHost对象来简化配置。 This all works just fine using myServiceHost.AddServiceEndpoint() . 使用myServiceHost.AddServiceEndpoint()都可以正常工作。

The last piece I haven't been able to get programatically is enabling the ServiceDebug behavior on the same URL as the WSHttpBinding . 我无法以编程方式获得的最后一部分是在与WSHttpBinding相同的URL上启用ServiceDebug行为。 I have the following: 我有以下几点:

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);

which works, so long as I a) change the address of the help page (as commented above), or b) change the URI on which the WSHttpBinding listens. 只要我a)更改帮助页面的地址(如上所述),或b)更改WSHttpBinding侦听的URI,该方法就WSHttpBinding Conceptually this makes sense: .NET doesn't want to have two endpoints listening on the same URI. 从概念上讲,这是有道理的:.NET不想让两个终结点侦听同一URI。

My problem is that I have to maintain compatibility with existing applications using this service, which means my endpoint addresses cannot change. 我的问题是,我必须使用此服务保持与现有应用程序的兼容性,这意味着我的端点地址无法更改。 If I can accomplish this through the app.config , why can I not accomplish this programatically? 如果我可以通过app.config完成此操作,为什么不能以编程方式完成此操作?

You can't actually ADD a ServiceDebugBehavior to a ServiceHost, what you have to do is modify the existing ServiceDebugBehavior (was having the same issue) 您实际上无法将ServiceDebugBehavior添加到ServiceHost,您需要做的是修改现有的ServiceDebugBehavior(存在相同的问题)

Example

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM