无法从IE访问WCF服务

时间:2011-07-29 20:48:28

标签: wcf rest

这可能已经讨论过,但是我在这个主题上看到的所有主题都没有帮助我,因此我发帖了。

我正在尝试在IIS 5.1上托管WCF HTTP服务。这是非常基本的。我在IE中创建了一个名为wcftest的虚拟目录,它指向包含服务内容的实际文件夹。结构如下:

Web.config
ConsoleWCF.svc
[App_Code] \ Program.cs

以下是ConsoleWCF.svc

的代码
<%@ ServiceHost Debug="true" Service="ConsoleWCF.WCFImplementer" Language="C#" %>

Web.config看起来像这样:

<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="ConsoleWCF.WCFImplementer" 
               behaviorConfiguration="ServiceBehavior">
        <endpoint 
            address="" 
            binding="basicHttpBinding" 
            contract="ConsoleWCF.WCFInterface" />
        <endpoint 
            address="mex" 
            binding="mexHttpBinding" 
            contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

最后Program.cs是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Description;

namespace ConsoleWCF
{
    [ServiceContract]
    public interface WCFInterface
    {
        [OperationContract]
        [WebGet]
        string GetData();
    }

    public class WCFImplementer : WCFInterface
    {
        public string GetData()
        {
            return "Tested Console WCF";
        }
    }
}

我可以通过浏览http://localhost/wcftest/ConsoleWCF.svc在IE中访问此内容。

但是我不明白如何访问我所拥有的GetData方法。当我使用独立主机执行此操作时,我没有遇到任何问题。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

为了让WCF理解[WebGet]属性,端点需要具有某种配置,即使用webHttpBinding(而不是basicHttpBinding)并拥有{ {1}}已添加到其中。如果您将WebHttpBehavior更改为类似下面的内容,则应该可以浏览到web.config

http://localhost/wcftest/ConsoleWCF.svc/GetData
相关问题