小编Del*_*ity的帖子

如何从浏览器访问WCF服务方法?

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

http://localhost/WcfService1/Service1.svc/GetTime
Run Code Online (Sandbox Code Playgroud)

在浏览器中,我看到空的浏览器屏幕和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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Service1.svc.cs

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

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" …
Run Code Online (Sandbox Code Playgroud)

browser wcf

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

browser ×1

wcf ×1