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

时间:2017-01-20 02:38:33

标签: c# web-services wcf wcf-binding

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

使用浏览器,当我转到http://localhost:6000/CustomService.svc时,会显示 "您已经创建了一项服务"和其他信息。这意味着服务已成功部署。

但是当我转到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); 
        }
    }

这是实现的类:

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复制了大部分内容。

2 个答案:

答案 0 :(得分:1)

对我来说没问题。

enter image description here

使用此网址:

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右键单击.SVC文件,选择浏览并确保配置文件中有正确的基址。您的基地址看起来更像是IIS Express地址。

答案 1 :(得分:0)

尝试使用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