none
Programmatically add ServiceDebug behavior on same URL RRS feed

  • Question

  • I am working on a windows service to host WCF services for various projects. Using the following app.config fragment:

    <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:

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

    I can then open a web browser and go to, e.g. http://host:8001/services/MyWcfService, and I see the standard WCF help page so I know the service is working. (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.

    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. This all works just fine using 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. 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. Conceptually this makes sense: .NET doesn't want to have two endpoints listening on the same 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?

    Thursday, January 30, 2014 3:52 PM

Answers