如何正确配置WCF REST服务端点?

时间:2012-08-14 14:13:45

标签: c# wcf rest service endpoint

我打算根据不同的合同托管多个RESTful服务。有很多类似的问题,但我的web.config文件看起来不一样,我不知道为什么。

以下是我的web.config文件的一部分:

    <standardEndpoints>
          <webHttpEndpoint>
            <standardEndpoint name="" 
                              helpEnabled="true" 
                              automaticFormatSelectionEnabled="true">
            </standardEndpoint>
          </webHttpEndpoint>
    </standardEndpoints>

这是我的网络应用程序中的服务声明:

    RouteTable.Routes.Add(new ServiceRoute("bob/chocolate", new WebServiceHostFactory(), typeof(RESTchocolate)));
    RouteTable.Routes.Add(new ServiceRoute("bob/vanilla", new WebServiceHostFactory(), typeof(RESTvanilla)));

只有第一条路线似乎正在工作(使用.NET的漂亮的“bob / chocolate / help”端点功能测试列出可用的方法)这实际上并不令我感到惊讶,但我应该如何修改我的web.config档案?你们有谁知道怎么做吗?我需要修改其他内容吗?

对于那些想知道的人,我的合同是有效的。

如果我尝试在浏览器中访问第二个端点,我会在一个漂亮的.NET显示中找到“Endpoint not found”。

编辑:

我将以下节点添加到配置文件...

    <services>
      <service name="chocolate">
        <endpoint address="bob/chocolate" binding="basicHttpBinding" name="chocolate" contract="RESTapi.IRESTchocolate" />
      </service>
      <service name="vanilla">
        <endpoint address="bob/vanilla" binding="basicHttpBinding" name="vanilla" contract="RESTapi.IRESTvanilla" />
      </service>
    </services>

但我得到了同样的行为。问题仍然存在

编辑:这是我提供的完整配置文件(没有上面的节点):

    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <authentication mode="None"></authentication>
      </system.web>
      <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
        </serviceHostingEnvironment>
         <standardEndpoints>
          <webHttpEndpoint>
            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"></standardEndpoint>
          </webHttpEndpoint>
        </standardEndpoints>
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>

    </configuration>

1 个答案:

答案 0 :(得分:1)

我真的建议您安装WCF REST Service Template 40并查看引导程序。

的Web.config

<standardEndpoints>
  <webHttpEndpoint>
    <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
    -->
    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
  </webHttpEndpoint>
</standardEndpoints>

Global.asax中

// Edit the base address of Service1 by replacing the "Service1" string below
RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
相关问题