views:

43

answers:

2

I am using WCF REST Start kit to build a RESTFul service. I defined a service like this:

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

MyCollectionServiceBase and IMyCollectionSeviceBase are defined like this:

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
  {...}
}

and here is a section of service in my 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>

When I test my service by using "http://localhost:1290/MyService.svc/", I got error message saying:

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

I am not sure if the endpoint is not matched or something is missing?

+1  A: 

Are you returning an abstract class or interface from one of your service operations? If so, consider adding a ServiceKnownType attribute on the service operation definition (in your interface) that do that.

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

If that's not the case, update your post with your web.config settings.

David Hoerster
updated my config. Since IMyCollectionServiceBase<T> takes generic type, the line of [ServiceKnownType...] seems not accepted.
David.Chu.ca
+2  A: 

You should read the An Introduction To RESTful Services With WCF article (MSDN Magazine, January 2009 issue) for an introduction.

Your current service config is using wsHttpBinding which is not REST (but SOAP instead) - you need ot use webHttpBinding instead.

Also, in order to have a RESTful service, you need to decorate your service operations with either a [WebGet] or a [WebInvoke] attribute and optionally provide parameters to define how exactly that HTTP URI should be called and should react.

marc_s
You are right. Adding a wcf service by using wcf service template will add those settings and svc files automatically. Actually I found that there are also two REST wcf servcies for singleton and collection templates provided by WCF REST Start kit. Adding those ones will not update web.config at all, and my web REST service works fine now.
David.Chu.ca