c# Soap 客户端问题 - 发现合约的多个端点配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17545175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:48:19  来源:igfitidea点击:

c# Soap Client Issue - more than one endpoint configuration for th at contract was found

c#soap

提问by Latheesan

I am trying to write a simple c# console application to test the SOAP API from here: https://www.imailtest.co.uk/webservice/imail_api.asmx?wsdl(or https://www.imailtest.co.uk/webservice/imail_api.asmxto see the api methods)

我正在尝试编写一个简单的 c# 控制台应用程序来测试这里的 SOAP API:https: //www.imailtest.co.uk/webservice/imail_api.asmx?wsdl(或https://www.imailtest.co.uk /webservice/imail_api.asmx查看api方法)

So, I added this reference and tried to invoke 2 api methods (Authentiacate & ProcessPrintReadyPDF) calls on it and got this error:

所以,我添加了这个引用并尝试调用 2 个 api 方法(Authentiacate & ProcessPrintReadyPDF)调用它并得到这个错误:

Error : An endpoint configuration section for contract 'ServiceReference1.imail_ apiSoap' could not be loaded because more than one endpoint configuration for th at contract was found. Please indicate the preferred endpoint configuration sect ion by name.

错误:无法加载合约“ServiceReference1.imail_apiSoap”的端点配置部分,因为找到了多个合约的端点配置。请按名称指明首选端点配置部分。

Here's my C# Code:

这是我的 C# 代码:

static void Main(string[] args)
{
    // Anticipate Error
    try
    {
        // Generate SOAP Client
        ServiceReference1.imail_apiSoapClient soapClient = new ServiceReference1.imail_apiSoapClient();

        // Login
        Console.WriteLine("Authenticating");
        soapClient.Authenticate(iMailUser, iMailPass);

        // Proceed If PDF File Exists
        if (File.Exists(PDFFile))
        {
            // Upload PDF File To iMail
            Console.WriteLine("Uploading PDF File");
            soapClient.ProcessPrintReadyPDF(File.ReadAllBytes(PDFFile), "", true);

            // Test Complete
            Console.WriteLine("Done");
        }
        else
        {
            // Log Error
            Console.WriteLine("PDF File [{0}] Does Not Exists", PDFFile);
        }
    }
    catch (Exception ex)
    {
        // Log Error
        Console.WriteLine("Error : "+ ex.Message);
    }

    // End Test
    Console.WriteLine("Press any key to continue ...");
    Console.ReadKey();
}

This is how I added the service reference to my console app:

这是我将服务引用添加到我的控制台应用程序的方式:

screenshot

截屏

Any ideas?

有任何想法吗?

采纳答案by Latheesan

I believe the problem is solved via defining the contract name like so (based on my screenshot):

我相信问题是通过像这样定义合同名称来解决的(基于我的截图):

ServiceReference1.imail_apiSoapClient soapClient = 
new ServiceReference1.imail_apiSoapClient("imail_apiSoap");

Now, I am no longer getting an error and the api appears to be working.

现在,我不再收到错误消息,而且 api 似乎正在运行。

回答by sudil ravindran pk

In your App.config you can see some thing like this

在你的 App.config 你可以看到这样的东西

 <client>
      <endpoint address="https://www.imailtest.co.uk/webservice/imail_api.asmx "
        binding="basicHttpBinding" bindingConfiguration="xxxxxxxxxx"
        contract="xxxxxxxxxx" name="xxxxxxxxxxxxx" />
      <endpoint address="https://www.imailtest.co.uk/webservice/imail_api.asmx"
        binding="customBinding" bindingConfiguration="xxxxxxxxxxxxx"
        contract="xxxxxxxxxxx" name="xxxxxxxxxxxxx" />
  </client>

remove the second endpoint and now it should be like this

删除第二个端点,现在应该是这样的

<client>
      <endpoint address="https://www.imailtest.co.uk/webservice/imail_api.asmx "
        binding="basicHttpBinding" bindingConfiguration="xxxxxxxxxxxxx"
        contract="xxxxxxxxxxxxxx" name="xxxxxxxxxxxxxxx" />      
  </client>

now run the code , hope your code works fine

现在运行代码,希望你的代码工作正常

回答by Larry

If you want to keep both client configurations in your config file, just create an application Setting.

如果您想在配置文件中保留两个客户端配置,只需创建一个应用程序设置。

So your App.config file will contains this entry that will allow you to specify the endpoint you want:

因此,您的 App.config 文件将包含此条目,该条目将允许您指定所需的端点:

<setting name="EndPoint" serializeAs="String">
    <value>imail_apiSoap</value>
</setting>

So you can use in your code :

所以你可以在你的代码中使用:

ServiceReference1.imail_apiSoapClient soapClient =
    new ServiceReference1.imail_apiSoapClient(Properties.Settings.Default.EndPoint);

回答by Abdul Khaliq

[Solved! just add the End point in the webservice's proxy class asp below screen shot

[解决了!只需在屏幕截图下方的 webservice 代理类 asp 中添加端点

enter image description here

在此处输入图片说明