对ASMX Web服务的引用想要实现IDisposable

时间:2013-10-10 21:14:17

标签: c# .net wcf asmx

好的,这对我来说有点新鲜。

我有一个简单的网络应用程序,它使用ASMX Web服务作为其唯一的数据访问。所有信息都是从中获取的,并保存到它。它工作得很好,所以不用了。

我刚刚更新到VS2012,它抱怨实现服务引用的类,不继承自IDisposeable。

经过一些阅读后,我更加困惑,因为有些解决方案非常精细,有些解决方案很简单。简短的版本是在理解得太少之后,似乎我无法使其适应我的应用程序的制作方式。

我有几个数据访问类,都专注于区域的方法。例如,一个数据访问用于客户相关的呼叫,一个用于与产品相关的呼叫等。

但由于它们都使用相同的服务,它们都来自一个保存引用的基础数据访问类。

这是基础数据访问类:

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。 已从构造函数中删除服务的实例化。该应用程序工作正常,无需进一步修改。 这还够吗?

0 个答案:

没有答案
相关问题