Azure AppFabricによるオンプレミス連携の適用例

2010年1月25日(月)
勇 大地

オンプレミス・システムの公開サービス

通常のWCFサービスと同様に、サービスを開発できます。

製品インスタンスProductsを配列で返すGetCampainAllメソッドを持つIProductContractクラスを、公開サービスとして実装します。
--------------------------------------------------------------------------------
【ProductContract.csから抜粋】

[ServiceContract(Name = "IProductContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
public interface IProductContract
{
    [OperationContract]
    Products[] GetCampaignAll();
}

public interface IProductChannel : IProductContract, IClientChannel { }
--------------------------------------------------------------------------------

製品がキャンペーン中か否かを判定するようにIProductContractを実装します。
--------------------------------------------------------------------------------
【ProductService.csから抜粋】

[ServiceBehavior(Name = "ProductService", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
class ProductService : IProductContract
{
    public Products[] GetCampaignAll()
    {
        return dataContext.Products.Where(product => product.IsCampaign).ToArray();
    }
(後略)
--------------------------------------------------------------------------------

オンプレミス・システムでサービスを公開する

オンプレミス・システム側でサービスを公開する手順について説明します。具体的には、以下の通りです。

  1. Web.Configの登録データを読み取る
  2. 接続モードをHTTPとして、サービスURIを作成し、Web.Configに登録されたサービス名を指定してChannelFactoryを作成する
  3. エンドポイント認証用のオブジェクトを生成し、ChannelFactoryに認証情報を登録する
  4. 公開サービスをオープンする
  5. 公開サービスをクローズする

1~4.はApplication_Startメソッド内、5.はApplication_Endメソッド内の処理となります。
--------------------------------------------------------------------------------
【Global.asax.csの抜粋】

protected ServiceHost host;

protected void Application_Start(object sender, EventArgs e)
{
    //サービス名前空間
    string servicePath = ConfigurationManager.AppSettings["ServicePath"];
    string serviceNamespace = ConfigurationManager.AppSettings["ServiceNamespace"];
    string issuerName = ConfigurationManager.AppSettings["IssuerName"];
    string issuerSecret = ConfigurationManager.AppSettings["IssuerSecret"];

  //接続モードはHTTPを選択
    ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http;

  // サービス名前空間からURIを作成
    Uri address = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, servicePath);

  // エンドポイント用の認証オブジェクトを作成
    TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
    sharedSecretServiceBusCredential.CredentialType = TransportClientCredentialType.SharedSecret;
    sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = issuerName;
    sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = issuerSecret;

  // サービスホストの作成
    host = new ServiceHost(typeof(ProductService), address);

  // エンドポイント用にServiceRegistrySettings を作成
    IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public);

  // 認証情報をすべてのエンドポイントに設定
    foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
    {
        endpoint.Behaviors.Add(serviceRegistrySettings);
        endpoint.Behaviors.Add(sharedSecretServiceBusCredential);
    }

  // 公開サービスをオープン
    host.Open();
}

protected void Application_End(object sender, EventArgs e)
{
    // 公開サービスをクローズ
    host.Close();
}
--------------------------------------------------------------------------------

次ページでは、オンプレミス側に作成した公開サービスと連携するよう、Azure platform側に設定を行います。

野村総合研究所(NRI)
野村総合研究所に入社後、主にJava/Webのフレームワーク開発・普及に従事している。プライベートでは.NET系の技術が好きで、Windows AzureやSilverlightに興味を持ち独自に学習している。趣味はひきこもる事で、机の半径2メートル以内が主な生活エリア。

Think ITメルマガ会員登録受付中

Think ITでは、技術情報が詰まったメールマガジン「Think IT Weekly」の配信サービスを提供しています。メルマガ会員登録を済ませれば、メルマガだけでなく、さまざまな限定特典を入手できるようになります。

Think ITメルマガ会員のサービス内容を見る

他にもこの記事が読まれています