Programming Newbie
Programming Newbie

Reputation: 1227

Configuration.GetSection("basicHttpBinding") not reading that section from app.config file

I have the following property:

protected BasicHttpBinding Binding
{
    get
    {
        var config = ConfigurationManager.GetSection("basicHttpBinding") as ServiceModelSectionGroup;
        foreach (ChannelEndpointElement bindings in config.Bindings.BasicHttpBinding.Bindings)
        {
            string binding = bindings.Binding;

            if (binding != null)
            {
                return new BasicHttpBinding(binding);
            }
        }
        return null;
    }
}           

When I debug it, it fails with a null exception: Object reference not set to an instance of an object at this line:

foreach (ChannelEndpointElement bindings in config.Bindings.BasicHttpBinding.Bindings)

however, I notice that the line above it is also null.

here is the app.config file

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="IntelexWSSoap" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="Transport">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>

.....

            </basicHttpBinding>
        </bindings>
    ....
    </system.serviceModel>
</configuration>

I can't figure out why it's failing.

Upvotes: 1

Views: 6825

Answers (2)

to StackOverflow
to StackOverflow

Reputation: 124726

Try this:

var config = ConfigurationManager.GetSection("system.serviceModel/bindings") as   
                        System.ServiceModel.Configuration.BindingsSection;

<basicHttpBinding> isn't a configuration section, it's an element within the <bindings> configuration section.

Upvotes: 4

Eren Ers&#246;nmez
Eren Ers&#246;nmez

Reputation: 39085

Looks like you're getting the "basicHttpBinding" section but then trying to cast it as ServiceModelSectionGroup which refers to the "system.serviceModel" section so it returns null.

Try ConfigurationManager.GetSection("system.serviceModel") instead.

Upvotes: 3

Related Questions