SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

Infragistics NetAdvantageチュートリアル(AD)

Silverlight/WPFでデータバインディングを利用しOutlookライクなスケジュールを構築する その3
WCF サービス 接続編

Silverlight/WPFでOutlookライクなアプリ構築(3)

  • このエントリーをはてなブックマークに追加

WCF サービスの実装

 xamSchedule にはクライアントにスケジュール データを提供するための WCF サービスクラスを 2 個用意しています。用途に合わせて選択してください。

クラス 概要
WcfListConnectorServiceSingle 1 度のみ作成されるサービスインスタンス。クライアントからのリモート コールはサーバーの同じスレッドで動作するためパフォーマンスの低下を防げる。ただし、接続クライアント数が多い場合はボトルネックが発生し、それがパフォーマンス低下につながる可能性がある
WcfListConnectorServiceMulti 抽象クラス。クライアントからのリモート コールごとに新しいインスタンスが作成され、サーバーの異なるスレッドで動作する。クライアント接続数が多い場合にボトルネックを発生しにくい。ただし、毎回インスタンスを作成するので、クライアント数が多くない場合にはパフォーマンスは Single に比べ低くなる

 この記事では WcfListConnectorServiceSingle を利用します。そのため、xamSchedule_SL.Web プロジェクトに次のアセンブリ参照を追加します。

  • Infragistics4.Services.Schedules.WcfConnectorService.v10.3.dll
図8 アセンブリ参照の追加
図8 アセンブリ参照の追加

 次に、[新しい項目の追加] - [Silverlight] を選択し、Silverlight 対応 WCF サービス を "XamScheduleWCFService" と名付け作成します。

図9 Silverlight 対応 WCF サービスの追加
図9 Silverlight 対応 WCF サービスの追加

 作成された XamScheduleWCFService.svc.cs を開き、WcfListConnectorServiceSingle を継承します。また、ASP.NET 互換となるように設定します。このクラスではエンティティのマッピングを行っています。

コード1 サービスの実装
using System.ServiceModel.Activation;
using Infragistics.Services.Schedules;
using Infragistics.Controls.Schedules.Services;

namespace xamSchedule_SL.Web
{
    // ASP.NET 互換を設定
    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
    public class XamScheduleWCFService : WcfListConnectorServiceSingle
    {
        // スケジュール エンティティ
        private xamSchedule_SampleEntities entities;

        public XamScheduleWCFService()
        {
            entities = new xamSchedule_SampleEntities();

            // リソースの設定
            this.ResourceItemsSource = entities.Resources;
            this.ResourcePropertyMappings =
                    new ResourcePropertyMappingCollection();
            this.ResourcePropertyMappings.UseDefaultMappings = true;

            // リソースカレンダーの設定
            this.ResourceCalendarItemsSource = entities.ResourceCalendars;
            this.ResourceCalendarPropertyMappings =
                    new ResourceCalendarPropertyMappingCollection();
            this.ResourceCalendarPropertyMappings.UseDefaultMappings = true;

            // 予定の設定
            this.AppointmentItemsSource = entities.Appointments;
            this.AppointmentPropertyMappings =
                    new AppointmentPropertyMappingCollection();
            this.AppointmentPropertyMappings.UseDefaultMappings = true;
        }
    }
}

 サービスの実装を完了し、Web.Config の contract を修正します。

コード2 Web.Config の修正
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <connectionStrings>
    <add name="xamSchedule_SampleEntities" connectionString="metadata=res://*/XamScheduleDataModel.csdl|res://*/XamScheduleDataModel.ssdl|res://*/XamScheduleDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;Initial Catalog=xamSchedule_Sample;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <customBinding>
        <binding name="xamSchedule_SL.Web.XamScheduleWCFService.customBinding0">
          <binaryMessageEncoding />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>
    <services>
      <!-- コントラクトを Infragistics.Services.Schedules.IWcfListConnectorService に修正-->
      <service name="xamSchedule_SL.Web.XamScheduleWCFService">
        <endpoint address="" binding="customBinding" bindingConfiguration="xamSchedule_SL.Web.XamScheduleWCFService.customBinding0"
          contract="Infragistics.Services.Schedules.IWcfListConnectorService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

 XamScheduleWCFService.svc をブラウザで開き、下記のような画面が出力されることを確認します。

図10 サービスの動作を確認
図10 サービスの動作を確認

次のページ
Silverlight アプリケーション ソリューションでサービスを使用

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
Infragistics NetAdvantageチュートリアル連載記事一覧

もっと読む

この記事の著者

インフラジスティックス・ジャパン株式会社 池原 大然(イケハラ ダイゼン)

国内ベンチャー企業にて.NETエンジニアとして開発に従事、2007年インフラジスティックス・ジャパンに入社。現在デベロッパー エバンジェリストとして、.NETやWPF/Silverlight製品や技術の啓蒙活動を行う。Microsoft MVP for Client App Dev 2010/04 ...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

【AD】本記事の内容は記事掲載開始時点のものです 企画・制作 株式会社翔泳社

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/5677 2011/01/05 14:28

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング