Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wcf

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,
               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

like image 555
user208662 Avatar asked Jan 16 '10 14:01

user208662


5 Answers

Your contract is the Interface not the implementation.

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

like image 200
Shiraz Bhaiji Avatar answered Oct 24 '22 22:10

Shiraz Bhaiji


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

like image 10
Snehlata Shaw Avatar answered Oct 24 '22 22:10

Snehlata Shaw


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">
like image 4
Ardalan Shahgholi Avatar answered Oct 24 '22 22:10

Ardalan Shahgholi


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">
like image 2
user1524080 Avatar answered Oct 25 '22 00:10

user1524080


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();
    }
}
like image 1
Jenna Leaf Avatar answered Oct 24 '22 22:10

Jenna Leaf