【问题标题】:How to access WCF service methods from browser?如何从浏览器访问 WCF 服务方法?
【发布时间】:2014-08-14 17:59:04
【问题描述】:

这是默认向导在 Visual Studio 2013 中创建的代码。 在项目属性中,我设置为使用本地 IIS。 WCF 测试客户端测试成功。 但是如果我访问页面

http://localhost/WcfService1/Service1.svc/GetTime

在浏览器中,我看到空的浏览器屏幕,Fiddler 显示“HTTP/1.1 400 Bad Request”。 我知道我需要修改 web.config,以及接口中方法的属性,但不知道如何。你可以帮帮我吗?谢谢。

IService1.cs

using System.Runtime.Serialization;
using System.ServiceModel;
namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetTime();
    }
}

Service1.svc.cs

using System;
namespace WcfService1
{
    public class Service1 : IService1
    {
        public string GetTime()
        {
            return DateTime.Now.ToShortTimeString();
        }
    }
}

web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

【问题讨论】:

  • 你需要在 web.config 中描述端点
  • 你有一个 SOAP 服务。您正试图像 REST 服务一样访问它,但您不能这样做。您需要代理才能使用 SOAP 服务 - 您不能直接在浏览器中调用方法。

标签: wcf browser


【解决方案1】:

感谢 sakir 和 Tim,你们这些 cmets 允许我在适当的地方寻找答案。是的,这是 SOAP 服务,所以我需要重新配置它才能通过 web http 访问。

  1. 我在 web.config 中添加了允许 HttpBinding 的“行为”部分和此行为的配置
  2. 我将属性 [WebGet] 添加到要在浏览器中显示的方法中。

注意:我们可以在 Visual Studio 中得到类似的结果,如果我们将通过添加空项目“WCF 服务(启用 Ajax)”模板来创建 WCF Web 服务(我在 VS2013 中很难找到,但它在top 在 VS2012 中作为一个单独的项目)。这将为我们修改 web.config,并给出工作代码示例(但不是基于接口)。

修改文件并重新配置web.config如下:

IService1.cs

using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet]
        string GetTime();
    }
}

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

      **<!--added behavior-->
      <endpointBehaviors>
        <behavior name="WcfService1.Service1AspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>**

    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    **<!--added service behaviour configuration-->
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="" behaviorConfiguration="WcfService1.Service1AspNetAjaxBehavior"
          binding="webHttpBinding" contract="WcfService1.IService1" />
      </service>
    </services>**

  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2021-10-12
    • 2017-12-01
    • 2017-07-21
    • 2017-07-12
    • 2014-11-21
    • 2023-03-11
    相关资源
    最近更新 更多