开发者

WCF - Contract Name could not be found in the list of contracts

开发者 https://www.devze.com 2022-12-17 02:05 出处:网络
I am relatively new to WCF. However, I need to create a service that exposes data to both Silverlight and AJAX client applications. In an attempt to accomplish this, I have created the following servi

I am relatively new to WCF. However, I need to create a service that exposes data to both Silverlight and AJAX client applications. In an attempt to accomplish this, I have created the following service to serve as a proof of concept:

[ServiceContract(Namespace="urn:MyCompany.MyProject.Services")]
public interface IJsonService
{
    [OperationContract]
    [WebInvoke(Method = "GET",
               RequestFormat=WebMessageFormat.Json,开发者_StackOverflow中文版
               ResponseFormat = WebMessageFormat.Json)]
    List<String> JsonFindNames();
}

[ServiceContract(Namespace="urn:MyCompany.MyProject.Services")]
public interface IWsService
{
    [OperationContract(Name="FindNames")]
    List<String> WsFindNames();
}


[ServiceBehavior(Name="myService", Namespace="urn:MyCompany.MyProject.Services")]
public class myService : IJsonService, IWsService
{
    public List<String> JsonFindNames() 
        { return FindNames(); }
    public List<String> WsFindNames()
        { return FindNames(name); }
    public List<string> FindNames()
    { 
       List<string> names = List<string>(); 
       names.Add("Alan");
       names.Add("Bill");
       return results; 
    }        
}

When I try to access this service, I receive the following error:

The contract name 'myService' could not be found in the list of contracts implemented by the service 'myService'.

What is the cause of this? How do I fix this?

Thank you


Your contract is the Interface not the implementation.

Somewhere in the config you have written myService instead of IJsonService.


Remove the namespace from Service name. It will work fine.


Modify your web.config You can find <services> tag and below of this tag you have to have two other tag :

<service .... And <endpoint ....

In <endpoint> tag you have to reference to interface of your class.

For exampl : If your service class named CustomerSearch and your interface named ICustomerSearch you have to config like this :

  <service name="CustomerSearch" behaviorConfiguration="ServiceBehavior">
  <endpoint address="" binding="webHttpBinding" contract="ICustomerSearch" 
            behaviorConfiguration="ServiceAspNetAjaxBehavior">


I had the same issue, but my solution was that in my web.config, I was specifying the entire class name (including namespace), whereas WCF would only accept a class name.

This didn't work:

<services>
    <service name="BusinessServices.Web.RfsProcessor">

This worked:

<services>
    <service name="RfsProcessor">


I have had that error before for ServiceModel framework 3.5, and I checked my host's config file. I found it was my cut-and-paste error. My service was pointing to an old non-existing service than the one I am using. It starts working again after I corrected these lines like below:

<system.serviceModel>
<services>
  <!--<service name="NotUsed.Serv">-->
  <service name="InUse.MyService">
    <host>
      <baseAddresses>
        <!--<add baseAddress="http://localhost:8181/LastService" />-->
        <add baseAddress="http://localhost:8181/InUseService"/>
      </baseAddresses>
    </host>
  </service>
</services>
</system.serviceModel>

Note that MyService has to be the name of your contract class in ServiceModel 3.5 BUT IT IS IMyService contract interface in Framework 4.0 -->

namespace InUse {
[ServiceContract]
public interface IMyService 
{
    [WebGet(UriTemplate = "/GetList/{PATTERN}",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    List<string> GetIDListByPattern(string PATTERN);

}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]    
public class MyService : IMyService
{        
    List<string> MySample = (new _PointRT()).Sample().Select(r=>r._pointXID).ToList();

    public List<string> GetIDListByPattern(string PATTERN) {
        return MySample.Where(x => x.Contains(PATTERN)).ToList();
    }
}


In the web.config file, the <service element's name attribute needs to be the service type's name with the namespace, but not the assembly (Namespace1.Namespace2.Class). The <endpoint element's contract attribute similarly has namespace-qualified interface type - Namespace1.Namespace2.Interface.

This also solves all behavior shenanigans, like CreateBehavior not being invokes on BehaviorExtensionElement.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号