【问题标题】:WCF REST service doesn't seems to work - configuration issue?WCF REST 服务似乎不起作用 - 配置问题?
【发布时间】:2011-12-20 14:00:06
【问题描述】:

我有一个带有自定义绑定的 WCF 服务,托管在 IIS(开发中的 IIS Express)中,具有以下服务合同:

[ServiceContract]
public interface IServer
{
    [OperationContract]
    StreamCollection GetStreams(Guid poolKey);

    [OperationContract]
    PoolCollection GetPools();

    [OperationContract]
    StreamEntity GetStream(Guid poolKey, string localIndex);    
}

它工作正常(从客户端,我也可以看到它在 WCFTestClient 中发现的元数据正常)。
我必须将其功能公开为 REST,因此我创建了一个新合约,如下所示

[ServiceContract]
public interface IRestServer
{
    [OperationContract(Name="GetStreamsREST")]
    [WebGet(UriTemplate = "pool/{poolKey}/streams")]
    StreamCollection GetStreams(string poolKey);

    [OperationContract(Name = "GetPoolsREST")]
    [WebGet(UriTemplate = "pools")]
    PoolCollection GetPools();

    [OperationContract(Name = "GetStreamREST")]
    [WebGet(UriTemplate = "pool/{poolKey}/stream/{localIndex}")]
    StreamEntity GetStream(string poolKey, string localIndex);
}

我在同一个服务类中实现了两个接口。 web.config 文件是

<behaviors>
  <endpointBehaviors>
    <behavior name="webHttp">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<bindings>
  <customBinding>
    <binding name="CustomBinding">
      <binaryMessageEncoding />
      <httpTransport maxReceivedMessageSize="655360" />
    </binding>
  </customBinding>
</bindings>
<services>
  <service behaviorConfiguration="ServiceBehavior" name="MyServ.Server.Server">
    <endpoint address="" binding="customBinding" bindingConfiguration="CustomBinding" contract="MyServ.Server.IServer" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <endpoint address="rest" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="MyServ.Server.IRestServer" />
  </service>
</services>

但是,当我使用 rest 浏览访问服务时,使用类似 url 的 URL

http://localhost:<myport>/Service.svc/pools 
http://localhost:<myport>/Service.svc/pool/somekey/streams
http://localhost:<myport>/Service.svc/pool/somekey/streams

我收到错误 404。

我在某些方法中设置了断点,并将调试器附加到 IIS 进程,但似乎没有调用任何内容。

如果我使用 WCFTestClient 检查服务元数据,它只会看到 IService,而不是 IRestService。这是正常的吗? 编辑:是的,似乎是 (check this)

感谢您的任何建议。

【问题讨论】:

  • 我假设您的同一个服务实现了这两个接口?
  • 是的。这种方法可能有问题?
  • 我打算建议如果两者都没有实现,可能会有问题。 :)
  • 知道我可能做错了什么吗?
  • 您忘记了端点地址。链接应为:localhost:<myport>/Service.svc/rest/pools

标签: wcf wcf-rest


【解决方案1】:

REST 端点不会以 WCFTestClient 可以使用的方式公开元数据,这解释了为什么您无法访问它。而且您正在浏览的地址不正确,因为您将 REST 端点的端点地址指定为“rest”,您需要以类似的方式访问它们

http://localhost:port/Service.svc/rest/pool/myPoolKey/streams
http://localhost:port/Service.svc/rest/pools
http://localhost:port/Service.svc/rest/myPoolKey/stream/1

另一件事:[OperationContract] 属性中的 Name 属性对于 REST 端点无关紧要,因此您可以删除它(不过,将它放在那里并没有什么坏处)。此外,如果您使用的是 .NET 4.0,您甚至根本不需要 [OperationContract] 属性,因为您已经拥有 [WebGet],因此您的接口可以定义为

[ServiceContract]
public interface IRestServer
{
    [WebGet(UriTemplate = "pool/{poolKey}/streams")]
    StreamCollection GetStreams(string poolKey);

    [WebGet(UriTemplate = "pools")]
    PoolCollection GetPools();

    [WebGet(UriTemplate = "pool/{poolKey}/stream/{localIndex}")]
    StreamEntity GetStream(string poolKey, string localIndex);
}

【讨论】:

    【解决方案2】:

    您能否发布有关如何调用 WCF 服务的代码?前几天我遇到了类似的问题,客户端应用程序使用 HTTP POST 而不是 HTTP GET 调用页面,因此出现 404 错误。

    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GuessWhat")]
    

    对

    [WebGet(UriTemplate = "/GuessWhat/{variable}", ResponseFormat = WebMessageFormat.Json)] 
    

    我不确定您的服务是否具有此属性,但请确保执行您的服务合同的服务具有此属性。

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    

    【讨论】:

    【解决方案3】:

    感谢大家的所有建议。 不过,我终于解决了这个问题。

    问题是我仍然不知道问题出在哪里,但我决定采取不同的更“干净”的方式。

    所以我所做的是创建一个不同的服务 SVC 文件 RestServer,我在其中实现了 REST 方法

    我修改了 web config 中的设置,使其具有两种不同的服务,一种用于原始服务(纯 WCF),一种用于 REST 服务,如下所示

    <service name="MyServ.Server.RestServer" >
        <endpoint address="" name="RESTEndpoint" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="MyServ.Server.IRestServer" />
    </service> 
    

    这成功了。

    其实我应该从一开始就使用这种方式,遵守单一职责原则……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 2016-08-26
      • 2018-06-07
      • 2018-07-05
      相关资源
      最近更新 更多