0

ASMX Web サービスを唯一のデータ アクセスとして使用するやや単純な Web アプリがあります。そこからすべての情報が取得され、そこに保存されます。それは邪魔にならないようにうまく機能します。

VS2012 に更新したところ、サービス参照を実装するクラスについて不満があり、IDisposeable から継承されません。

いくつか読んだ後、いくつかのソリューションは本当に精巧で、いくつかは単純であるため、私はさらに混乱しています。短いバージョンは、ほとんど理解していないため、アプリの作成方法に適応できないようです。

私はいくつかのデータ アクセス クラスを持っていますが、そのすべてが特定の領域のメソッドに焦点を当てています。たとえば、顧客関連の通話に対して 1 つのデータアクセス、製品関連の通話に対して 1 つのデータアクセスなどです。

ただし、それらはすべて同じサービスを使用しているため、参照を保持する基本データ アクセス クラスから派生します。

これは基本データ アクセス クラスです。

public class BaseDataAccess
{
    private dk.odknet.webudv.WebService1 _service;
    private string _systemBrugerID, _systemPassword;

    public BaseDataAccess()
    {
        //Gets the system user and password that is stored in the webconfig file. This means you only have to change
        //the username and password in one place without having to change the code = its not hardcoded.
        _systemBrugerID = System.Configuration.ConfigurationManager.AppSettings["SystemBrugerID"].ToString();
        _systemPassword = System.Configuration.ConfigurationManager.AppSettings["SystemPassword"].ToString();
        _service = new dk.odknet.webudv.WebService1();
    }

    /// <summary>
    /// Gets an instance of the webservice.
    /// </summary>
    protected dk.odknet.webudv.WebService1 Service
    {
        get { return _service; }
    }

    /// <summary>
    /// Gets the system user id, used for certain methods in the webservice.
    /// </summary>
    protected string SystemBrugerID
    {
        get { return _systemBrugerID; }
    }

    /// <summary>
    /// Gets the system user password, used for certain methods in the webservice.
    /// </summary>
    protected string SystemPassword
    {
        get { return _systemPassword; }
    }
}

そして、派生クラスが基本クラスからのサービス参照を利用する方法は次のとおりです。

public class CustomerDataAccess : BaseDataAccess
{
    public CustomerDataAccess() {}

    /// <summary>
    /// Get's a single customer by their ID, as the type "Kunde".
    /// </summary>
    /// <param name="userId">The user's username.</param>
    /// <param name="customerId">Customer's "fkKundeNr".</param>
    /// <returns>Returns a single customer based on their ID, as the type "Kunde".</returns>
    public dk.odknet.webudv.Kunde GetCustomerById(string userId, string customerId)
    {
        try
        {
            return Service.GetKunde(SystemBrugerID, SystemPassword, userId, customerId);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
            throw;
        }
    }}

では、この状況で IDisposable を実装するにはどうすればよいでしょうか。私はそれについて頭を包むことができません。

編集 私はサービス参照をいじって、これを思いつきました:

/// <summary>
    /// Gets an instance of the webservice.
    /// </summary>
    protected dk.odknet.webudv.WebService1 Service
    {
        get
        {
            try
            {
                using (_service = new dk.odknet.webudv.WebService1())
                {
                    return _service;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                throw;
            }
        }
    }

はい、例外処理は素晴らしいものではありません。私はそれに到達します(アドバイスをいただければ幸いです)が、VS2012はもうIDisposableの欠如について文句を言いません。サービスのインスタンス化がコンストラクターから削除されました。アプリは、それ以上の変更なしで正常に動作します。これで十分ですか?

4

0 に答える 0