2010-01-16 25 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(); 
    } 
} 
相關問題