【发布时间】: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