2013-09-30 69 views
0

我是創建WCF Web服務的新手。我正在使用VS2012與目標框架4.5。我在我的項目中添加了一個WCF服務文件。 在「IService.cs」我寫了下面的代碼在WCF服務中找不到方法?

namespace _3TWebServ 
    { 
     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
     [ServiceContract] 
     public interface IService1 
     { 
      [OperationContract] 
      [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, 
          RequestFormat = WebMessageFormat.Json, 
          UriTemplate = "Calculate")] 
      String Calculate(Inputs ip1); 
     } 


     [DataContract] 
     public class Inputs 
     { 
      [DataMember(Name = "Coil_type")] 
      public string Coil_type { get; set;} 

      [DataMember(Name = "Finned_length")] 
      public string Finned_length { get; set;} 
     } 

    } 

和「Service.svc.cs」

namespace _3TWebServ 
{ 
    public class Service1 : IService1 
    { 
     [DataMember] 
     public string input; 

     public String Calculate(Inputs ip1) 
     { 
      String str = ip1.Coil_type + ip1.Finned_length; 
      return str; 
     } 
    } 
} 

但是當我跑我的服務其沒有顯示問題就來了我方法Calulate,當我通過我的URL如下 localhost:2121/Service1.svc /計算它顯示「方法不允許」的錯誤。

我已經做了一些Google搜索並啓用了我的IIS管理器目錄瀏覽功能。我的配置文件如下

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    </system.web> 

    <system.serviceModel> 
    <services> 
     <service behaviorConfiguration="_3TWebServ.IService1" name="_3TWebServ.Service1"> 
     <endpoint address="" behaviorConfiguration="Rest" binding="webHttpBinding" contract="_3TWebServ.IService1"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <!--endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />--> 
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="_3TWebServ.IService1"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 

     <endpointBehaviors> 
     <behavior name="Rest"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 

    </behaviors> 
    </system.serviceModel> 

    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 

回答

0

我可以看到一些可能的問題。

  1. 您的計算方法是爲HTTP POST請求設置的。您應該發出HTTP POST請求以獲得正確的響應。
  2. 您的請求格式是JSON(RequestFormat屬性值),因此請確保您的請求主體包含JSON格式的參數({「Coil_type」:「type」,「Finned_length」:12})。
  3. 爲什麼在服務實現上有[DataMember]公共字符串輸入?服務實施通常應該承擔運營合同。
+0

請告訴如何提出HTTP發佈請求 – Rahul

+0

這取決於您的客戶端應用程序。您可以使用HttpClient,HttpWebRequest,ajax/javasript,fiddler進行POST請求。您可以搜索互聯網瞭解更多詳情。 – Praburaj