WCF REST web service: contract name cannot be found
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 = AspNetCompatib开发者_运维问答ilityRequirementsMode.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?
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.
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.
精彩评论