How to read basicHttpBinding defined in app.config file

I am trying to understand how to read, how to read the basicHttpBinding section defined in the app.config file. I tried to do this, as I did the endpoint address, but it does not work. Here's how I made my endpoint address:

private EndpointAddress EndPointAddy { get { var config = ConfigurationManager.GetSection("system.serviceModel/Client") as ServiceModelSectionGroup; foreach (ChannelEndpointElement endpoint in config.Client.Endpoints) { Uri address = endpoint.Address; if (address != null) { return new EndpointAddress(address); } } return null; } } 

Here is what I tried using BasicHttpBinding

 private BasicHttpBinding Binding { get { var config = ConfigurationManager.GetSection("system.serviceModel/Client") as ServiceModelSectionGroup; foreach (ChannelEndpointElement bindings in config.Bindings.BasicHttpBinding) { string binding = config.Bindings.BasicHttpBinding; if (config.Bindings.BasicHttpBinding != null) { return new BasicHttpBinding(config.Bindings.BasicHttpBinding); } } return null; } 

However, I get the error message:

"foreach statement cannot work with variables of type 'System.ServiceModel.Configuration.BasicHttpBindingCollectionElement' because 'System.ServiceModel.Configuration.BasicHttpBindingCollectionElement' does not contain a public definition for" GetEnumerator "

I tried replacing ChannelEndpointElement with BasicHttpBindingElement, but that does not help. I don’t understand how to do it right, so I get the same effect as endpointAddress.

Edit: I thought I had it fixed, but all I did was get rid of the errors. When I try to debug a program, I get

System.NullReferenceException: object reference not set to object instance

in the following paragraph:

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

however, despite the fact that it fails on this line, I noticed that the line above it:

 var config = ConfigurationManager.GetSection("system.serviceModel") as ServiceModelSectionGroup; 

also is null. I am not sure what I am doing wrong.

+4
source share

All Articles