Could not find endpoint element with name 'Address:http://example.com:8123/blmrg/test_ws/Service1.svc

1.1k views Asked by At

I have created a proxy class for a web service using svcutil.exe. I was trying to connect to webservice using below code. Below exception occurred

Could not find endpoint element with name 'Address:http://example.com:8123/blmrg/test_ws/Service1.svc; and contract 'IService1' in the ServiceModel client configuration section". This might be because no configuration file was found for your application.

When I tried to access that webservice url in browser, its working fine. Please let me know what went wrong in below code.

My code:

ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
ChannelEndpointElement endpoint = clientSection.Endpoints[0];
string endpointStr = string.Format("Address: {0}; Binding: {1}; Contract: {2}", endpoint.Address.ToString(), endpoint.Binding, endpoint.Contract);
Service1Client proxy = new Service1Client(endpointStr); // Getting exception here
CitizenType citizen = proxy.GetMan(562054);

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>  
<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService1" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://example.com:8123'/blmrg/test_ws/Service1.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
      contract="IService1" name="BasicHttpBinding_IService1" />
</client>
 </system.serviceModel>
</configuration>

Proxy class:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1
{

    public Service1Client()
    {
    }

    public Service1Client(string endpointConfigurationName) : 
            base(endpointConfigurationName)
    {

    }

    public Service1Client(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
    {
    }

    public CitizenType GetMan(decimal id)
    {
        return base.Channel.GetMan(id);
    }

    public System.Threading.Tasks.Task<CitizenType> GetManAsync(decimal id)
    {
        return base.Channel.GetManAsync(id);
    }
}
1

There are 1 answers

1
Tim On BEST ANSWER

The overload for Service1Client you're using (or the compiler thinks you're using) is the one that takes an endpoint configuration name:

public Service1Client(string endpointConfigurationName) : 
    base(endpointConfigurationName)

But what you're passing in is Address:http://132.158.6.6:8123/blmrg/test_ws/Service1.svc; and contract 'IService1', apparently because of this line (though it's missing the binding and doesn't have the word "and" in it):

string endpointStr = string.Format("Address: {0}; Binding: {1}; Contract: {2}", endpoint.Address.ToString(), endpoint.Binding, endpoint.Contract);

There's no need (based on the posted code) to be using ConfigurationManager to get the client section of the config file. You can pass in the name of the endpoint section in the constructor, like this:

Service1Client proxy = new Service1Client("BasicHttpBinding_IService1");

The above line will pull the necessary information from the endpoint with the name "BasicHttpBinding_IService1".

Note there are some other overloads for the constructor as well:

public Service1Client(string endpointConfigurationName, string remoteAddress) : 
    base(endpointConfigurationName, remoteAddress)

This will take an endpoint configuration name and the address for the service (which is usually in the endpoint element, but perhaps you want to override it based on environments or what not).

public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
    base(endpointConfigurationName, remoteAddress)

Similar to the previous one, but passes in an EndpointAddress as opposed to a string.

public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
    base(binding, remoteAddress)

This one passes in a Binding and an EndpointAddress.