【问题标题】:How to correctly configure a WCF REST service endpoint?如何正确配置 WCF REST 服务端点?
【发布时间】:2012-08-14 14:13:45
【问题描述】:

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

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

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

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

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

似乎只有第一个 Route 有效(使用 .NET 的“bob/chocolate/help”端点功能进行测试以列出可用的方法)这并不让我感到惊讶,但是我应该如何修改我的 web.config文件 ?你们中有人知道怎么做吗?我需要修改其他内容吗?

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

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

编辑:

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

    <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>

【问题讨论】:

  • 您能否发布完整的 system.serviceModel 部分。可能很容易帮助你
  • @Rajesh:刚刚编辑。这是一个非常基本的,所以我不明白发生了什么
  • 您能否尝试在配置中的 serviceHostingEnvironment 元素上添加 multipleSiteBindingsEnabled="true" 并查看是否可以正常工作。我尝试了相同的示例,它可以工作
  • 还可以尝试在 URL 路由的配置中包含以下条目:
  • @Rajesh :在达到方法时没有成功,相同的行为和 404 错误

标签: c# wcf rest service endpoint


【解决方案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>

全球.asax

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

【讨论】:

猜你喜欢
  • 2016-11-28
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多