2010-11-25 56 views
1

我們已經嘗試使用HTTp Get獲得一個非常非常簡單的WCF服務,但我們無法使其工作。 我們跟着那些「指南」,但它不工作如何通過HTTP獲取使用WCF服務(在Visual Studio 2010中)

當我們撥打我們的服務與下面的網址,我們得到找不到網頁錯誤:

http://localhost:9999/Service1.svc/GetData/ABC

基本URL(http:// localhost:9999/Service1.svc)工作正常,並正確返回wcf服務信息頁面。

這些是重現我們示例的步驟和代碼。

  1. 在Visual Studio 2010中,創建一個新的 「WCF服務應用程序」 項目
  2. 與此代碼

    [ServiceContract()] 
        public interface IService1 
        { 
         [OperationContract()] 
         [WebInvoke(Method = "GET", 
           BodyStyle = WebMessageBodyStyle.Bare, 
           UriTemplate = "GetData/{value}")] 
         string GetData(string value); 
        } 
    
  3. 更換IService接口與此代碼

    public class Service1 : IService1 
    { 
        public string GetData(string value) 
        { 
         return string.Format("You entered: {0}", value); 
        } 
    } 
    
    更換服務類
  4. web.config看起來像這樣

    <system.web> 
        <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
        <services> 
         <service name="Service1"> 
          <endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="WebBehavior1"> 
          </endpoint> 
         </service> 
        </services> 
        <behaviors> 
         <endpointBehaviors> 
          <behavior name="WebBehavior1"> 
          <webHttp helpEnabled="True"/> 
        </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
    <behavior> 
        <serviceMetadata httpGetEnabled="true"/> 
        <serviceDebug includeExceptionDetailInFaults="false"/> 
        </behavior> 
    </serviceBehaviors> 
    

  5. 按運行,並試圖調用get方法

如果有人得到這或類似的東西的工作,那將是很親切,如果你能回覆有關工作示例的信息。

非常感謝你

回答

1

我重新創建了你的樣本 - 作品像一個魅力。

一點:您的服務合同(public interface IService1)和服務實現(public class Service1 : IService1)是否存在於一個.NET命名空間內?

如果是這樣,你需要改變你的* .SVC和你web.config包括:

<services> 
     <service name="Namespace.Service1"> 
      <endpoint address="" binding="webHttpBinding" 
        contract="Namespace.IService1" 
        behaviorConfiguration="WebBehavior1"> 
      </endpoint> 
     </service> 
    </services> 

<service name="...">屬性和<endpoint contract="...">必須包括這項工作的.NET命名空間。

+0

你是非常正確的...這只是因爲缺少根名稱空間。它可以正常工作(沒有根名稱空間)用於其他綁定,但不適用於webHttpBinding。非常感謝你。 – 2010-11-25 17:44:05

相關問題