从c#服务器端调用asmx:可以在client元素中找到匹配此契约的端点元素

Ela*_*nda 20 c# soap web-services visual-studio-2010

我在srv1上写了一个asmx webSerivce.我在srv2上写了一个asp.net(原文:一个asp.net)项目的bll项目.两者都托管在同一个Web域下

我想从asp.netbll项目中调用asmx (原文:asp.net(c#)代码).

1)我添加了一个Web引用,但找不到任何教程如何真正调用引用的服务.

我试过了:

private void GetTemplateComponentsData()
{
    var service = new ServiceReference.GetTemplateParamSoapClient();
    TemplateParamsKeyValue[] responsArray = service.GetTemplatesParamsPerId(id);

    foreach (var pair in responsArray)
    {
        TemplateComponentsData.Add(pair.Key, pair.Value);
    }
}
Run Code Online (Sandbox Code Playgroud)

但在执行第一行时出现以下错误: 无法在ServiceModel客户端配置部分中找到引用合同"ServiceReference.GetTemplateParamSoap"的默认端点元素.这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素.

我错过了什么?

2)我将asp.net proj和asmx从一个域迁移到另一个域.有没有办法相对引用这个webservice?

Dar*_*rov 40

好吧,让我试着改写你的场景,以确保我做对了:

  1. 您在某个域上托管了ASMX Web服务.
  2. 您有一个ASP.NET应用程序托管在相同或不同的域(它并不重要),您希望使用WCF客户端(svcutil)从中使用此ASMX Web服务.

第一步是通过指向ASMX服务的WSDL向ASP.NET应用程序添加服务引用:

在此输入图像描述

这将做两件事:

  • 它将为您的Web应用程序添加ServiceReference

在此输入图像描述

  • 它将修改您的web.config并包含客户端端点:

    <client>
      <endpoint address="http://ws.cdyne.com/NotifyWS/phonenotify.asmx"
        binding="basicHttpBinding" bindingConfiguration="PhoneNotifySoap"
        contract="ServiceReference1.PhoneNotifySoap" name="PhoneNotifySoap" />
      <endpoint address="http://ws.cdyne.com/NotifyWS/phonenotify.asmx"
        binding="customBinding" bindingConfiguration="PhoneNotifySoap12"
        contract="ServiceReference1.PhoneNotifySoap" name="PhoneNotifySoap12" />
    </client>
    
    Run Code Online (Sandbox Code Playgroud)

现在,当您想从应用程序调用此服务时,您必须选择要使用的端点:

using (var client = new ServiceReference1.PhoneNotifySoapClient("PhoneNotifySoap"))
{
    var result = client.GetVersion();
}
Run Code Online (Sandbox Code Playgroud)

现在只需用我的实际服务名称替换我的代码片段.

  • @Elad Benda,是的,您确实需要在使用此库的ASP.NET应用程序的web.config中使用客户端端点.您还可以在BLL中以编程方式配置端点,但更好的做法是使用配置部分,因为它可以让您更轻松地更改设置,如网址,绑定,... (2认同)