找不到名称为“Address:http://example.com:8123/blmrg/test_ws/Service1.svc”的端点元素

时间:2014-12-22 05:59:10

标签: c# wcf wcf-configuration

我使用svcutil.exe为Web服务创建了一个代理类。我试图使用下面的代码连接到webservice。发生以下异常

  

无法找到名称为'地址:http://example.com:8123/blmrg/test_ws/Service1.svc的端点元素;和合同'IService1'在ServiceModel客户端配置部分"。这可能是因为找不到您的应用程序的配置文件。

当我尝试在浏览器中访问该webservice网址时,其工作正常。请告诉我下面的代码出了什么问题。

我的代码:

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>

代理类:

[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 个答案:

答案 0 :(得分:0)

您正在使用的Service1Client的重载(或编译器认为您正在使用的)是获取端点配置名称的重载:

public Service1Client(string endpointConfigurationName) : 
    base(endpointConfigurationName)

但你传入的是Address:http://132.158.6.6:8123/blmrg/test_ws/Service1.svc; and contract 'IService1',显然是因为这条线(尽管它缺少了绑定,并且没有#34;和#34这个词;在其中):

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

没有必要(基于发布的代码)使用ConfigurationManager来获取配置文件的客户端部分。您可以在构造函数中传入端点部分的名称,如下所示:

Service1Client proxy = new Service1Client("BasicHttpBinding_IService1");

以上行将从端点提取必要信息,名称为&#34; BasicHttpBinding_IService1&#34;。

注意构造函数还有一些其他重载:

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

这将采用端点配置名称和服务的地址(通常位于端点元素中,但您可能希望根据环境或不支持来覆盖它)。

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

与前一个相似,但传递的是EndpointAddress,而不是string

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

这张传递了BindingEndpointAddress