简体   繁体   中英

Consuming WCF service with .net api 2.0 / endpoint configuration failed

After add an Basic WCF server as Service Reference ,i try to run it and got error :

System.InvalidOperationException: An endpoint configuration section for contract 'AlineTest.ServicesSoap' could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name. at System.ServiceModel.Description.ConfigLoader.LookupChannel(ContextInformation configurationContext, String configurationName, ContractDescription contract, EndpointAddress address, Boolean wildcard, Boolean useChannelElementKind, ServiceEndpoint& serviceEndpoint) at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address) at System.ServiceModel.ChannelFactory 1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress) at System.ServiceModel.ConfigurationEndpointTrait 1.CreateSimplexFactory() at System.ServiceModel.ConfigurationEndpointTrait 1.CreateChannelFactory() at System.ServiceModel.ClientBase 1.CreateChannelFactoryRef(EndpointTrait 1 endpointTrait) at System.ServiceModel.ClientBase 1.InitializeChannelFactoryRef() at System.ServiceModel.ClientBase`1..ctor() at ComboProjectReference.AlineTest.ServicesSoapClient..ctor() in c:\\users\\nguyen ba nguyen\\documents\\visual studio 2017\\Projects\\ComboProjectReference\\ComboProject Reference\\Connected Services\\AlineTest\\Reference.cs:line 2898 at ComboProjectReference.Controllers.ValuesController.d__0.MoveNext() in c:\\users\\nguyen ba nguyen\\documents\\visual studio 2017\\Projects\\ComboProjectReference\\ComboProjectReference\\Controllers\\ValuesController.cs:line 28

my code for consuming WCF :

  try { ServicesSoapClient client = new ServicesSoapClient(); Task<CustomerAccountInfo[]> GetAccount = client.GetAccountAsync(User, Password, DemoAccount); CustomerAccountInfo[] AccountInfor = await GetAccount; foreach(CustomerAccountInfo a in AccountInfor) { AccountView.Add(a); } } catch (Exception ex) { AccountView.Error = ex.ToString(); return BadRequest(ex.ToString()); } 

It seem like i need to config/ add endpoint configuration at the line

ServicesSoapClient client = new ServicesSoapClient();

but i can't find where the connected services save the endpoint configuration . WCF reference

So any help to config endpoint is welcome :D

Find out the string endpointConfigurationName listed on web.config file. My string endpointConfigurationName in web.config is look like

<client>
      <endpoint address="https://***" binding="basicHttpBinding" bindingConfiguration="ServicesSoap" contract="AlineTest.ServicesSoap" name="ServicesSoap" />
      <endpoint address="https://***" binding="customBinding" bindingConfiguration="ServicesSoap12" contract="AlineTest.ServicesSoap" name="ServicesSoap12" />
      <endpoint address="https://***" binding="basicHttpBinding" bindingConfiguration="SubscriberManagementSoap" contract="Mytv.SubscriberManagementSoap" name="SubscriberManagementSoap" />
      <endpoint address="https://***" binding="customBinding" bindingConfiguration="SubscriberManagementSoap12" contract="Mytv.SubscriberManagementSoap" name="SubscriberManagementSoap12" />
    </client>

so i added string "ServicesSoap" to my ServicesSoapClient client = new ServicesSoapClient(); and it work

  try
        {
            ServicesSoapClient client = new ServicesSoapClient("ServicesSoap");
            Task<CustomerAccountInfo[]> GetAccount = client.GetAccountAsync(User, Password, DemoAccount);
            CustomerAccountInfo[] AccountInfor = await GetAccount;
            foreach(CustomerAccountInfo a in AccountInfor)
            {
                AccountView.Add(a);
            }
        }
        catch (Exception ex)
        {
            AccountView.Error = ex.ToString();
            return BadRequest(ex.ToString());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM