找不到名称为"地址:http://example.com:8123/blmrg/test_ws/Service1.svc 的终结点元素



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

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

当我尝试在浏览器中访问该网络服务网址时,它工作正常。请让我知道下面的代码出了什么问题。

我的代码:

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

应用配置

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

你正在使用的Service1Client(或编译器认为你正在使用的(的重载是采用终结点配置名称的重载:

public Service1Client(string endpointConfigurationName) : 
    base(endpointConfigurationName)

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

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

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

Service1Client proxy = new Service1Client("BasicHttpBinding_IService1");

上面的行将从名为"BasicHttpBinding_IService1"的端点中提取必要的信息。

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

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)

这个通过一个Binding和一个EndpointAddress.

最新更新