WCF REST Web服务:无法找到合同名称

时间:2010-07-16 20:28:25

标签: visual-studio-2008 wcf rest

我正在使用WCF REST Start工具包来构建RESTFul服务。我定义了这样的服务:

namespace MyNS {
  [ServiceBehavior(IncludeExceptionDetailInFaults = true, 
   InstanceContextMode = InstanceContextMode.Single,
   ConcurrencyMode = ConcurrencyMode.Single)]
  [AspNetCompatibilityRequirements(
   RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  public class MyService : MyCollectionServiceBase, IMyCollectionServiceBase 
  {...}
}

MyCollectionServiceBase和IMyCollectionSeviceBase的定义如下:

namespace MyNS.Contract {
  [ServiceContract]
  public interface IMyCollectionServiceBase<TItem> where TItem : class 
  {...}

  // COllectionServiceBase is an abstract class in 
  // Microsoft.ServiceModel.Web.SpecializedServices
  public abstract class MyCollectionServiceBase<TItem> :
    CollectionServiceBase<TItem>
    where TItem : class
  {...}
}

这是我的Web.config中的一部分服务

<serviceBehaviors>
  ...
  <behavior name="MyNS.MyService1Behavior">
    <serviceMetadata httpGetEnabled="true" />
    <serviceDebug includeExceptionDetailInFaults="false" />
  </behavior>
</serviceBehaviors>
...
<service behaviorConfiguration="MyNS.MyService1Behavior"
  name="MyNS.MyService1">
  <endpoint address="" binding="wsHttpBinding" 
    contract="MyNS.IMyService1">
    <identity>
      <dns value="localhost" />
    </identity>
  </endpoint>
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>

当我使用“http://localhost:1290/MyService.svc/”测试我的服务时,收到错误消息:

The contract name 'MyNS.Contract.IMyCollectionServiceBase' could not be found 
in the list of contracts implemented by the service 'MyService'.

我不确定端点是否匹配或缺少什么?

2 个答案:

答案 0 :(得分:2)

您应该阅读An Introduction To RESTful Services With WCF文章(MSDN杂志,2009年1月刊)进行介绍。

您当前的服务配置使用wsHttpBinding REST(而非SOAP) - 您需要使用webHttpBinding代替。

此外,为了获得RESTful服务,您需要使用[WebGet][WebInvoke]属性来装饰服务操作,并可选择提供参数来定义应该如何调用HTTP URI。并且应该做出反应。

答案 1 :(得分:1)

您是从某个服务操作返回抽象类或接口吗?如果是这样,请考虑在服务操作定义(在您的界面中)添加ServiceKnownType属性。

[OperationContract()]
[WebGet(...)]
[ServiceKnownType(typeof(IMYCollectionServiceBase))]
IWhatever MyServiceOperation(...);

如果不是这种情况,请使用您的web.config设置更新您的帖子。