使用.net api 2.0 /端点配置的WCF服务失败

时间:2017-05-16 07:04:21

标签: c# asp.net .net web-services wcf

将基本WCF服务器添加为服务引用后,我尝试运行它并收到错误:

  

System.InvalidOperationException:端点   合同的配置部分' AlineTest.ServicesSoap'不能   因为有多个端点配置而被加载   合同被发现。请指出首选端点   按名称配置部分。在   System.ServiceModel.Description.ConfigLoader.LookupChannel(ContextInformation   configurationContext,String configurationName,ContractDescription   contract,EndpointAddress地址,布尔通配符,布尔值   useChannelElementKind,ServiceEndpoint& serviceEndpoint)at   System.ServiceModel.ChannelFactory.InitializeEndpoint(字符串   configurationName,EndpointAddress地址)at   System.ServiceModel.ChannelFactory 1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress) at System.ServiceModel.ConfigurationEndpointTrait 1.CreateSimplexFactory()   在   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年\项目\ ComboProjectReference \ ComboProjectReference \连接   Services \ AlineTest \ Reference.cs:2898行   ComboProjectReference.Controllers.ValuesController.d__0.MoveNext()   在c:\ users \ nguyen ba nguyen \ documents \ visual studio   2017年\项目\ ComboProjectReference \ ComboProjectReference \ \控制器ValuesController.cs:行   28

我使用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());
        }

好像我需要在

行配置/添加端点配置
  

ServicesSoapClient client = new ServicesSoapClient();

但我无法找到连接的服务保存端点配置的位置。 WCF reference

所以欢迎任何有关配置端点的帮助:D

1 个答案:

答案 0 :(得分:0)

找出web.config文件中列出的字符串endpointConfigurationName。 web.config中的字符串endpointConfigurationName看起来像

<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>

所以我添加了字符串&#34; ServicesSoap&#34;到我的ServicesSoapClient客户端= new ServicesSoapClient();它的工作

  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());
        }