簡體   English   中英

找不到名稱為'Address:http://example.com:8123 / blmrg / test_ws / Service1.svc的終結點元素

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

我已經使用svcutil.exe為Web服務創建了代理類。 我試圖使用以下代碼連接到Web服務。 發生以下異常

找不到名稱為'Address: http : //example.com : 8123/blmrg/test_ws/Service1.svc的終結點元素; 並在ServiceModel客戶端配置部分中與合同'IService1'簽訂合同。”這可能是因為未為您的應用程序找到配置文件。

當我嘗試在瀏覽器中訪問該Web服務URL時,其工作正常。 請讓我知道以下代碼出了什么問題。

我的代碼:

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

您正在使用(或編譯器認為您正在使用)的Service1Client的重載是采用端點配置名稱的重載:

public Service1Client(string endpointConfigurationName) : 
    base(endpointConfigurationName)

但是您要傳遞的是Address:http://132.158.6.6:8123/blmrg/test_ws/Service1.svc; and contract 'IService1' Address:http://132.158.6.6:8123/blmrg/test_ws/Service1.svc; and contract 'IService1' ,顯然是因為這一行(盡管它缺少綁定並且其中沒有單詞“ and”):

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)

這一個傳入BindingEndpointAddress

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM