【问题标题】:WCF - Contract Name could not be found in the list of contractsWCF - 在合同列表中找不到合同名称
【发布时间】:2011-01-05 20:44:04
【问题描述】:

我对 WCF 比较陌生。但是,我需要创建一个向 Silverlight 和 AJAX 客户端应用程序公开数据的服务。为了实现这一目标,我创建了以下服务作为概念证明:

[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; 
    }        
}

当我尝试访问此服务时,我收到以下错误:

在服务“myService”实施的合同列表中找不到合同名称“myService”。

这是什么原因?我该如何解决这个问题?

谢谢

【问题讨论】:

    标签: wcf


    【解决方案1】:

    您的合同是接口而不是实现。

    您在配置中的某处编写了 myService 而不是 IJsonService。

    【讨论】:

      【解决方案2】:

      从服务名称中删除命名空间。它会正常工作的。

      【讨论】:

        【解决方案3】:

        修改你的 web.config 你可以找到&lt;services&gt;标签,在这个标签下面你必须有另外两个标签:

        &lt;service ....&lt;endpoint ....

        &lt;endpoint&gt; 标签中你必须引用你的类的接口。

        例如:如果您的服务类名为CustomerSearch,而您的接口名为ICustomerSearch,您必须像这样配置:

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

        【讨论】:

          【解决方案4】:

          我遇到了同样的问题,但我的解决方案是在我的 web.config 中,我指定了整个类名(包括命名空间),而 WCF 只接受一个类名。

          这不起作用:

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

          这行得通:

          <services>
              <service name="RfsProcessor">
          

          【讨论】:

          • 我刚刚删除了我的命名空间,试图解决与 OP 相同的问题,但我的服务消失了。
          • 这是不正确的。命名空间是必需的。程序集不是
          【解决方案5】:

          我之前遇到过 ServiceModel 框架 3.5 的错误,我检查了主机的配置文件。我发现这是我的剪切和粘贴错误。我的服务指向一个旧的不存在的服务,而不是我正在使用的服务。在我更正如下这些行后,它又开始工作了:

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

          请注意,MyService 必须是 ServiceModel 3.5 中合同类的名称,但它是 Framework 4.0 中的 IMyService 合同接口 -->

          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();
              }
          }
          

          【讨论】:

            【解决方案6】:

            web.config 文件中,&lt;service 元素的name 属性必须是带有命名空间的服务类型名称,而不是程序集 (Namespace1.Namespace2.Class)。 &lt;endpoint 元素的 contract 属性同样具有命名空间限定的接口类型 - Namespace1.Namespace2.Interface

            这也解决了所有的行为恶作剧,例如 CreateBehavior 没有被 BehaviorExtensionElement 调用。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-12-01
              • 2010-10-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-05-26
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多