サービスの使用
公開日: 2007 年 12 月 6 日 (作業者: walterov (英語))
更新日: 2008 年 3 月 10 日 (作業者: walterov (英語))
はじめに
ホスティング サービスのコード サンプルは Windows Communication Foundation を使用して実装されるので、簡単に使用できます。ここでは、いくつかの重要なポイントについて説明します。WCF サービスの使用方法の詳細については、WCF のドキュメントを参照してください。
この記事は、次の内容で構成されています。
- クライアントの構成
- クライアントの Web.Config
- プロキシの実装
- 標準的なシナリオ
クライアントの構成
クライアント アプリケーションからサービスを呼び出すには、いくらかの構成が必要です。SampleWebClient プロジェクトは、そのようなアプリケーションのサンプルです。各サービスのプロキシを生成するには、Visual Studio® 2008 を使用するか、SvcUtil.exe コマンド ライン ユーティリティ (\Program Files\Microsoft SDKs\Windows\v6.0\Bin にある) を使用します。以下のコマンドは、WebManagementService.svc のプロキシを生成します。
SvcUtil.exe http://<hostheader>/webmanagementservice.svc /out:webmanagementservice.cs
これで、プロジェクトのサービス参照に Proxy クラスを追加できます。
SvcUtil.exe には、プロキシ クラスの生成方法を制御できるさまざまなオプションがあります。詳細については、SvcUtil の関連ドキュメントを参照してください。サンプルのクライアント アプリケーションでは、クラスの競合を避けるため、名前空間オプションを使用して、プロキシ クラス生成のために専用の名前空間を確保しています。
下のスクリーン ショットは、生成されたプロキシのコードの一部を示しています。
クライアントの Web.Config
SampleWebClient では、クライアントの web.config ファイルでアドレスとバインドを指定しています。プロキシのクラスごとにバインドとエンドポイントのセクションがあることに注目してください。
<binding name="WSHttpBinding_IWebManagementService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
<endpoint address="http://HostingServices/WebManagementService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IWebManagementService"
contract="Microsoft.Hosting.Web.Client.WebManagement.IWebManagementService" name="WSHttpBinding_IWebManagementService">
<identity>
<userPrincipalName value="IIS7SHWS.IIS7Live\administrator" />
</identity>
</endpoint>
クライアント アプリケーションを別の環境に移動する場合は、エンドポイントのアドレスと userPrincipalName が正しい環境を反映するように修正されていることを確認してください。
プロキシの実装
クライアント アプリケーションから、System.ServiceModel 名前空間および適切なサブシステムの名前空間を追加します。
using System.ServiceModel; using Microsoft.Hosting.Web.Client.WebManagement; using
Microsoft.Hosting.Web.Client.WebProvisioning;
サービスを呼び出すには、まずプロキシをインスタンス化する必要があります。以下のコマンド ラインは、Web サブシステムのプロビジョニング操作を実行するため、プロキシ オブジェクトをインスタンス化します。
WebProvisioningServiceClient client = new WebProvisioningServiceClient();
実行するメソッドを呼び出し、適切なパラメーターを渡します。パラメーターが複雑なデータ型で構成されている場合は、対応する Data Contract クラスで定義を確認します。
status = client.StartSite(sitename);
プロキシを閉じます。プロキシを閉じると、サービスとのセッションが終了し、接続が解除されます。
client.Close();
標準的なシナリオ
次の CreateWebSite メソッドは、WebSiteProvisioningRequest パラメーターで設定されているサイトのプロパティに基づいてサイトを作成します。
WebProvisioningServiceClient.CreateWebSite(WebSiteProvisioningRequest request)
WebSiteProvisioningRequest は、以下に示すように、Microsoft.Hosting.Web.Client.WebManagement.DataContract 名前空間にあります。
public class WebSiteProvisioningRequest
{
#region Fields
private long _siteId;
private string _siteName;
private string _domainName;
private string _userName;
private string _password;
private string _contentPath;
private bool _useDefaultContentStructure;
private string _physicalRootPath;
private string _logPath;
private string _faultRequestsLoggingPath;
private bool _createNewApplicationPool;
private string _applicationPoolName;
private bool _startSite;
private BindingInfo _bindingInfo;
//give the option for specifing to use an existing application pool or to create a new application pool #endregion
WebSiteProvisioningRequest クラスには、以下を実行するプロパティが格納されます。
- アプリケーション プールの作成
- サイトの作成
- 既定のアプリケーションの作成
- 既定の仮想ディレクトリの作成
- バインドの作成
加えて、ログおよびエラー ログ機能も構成されます。
C# のサンプル
以下のコード サンプルは、サービスを使用して Web サイトを作成する方法を示しています。WebProvisioningServiceClient プロキシをインスタンス化する必要があります。CreateWebSite(WebSiteProvisioningRequest request) メソッドは、パラメーターを必要とします。このメソッドで使用されるデータ型はすべて、対応する Data Contract クラスで確認できます。このサンプルでは、WebSiteProvisioningRequest オブジェクトは Microsoft.Hosting.Web.DataContract 名前空間にあります。
エラー コントラクトは、すべてのサブシステムに適用されます。また、以下のコードに示されている方法で実装できます。
WebProvisioningServiceClient client = new WebProvisioningServiceClient();
try
{
BindingInfo binding = new BindingInfo();
binding.Protocol ="http";
binding.IPAddress ="*"; //all ip addresses
binding.TCPPort = 80;
binding.HostHeader ="www.contoso.com";
WebSiteProvisioningRequest request = new WebSiteProvisioningRequest();
request.SiteName ="www.contoso.com";
request.DomainName =string.Empty;
request.UserName ="Testuser";
request.Password ="pass@word1";
request.ContentPath = @"c:\contents\";
request.UserDefaultContentStructure = false;
request.PhysicalRootPath = @"c:\contents\www.contoso.com";
request.LogPath = @"c:\contents\www.contoso.com\logs";
request.FaultRequestsLoggingPath = c:\contents\www.contoso.com\logs\FailedRequestLogs";
request.Binding = binding;
request.CreateNewApplicationPool = true;
request.ApplicationPoolName = "TestAppPool";
request.StartSite = true;
request.SiteId = 0;
if (client.CreateWebSite(request))
this.lblResult.Text = "Web Site: " + request.SiteName + " has been successfully created.";
client.Close();
}
catch (FaultException<HostingServiceFault> ex)
{
this.lblResult.Text = "FaultException<HostingServiceException>: Hosting service fault while doing " +
ex.Detail.Operation + ". Error: " + ex.Detail.ErrorMessage;
client.Abort();
}
catch (FaultException ex)
{
this.lblResult.Text = "Add Web Site Failed with unknown faultexception: " + e.GetType().Name + " - " + ex.Message;
client.Abort();
}
catch (Exception ex)
{
this.lblResult.Text = "Failed with exception: " + ex.GetType().Name + " - " + ex.Message;
client.Abort();
}
他のサンプルを見るには、Visual Studio ソリューションに含まれているサンプル Web クライアント アプリケーションを参照してください。