问题
I'm trying to figure out how to read how to read basicHttpBinding section defined in app.config file. I attempted to do it like I did the endpoint address but it does not work. Here is how I did 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 have tried with the 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 recieve an error that states:
"foreach statement cannot operate on variables of type 'System.ServiceModel.Configuration.BasicHttpBindingCollectionElement' because 'System.ServiceModel.Configuration.BasicHttpBindingCollectionElement' does not contain a public definition for 'GetEnumerator'"
I've tried replacing ChannelEndpointElement with BasicHttpBindingElement but that doesn't help. I'm at a loss how to do this correctly so that I get the same effect as the endpointAddress.
Edit: I thought I had this fixed but all I did was get rid of the errors. When I try to debug the program, I receive a
System.NullReferenceException: Object reference not set to an instance of an object
at the following point:
foreach (ChannelEndpointElement bindings in config.Bindings.BasicHttpBinding.Bindings)
however, even though it fails on this line, I noticed that the line above it:
var config = ConfigurationManager.GetSection("system.serviceModel") as ServiceModelSectionGroup;
is null as well. I'm not sure what it is I am doing wrong.
来源:https://stackoverflow.com/questions/11451722/how-to-read-basichttpbinding-defined-in-app-config-file