0

Visual Studio 2010 で WCF サービス アプリケーションを作成しました。次の方法を除いて、すべての方法が機能します。

public List<Friend> SendFriendList(int id)
    {
        FyfDataContext db = new FyfDataContext();
        List<Friend> friends = db.Friends.Where(P => P.PK_ID == id).ToList();
        return friends;
    }

実際にはデータを完全に受信しますが、Google Chrome でテストすると表示されません。 データを受信したことの証明

ここにひねりがあります:この問題の前に、私は Visual Studio 2012 で WCF Service App 全体を作成しました。そこでは完全に機能しましたが、他の問題のために 2010 に戻らなければなりませんでした。

私の IService.cs のスニペット:

public interface IService2
    {
            [OperationContract]
            [WebGet(UriTemplate = "/Friendlist?id={id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            List<Friend> SendFriendList(int id);
            [OperationContract]
            [WebGet(UriTemplate = "/Friendloc?id={id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            Friendloc SendFriendLoc(int id); 
    }

Friendloc メソッドは機能しますが、他のすべてのメソッドと同様に、リストではなくオブジェクトを返します。
問題は web.config にあると思われますが、何が原因かわかりません。VS12 は 4.5 フレームワークを作成し、VS10 は 4.0 を作成したことを認識しています。Visual Studio 2010
のWeb.config は次のとおりです。

 <?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="FyfConnectionString" connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\Fyf.mdf;Integrated Security=True;User Instance=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

    <behaviors>
      <serviceBehaviors>
        <behavior name ="servicebehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restbehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name ="Fyf.Service1"
      behaviorConfiguration ="servicebehavior" >
        <endpoint name ="SOAPEndPoint"
        contract ="Fyf.IService1"
        binding ="basicHttpBinding"
        address ="soap" />

        <endpoint name ="RESTEndPoint"
        contract ="Fyf.IService2"
        binding ="webHttpBinding"
        address ="rest"
        behaviorConfiguration ="restbehavior"/>

        <endpoint contract="IMetadataExchange"
        binding="mexHttpBinding"
        address="mex" />
      </service>
    </services>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Visual Studio 2012のWeb.config は次のとおりです。

   <?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="FyfdbConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Fyfdb.mdf;Integrated Security=True"

      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name ="servicebehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restbehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name ="Fyf.Service1"
      behaviorConfiguration ="servicebehavior" >
        <endpoint name ="SOAPEndPoint"
        contract ="Fyf.IService1"
        binding ="basicHttpBinding"
        address ="soap" />

        <endpoint name ="RESTEndPoint"
        contract ="Fyf.IService2"
        binding ="webHttpBinding"
        address ="rest"
        behaviorConfiguration ="restbehavior"/>

        <endpoint contract="IMetadataExchange"
        binding="mexHttpBinding"
        address="mex" />
      </service>
    </services>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

要素の一部を VS12 バージョンから VS10 にコピーしてみました。aspNetCompatibilityEnabledを「true」にして、directoryBrowseを入れるなど。しかし、運が悪い。
私が見逃しているのが非常に単純なものであれば申し訳ありません。これは私の最初の WCF サービスです。どんな助けでも大歓迎です!

4

1 に答える 1

0

なぜこれが機能するのか、他の方法は機能しないのかよくわかりません。(ほぼ) 同じ属性を持つ Userlist と呼ばれる DataContract を作成しました (ID でテストしただけです)。
確かに良いコードではありませんが、動作します。ただし、他の問題についてはまだ興味があります。

public List<Userlist> Friendlist(int id)
    { 
        int i = 0;
        FyfDataContext db = new FyfDataContext();
        List<Friend> friends = db.Friends.Where(P => P.PK_ID == id).ToList();
        List<Userlist> userlist = new List<Userlist>();

        while (i < friends.Count)
        {
            Userlist u = new Userlist();
            u.id = Convert.ToInt32(friends[i].FK_ID);
            userlist.Add(u);
            i++;
        }

        return userlist;
    }
于 2013-10-13T23:18:07.997 に答える