0

私はWCF Webサービスを作成する初心者です。ターゲット フレームワーク 4.5 で VS2012 を使用しています。プロジェクトに 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;
        }
    }
}

しかし、サービスを実行するとメソッド Calulate が表示されず、次のように URL を渡すと問題が発生します。

いくつかのグーグルを実行し、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>
4

1 に答える 1

0

考えられる問題がいくつか見られます。

  1. Calculate メソッドが HTTP POST リクエスト用に設定されています。適切な応答を得るには、HTTP POST 要求を行う必要があります。
  2. リクエスト形式は JSON (RequestFormat 属性プロパティ値) であるため、リクエスト本文に JSON 形式のパラメーターが含まれていることを確認してください ({ "Coil_type" : "type", "Finned_length": 12 })。
  3. サービスの実装に [DataMember] public string 入力があるのはなぜですか? サービスの実装は、通常、操作契約を保持する必要があります。
于 2013-09-30T15:08:22.920 に答える