【问题标题】:Unable to consume WCF services from my .net MVC application无法从我的 .net MVC 应用程序使用 WCF 服务
【发布时间】:2019-11-12 14:41:20
【问题描述】:

尝试在新创建的 .net (MVC) 应用程序中使用 WCF Web 服务。

我已经完成了以下步骤。 1.创建了一个新的mvc应用程序。 2.要添加服务参考。 3. 粘贴网址“http://api.tektravels.com/BookingEngineService_AirBook/AirService.svc” 4. 出错。 下载“http://api.tektravels.com/BookingEngineService_AirBook/AirService.svc/$metadata”时出错。 请求失败,HTTP 状态为 404:未找到。

http://api.tektravels.com/BookingEngineService_AirBook/AirService.svc

需要使用网络服务。任何帮助将不胜感激。

【问题讨论】:

标签: .net web-services wcf


【解决方案1】:

对于没有共享Web服务元数据的WCF服务,我们只有在知道服务契约后才能使用服务,如下表所示。

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string SayHello();
}

然后我们使用 ChannelFactory 调用服务。

  BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.None;
            Uri uri = new Uri("http://serviceaddress:8008");
            ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
            IService service = factory.CreateChannel();
            var result = service.SayHello();
            Console.WriteLine(result);

您尝试使用的上述服务尚未发布服务元数据。因此,要么我们更改服务器设置以启用服务元数据,要么我们必须知道服务合同,才能成功调用。
关于如何启用服务元数据,取决于发布服务的方式。 这里是服务端常用的配置,我们需要额外配置一个 MEX 端点。

<system.serviceModel>
    <services>
      <service name="WcfService3.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="WcfService3.IService1"></endpoint>
        <!--Add an extra endpoint to exchange the service metadata-->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

如果问题仍然存在,请随时告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 2023-03-31
    • 2014-08-23
    • 2023-03-05
    • 1970-01-01
    • 2015-03-12
    相关资源
    最近更新 更多