簡體   English   中英

在WCF服務中找不到方法?

[英]Method Not Found in WCF service?

我是創建WCF Web服務的新手。 我正在將VS2012與目標框架4.5一起使用。 我在項目中添加了WCF服務文件。 在“ IService.cs”中,我編寫了以下代碼

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

但是問題出在我運行服務時未顯示我的方法“計算”,當我將URL傳遞為以下本地主機時:2121 / Service1.svc / Calculate,它顯示“不允許的方法”錯誤。

我已經完成了一些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>

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

  1. 您的Calculate方法是為HTTP POST請求設置的。 您應該發出HTTP POST請求以獲得正確的響應。
  2. 您的請求格式為JSON(RequestFormat屬性屬性值),因此請確保您的請求正文包含JSON格式的參數({“ Coil_type”:“ type”,“ Finned_length”:12})。
  3. 為什么在服務實現上有[DataMember]公共字符串輸入? 服務實施通常應攜帶運營合同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM