Azure AppFabricによるオンプレミス連携の適用例
オンプレミス・システムの公開サービス
通常の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();
}
(後略)
--------------------------------------------------------------------------------
オンプレミス・システムでサービスを公開する
オンプレミス・システム側でサービスを公開する手順について説明します。具体的には、以下の通りです。
- Web.Configの登録データを読み取る
- 接続モードをHTTPとして、サービスURIを作成し、Web.Configに登録されたサービス名を指定してChannelFactoryを作成する
- エンドポイント認証用のオブジェクトを生成し、ChannelFactoryに認証情報を登録する
- 公開サービスをオープンする
- 公開サービスをクローズする
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側に設定を行います。