【问题标题】:Unable to call WCF method with browser无法使用浏览器调用 WCF 方法
【发布时间】:2017-01-20 02:38:33
【问题描述】:

我开发了一个 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复制了大部分东西。

【问题讨论】:

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


    【解决方案1】:

    它对我来说没有问题。

    使用此网址:

    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 地址。

    【讨论】:

    • 在我的情况下,我的 URL 是什么?您正在使用“WcfService1”这是什么?您的示例可以使用与我相同的命名空间和类名吗?
    • 请看我最后的评论。
    • 当我浏览 .SVC 文件时,它在浏览器上打开为localhost:6000/CustomService.svc。它始终是“localhost”,即使我在 Visual Studio 上运行“开始调试”。
    • 好的,请注意,此基地址与您在问题中发布的基地址不匹配。
    • /testservice还是/CustomerService.svc
    【解决方案2】:

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

    【讨论】:

    • 我的开发笔记本电脑上没有 IIS。我将它部署到一个带有 IIS 的服务器上,而在那个服务器上,没有 Visual Studio。
    • 您可以使用 IIS express。附带视觉工作室。当您的项目为“设置为启动项目”时,没有启动或运行选项?
    • 我可以从我的开发笔记本电脑上运行“开始调试”。浏览器将打开localhost:57371,其中显示了我的开发文件的目录。那么接下来呢?当我转到 localhost:57371/testservice/date/12/1/1 时,它也显示 HTTP 404。
    • 取localhost:57371/whateverthepathistoyourservice.svc并放入wcf客户端
    • 我运行了 WCFTestClient,然后在文件 -> 添加服务中,我输入了我的服务 localhost:57371/CustomService.svc。那么接下来呢?在程序左窗格中,它只是显示“我的服务项目”,而在右窗格中,它是“起始页”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    相关资源
    最近更新 更多