0

私はWCFが初めてで、それを起動して実行しようとしています。エラーなしで空のバージョンを取得することさえできません。

web.config:

 <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="ServiceAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>

        <!--wcf-->     
        <behavior name="recordsWCF">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="recordsSWCF" >
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    <!--end wcf-->      

    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />   
    <services>


      <!--wcf-->
      <service name="records.wcf" behaviorConfiguration="recordsSWCF" >
        <!--<endpoint address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="recordsWCF"/>-->
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      </service>
      <!--end wcf-->         

      <service name="Service">
        <endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="Service" />
      </service>
    </services>
  </system.serviceModel>

私の wcf コードに関しては、やるべきことはあまりありません...まだです。エラーなしで web.config をセットアップする方法を学ぼうとしています。そして、ここに私のwcf定義があります:

[ServiceContract(Namespace = "records")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class wcf
{

 public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }      
    [OperationContract]
    [WebGet]
    public string GetData()
    {
        Person p = new Person();
        List<Person> test = new List<Person>();
        p.Name = "Tom";
        p.Age = 28;   
        test.Add(p);   
        p = new Person();
        p.Name = "Dan";
        p.Age = 22;
        test.Add(p);
        JavaScriptSerializer js = new JavaScriptSerializer();   
        string jsonString = js.Serialize(test);    

        return jsonString;
    }    
}
4

1 に答える 1

2

web.config の records.wcf サービス セクションでコメント アウトされていますが、公開している (または公開しようとしている) エンドポイントは、REST バインディングである webHttpBinding エンドポイントのようです。WCF メタデータ交換エンドポイントは、SOAP エンドポイント専用です (basicHttpBinding または wsHttpBinding など)。このような WCF REST サービスからメタデータを取得する方法はありません。

メタデータが必要な場合は、次の 2 つのオプションがあります。

  1. サービスを SOAP サービスにリファクタリングする
  2. WCF の代わりに、ASP.Net Web API を使用します。これは REST 専用であり、WCF のメタデータ エンドポイントと概念的に同じ自動生成されたヘルプ ページを提供します。

あなたの質問から判断すると、メタデータは実際には目的ではなく、サービスが順調に進んでいるかどうかを確認する方法にすぎません。この場合、次のことを行う必要があります。

  1. recordWCF サービス動作から serviceMetadata 要素を削除します
  2. IMetadataExchange エンドポイントを削除する
  3. サービス エンドポイントを追加する

web.config は次のようになります。

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>

        <!--wcf-->
        <behavior name="recordsWCF">
          <webHttp/>
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="recordsSWCF" >
          <!--<serviceMetadata httpGetEnabled="true" />-->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <!--end wcf-->

    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>


      <!--wcf-->
      <service name="records.Service" behaviorConfiguration="recordsSWCF" >
        <endpoint address="" binding="webHttpBinding" contract="records.Service" behaviorConfiguration="recordsWCF"/>
        <!--<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />-->
      </service>
      <!--end wcf-->
    </services>
  </system.serviceModel>

そして、サービスを実装するクラスは次のようになります

using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web.Script.Serialization;

namespace records
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceContract]
    public class Service
    {
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

        [WebGet]
        public string GetData()
        {
            Person p = new Person();
            List<Person> test = new List<Person>();
            p.Name = "Tom";
            p.Age = 28;
            test.Add(p);
            p = new Person();
            p.Name = "Dan";
            p.Age = 22;
            test.Add(p);
            JavaScriptSerializer js = new JavaScriptSerializer();
            string jsonString = js.Serialize(test);

            return jsonString;
        }
    }
}

.svc のマークアップは次のようになります。

<%@ ServiceHost Language="C#" Debug="true" Service="records.Service" CodeBehind="Service.svc.cs" %>

その構成で、参照するhttp://localhost:56157/Service.svc/GetDataと取得します

[{"Name":"Tom","Age":28},{"Name":"Dan","Age":22}]

これは、IE ではなく Chrome で行う必要があります。IE で試してみると、JSON をファイルとして開こうとします。

サービスを実装する同じクラスで定義されたサービス コントラクトを持つことは少し珍しいことに注意してください。より一般的には、サービス コントラクトはインターフェイス (IService など) で定義され、クラス (public class Service : IService など) で実装されます。

また、WCF を使用する特別な理由 (SOAP サービスを実行したいなど) がない限り、REST サービスには ASP.Net Web API を強くお勧めします。私は一般的に ASP.Net よりも WCF についてよく知っていますが、それでもASP.Net Web API を使用して REST を実行する方がはるかに簡単だと思います。

于 2013-03-03T20:14:23.190 に答える