无法使用浏览器调用 WCF 方法

Unable to call WCF method with browser

我开发了一个 WCF 服务应用程序并将其部署到 IIS 8。

使用浏览器,当我转到 http://localhost:6000/CustomService.svc 时,它显示 "You have created a service" 等信息。这意味着服务已成功部署。

但是当我转到 http://localhost:6000/testservice/date/2016/12/1 时,它显示 HTTP 404 Not Found。

这是服务合同:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.ServiceModel.Web;

    namespace WCF
    {
        [ServiceContract]
        public interface ICustomService
        {
            [WebGet(UriTemplate = "date/{year}/{month}/{day}", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            string GetDate(string day, string month, string year); 
        }
    }

这里实现了class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCF
{
    public class CustomService : ICustomService
    {
        public string GetDate(string day, string month, string year)
        {
            return new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day)).ToString("dddd, MMMM dd, yyyy");
        }
    }
}

这里是Web.config:

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="CustomServiceBehavior" name="WCF.CustomService">
        <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="WCF.ICustomService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6000/testservice" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CustomServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

问题出在哪里?我基本上从 https://weblogs.asp.net/kiyoshi/wcf-using-webhttpbinding-for-rest-services.

复制了大部分内容

尝试使用内置于 Visual Studio 中的 WCF 测试客户端。它位于:C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\WcfTestClient.exe。打开测试客户端,添加服务并进行调用。

您可以使用 fiddler 或其他一些网络请求捕获工具来查看您的服务正在请求什么 URL。希望这能让您进一步排除故障。

这是 WCF 测试客户端的 MSDN。 https://msdn.microsoft.com/en-us/library/bb552364(v=vs.110).aspx

对我来说没问题。

使用这个 URL:

http://localhost/WcfService1/Service1.svc/date/2017/1/31

和这个配置文件:

 <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="NewBinding0">
          <security>
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="" behaviorConfiguration="NewBehavior0" binding="webHttpBinding"
          bindingConfiguration="" contract="WcfService1.IService1" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="NewBehavior0">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <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>

转到 IIS Manager right-click 您的 .SVC 文件,select 浏览并确保您的配置文件中有正确的基地址。您的基址看起来更像是一个 IIS Express 地址。