最近、.NET 3.5 を使用して開発しているアプリケーションの 1 つで、次の興味深いシナリオを観察しました。この特定のアプリケーションでは、静的変数としてアクセスする singletion オブジェクトがあります。.NET ランタイムは、最初にアクセスしたときにこのシングルトン オブジェクトを初期化するはずだと思っていましたが、そうではないようです。.NET ランタイムは、この特定のオブジェクトにアクセスする前にそれを初期化します。以下は、いくつかの疑似コードです。
if(!initSingleton)
//Do some work without using the singletion class.
else
//Do some work using the singletion class.
実行時でも、私のコードは if ステートメント内のコードのみを実行します。.NET ランタイムはまだシングルトン オブジェクトを初期化します。一部のアプリケーション実行では、この特定のオブジェクトにアクセスする必要はまったくありません!
また、デバッグ ビルドではこの動作は見られません。これは、最適化されたビルド (リリース ビルド) と関係があるようです。
これは .NET ランタイムの予想される動作ですか?
アップデート:
以下は実際のコードです。
private void InitServiceClient(NetworkCredential credentials, bool https)
{
string uri = currentCrawlingWebUrl;
if (!uri.EndsWith("/"))
uri = string.Concat(uri, "/");
uri = string.Concat(uri, siteDataEndPointSuffix);
siteDataService = new SiteData.SiteDataSoapClient();
siteDataService.Endpoint.Address = new EndpointAddress(uri);
if (credentials != null)
{
siteDataService.ClientCredentials.Windows.ClientCredential = credentials;
}
else if (MOSSStateHandler.Instance.UserName.Length > 0 && MOSSStateHandler.Instance.Password.Length > 0)
{
siteDataService.ClientCredentials.Windows.ClientCredential.UserName = MOSSStateHandler.Instance.UserName;
siteDataService.ClientCredentials.Windows.ClientCredential.Password = MOSSStateHandler.Instance.Password;
siteDataService.ClientCredentials.Windows.ClientCredential.Domain = MOSSStateHandler.Instance.Domain;
}
BasicHttpBinding httpBinding = (BasicHttpBinding)siteDataService.Endpoint.Binding;
httpBinding.Security.Mode = (https ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.TransportCredentialOnly);
string authmode = MOSSConnectorConfiguration.Instance.Config.GetString(ConfigConstants.SHAREPOINT_AUTH_PROVIDER, "ntlm");
if (authmode.Equals("ntlm", StringComparison.OrdinalIgnoreCase))
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
else if (authmode.Equals("kerberos", StringComparison.OrdinalIgnoreCase))
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
else
throw new Exception(string.Format("Not supported"));
}
クラスMOSSStateHandlerが初期化されるのをブロックする場合、私のアプリケーションはサイドelseでコードを実行しません。