无法从我的.net MVC应用程序使用WCF服务

尝试在新创建的.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

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

jianjunliqi 回答:无法从我的.net MVC应用程序使用WCF服务

对于不共享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>

请随时让我知道问题是否仍然存在。

本文链接:https://www.f2er.com/3116606.html

大家都在问