2010-01-16 24 views
36

我对WCF比较陌生。但是,我需要创建一个将数据公开给Silverlight和AJAX客户端应用程序的服务。在试图做到这一点,我创建了以下服务作为一个概念证明:WCF - 在合同列表中找不到合同名称

[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'实施的合同列表中找到。

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

谢谢

回答

55

您的合同是接口而不是执行。

配置中的某处已写入myService而不是IJsonService。

1

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

这不起作用:

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

这工作:

<services> 
    <service name="RfsProcessor"> 
+0

我刚刚删除了我的命名空间,试图解决与OP相同的问题,并且我的服务消失了。 – ProfK

+0

这是不正确的。命名空间是必需的。大会是**不**。 –

9

卸下服务名称的命名空间。它会正常工作。

3

修改你的web.config 你可以找到<services>标签和低于这个标签的,你必须有另外两个标签:

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

<endpoint>标签,你必须引用接口你的班级。

对于为例:如果你的服务类命名为CustomerSearch,你的界面命名ICustomerSearch你必须配置是这样的:

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

web.config文件时,<service元素的name属性必须是业务类型与名称命名空间,但不是程序集(Namespace1.Namespace2.Class)。 <endpoint元素的contract属性同样具有名称空间限定的接口类型 - Namespace1.Namespace2.Interface

这也解决了所有的行为诡计,例如CreateBehavior没有被调用BehaviorExtensionElement

0

我以前有过ServiceModel framework 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你的合同类的名称,但它是IMyService合同接口在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(); 
    } 
} 
相关问题