【问题标题】:using statement inside a WCF Service在 WCF 服务中使用语句
【发布时间】:2013-08-17 21:44:00
【问题描述】:

我正在使用 NHibernate 连接到我的数据库并检索一些数据,如下所示:

public abstract class BaseContext<TContext> : IBaseContext<TContext> where TContext : IGuid
{
    #region Data Members

    // NHibernate session
    private readonly Lazy<ISession> _session;

    // Logger
    private static readonly ILog log = LogManager.GetLogger(typeof(BaseContext<TContext>));

    #endregion

    #region Ctor

    protected BaseContext()
    {

        // Initialize session 
        _session = new Lazy<ISession>(NHibernateHelper.OpenSession);
        // log
        log.Debug("Session has been created but has not yet been used.");
    }

    #endregion

    #region Propreties

    /// <summary>
    /// Lazy load a session with NHibernate
    /// </summary>
    public ISession Session
    {
        get { return _session.Value; }
    }

    #endregion

    #region Methods

    /// <summary>
    /// Retreives all object of type <see cref="TContext"/> from the database.
    /// </summary>
    /// <returns>A list of all the <see cref="TContext"/> items</returns>
    public IEnumerable<TContext> Get()
    {
        try
        {
            log.DebugFormat("Retrieving all items of type {0} from the database.",
                    typeof(TContext).Name);

            // Return all the items of TContext type
            return from item in Session.Query<TContext>()
                   select item;
        }
        catch (Exception ex)
        {
            log.Error("Could not retreive items from the database.", ex);
            return default(IEnumerable<TContext>); 
        }
    }

    /// <summary>
    /// Disposes the context
    /// </summary>
    public void Dispose()
    {
        // Dispose session
        Session.Dispose();
    }

    #endregion
}

我有一个包装类,代表我想要检索的每个实体,例如:

public class EntityContext : BaseContext<DataEntity>
{
    #region Methods

    /// <summary>
    /// Gets a list of all enitities
    /// </summary>
    /// <returns>A list of all entities</returns>
    public new IEnumerable<DataEntity> Get()
    {
        return base.Get();
    }

    #endregion
}

为了公开这一点,我创建了一个使用它的 WCF 服务:

    public List<DataEntity> Get()
    {
        using (EntityContext context = new EntityContext())
        {
            var entities = context.Get();
            return entities.ToList();
        }
    }

当我使用 WCF 服务时,我不断收到“连接中止”异常,直到我找出原因。当我删除 using 语句(对 Dispose 方法的调用)时,它工作正常。

我的问题是为什么?我应该如何正确实施?

谢谢, 暗里

【问题讨论】:

  • 如果你return entities;,我可以看到这个问题是如何发生的。但是你确定即使你return entities.ToList();也有这个问题?
  • @ErenErsönmez 是的,这正是我不明白的......
  • ToList() 只会强制加载您的 root 实体,但不会强制加载它们的惰性属性。这些是稍后加载的,当第一次访问时(延迟加载)。在那一刻, Session 被处理了......没有懒惰。这就是为什么你需要一个“请求持久”的会话
  • @RadimKöhler 你是对的,我让我的所有映射都不是延迟加载,只是为了检查它是否有效,它确实有效。所以现在我的问题是,如何创建“请求持久”会话?您的答案中的示例使用了我不想使用的 IOC 容器。你有没有一个例子吗?
  • 你是对的......我已经通过一些链接扩展了答案。 IoC 是你无论如何都应该开始考虑的东西 ;) 但是,WcfOperationSessionContext 是开箱即用的!

标签: c# wcf nhibernate


【解决方案1】:

您需要的是 Session 生命周期,它将与 WCF Service 的请求相关。这就是你应该用谷歌搜索的。

这里有一些链接,如何:

请求持续会话:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 2012-07-02
    • 1970-01-01
    相关资源
    最近更新 更多