【问题标题】:An exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll but was not handled in user codeSystem.ServiceModel.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理
【发布时间】:2018-10-25 05:01:31
【问题描述】:

我有两个名为 AB 的项目。现在,我在 B 项目中有一个服务类,在服务类中,我有一个如下端点:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class ReportAPIService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function

End Class

现在,我想从另一个 WebAPI 项目调用这个端点,它是 A。因此,为了实现这一点,我首先在我的环境中本地运行了 B 项目。然后,在我的A项目中添加了ReportAPIService的服务引用,添加成功。我给的服务地址如下:

http://localhost:4043/ReportAPIService/ReportAPIService.asmx

然后,我添加了以下代码来调用项目 A 中的端点。

ReportAPIServiceSoapClient soapClient = new ReportAPIServiceSoapClient();        
return soapClient.HelloWorld();

但是,在尝试初始化 ReportAPIServiceSoapClient() 时,它给了我以下错误:

“System.InvalidOperationException”类型的异常发生在 System.ServiceModel.dll 但未在用户代码中处理

附加信息:找不到默认端点元素 引用合同中的“ReportService.ReportAPIServiceSoap” ServiceModel 客户端配置部分。这可能是因为没有 为您的应用程序找到了配置文件,或者因为没有 可以在客户端中找到与此合同匹配的端点元素 元素。

我应该在客户端配置部分添加什么?

然后,我尝试使用HTTPClient,如下所示:

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:4043/ReportAPIService/ReportAPIService.asmx/");
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response = await client.GetAsync("HelloWorld");
return await response.Content.ReadAsStringAsync();

但是,卡在GetAsync 方法上。服务器没有响应。

WCF 服务中的 web.config:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="ReportAPIServiceSoap" />
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint address="http://localhost:4043/ReportAPIService/ReportAPIService.asmx"
 binding="basicHttpBinding" bindingConfiguration="ReportAPIServiceSoap"
 contract="ReportService.ReportAPIServiceSoap" name="ReportAPIServiceSoap" />
  </client>
 </system.serviceModel>

在 ASP.NET 中有没有其他方法可以从另一个项目调用一个项目的端点?

【问题讨论】:

  • 错误很清楚,因为它说没有定义匹配的端点。发布您的 WCF 服务的 web.config 文件
  • 更新了帖子

标签: c# asp.net vb.net web-services


【解决方案1】:

您的配置在我看来是错误的。一个简化的如下所示。更多信息请见Documentation

<system.serviceModel>
    <services>  
      <service name="<Provide Fully qualified service name>">  
            <endpoint address="http://localhost:4043/ReportAPIService/ReportAPIService.asmx"
                   binding="basicHttpBinding" contract="ReportService.ReportAPIServiceSoap"/>  
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>  
      </service>  
    </services> 
 </system.serviceModel>

【讨论】:

  • 什么是完全限定的服务名称?我没有得到这部分。
  • 我已经添加了这个,但仍然遇到同样的错误。
猜你喜欢
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多