EF DbContext 的 WCF 自定义 OperationContext Lifetimemanager
WCF Custom OperationConext Lifetimemanager for EF DbContext
container.RegisterType<IDataContextFactory<MyDataContext>, DefaultDataContextFactory<MyDataContext>>(new PerRequestLifetimeManager());
使用 OperationContext 创建了一个 PerRequestLifetimeManager,但它似乎根本没有调用 setValue 函数,它总是尝试转到 GetValue() 函数,该函数总是返回 null,因为没有设置任何内容。
我的目标是为 dbconetxt 创建一个 lifetimeManager,它将为我的每个方法调用提供一个新的 dbContext。 transient 不是一个选项,因为它不会;不适用于连接查询。
public class WcfOperationContext : IExtension<OperationContext>
{
private readonly IDictionary<string, object> items;
private WcfOperationContext()
{
items = new Dictionary<string, object>();
}
public IDictionary<string, object> Items
{
get { return items; }
}
public static WcfOperationContext Current
{
get
{
WcfOperationContext context = OperationContext.Current.Extensions.Find<WcfOperationContext>();
if (context == null)
{
context = new WcfOperationContext();
OperationContext.Current.Extensions.Add(context);
}
return context;
}
}
public void Attach(OperationContext owner) { }
public void Detach(OperationContext owner) { }
}
public class PerRequestLifetimeManager : LifetimeManager
{
private string key;
public PerRequestLifetimeManager()
{
key = Guid.NewGuid().ToString();
}
public override object GetValue()
{
if (WcfOperationContext.Current == null)
{
return null;
}
else
{
return WcfOperationContext.Current.Items[key];
}
}
public override void RemoveValue()
{
if (WcfOperationContext.Current != null)
{
WcfOperationContext.Current.Items.Remove(key);
}
}
public override void SetValue(object newValue)
{
if (WcfOperationContext.Current != null)
{
WcfOperationContext.Current.Items.Add(key, newValue);
}
}
}
我的解决方案是使用这个 nuget 包:UnityWCF
服务应由 Unity 实例化,每次调用新实例。
为此,请在服务上使用此设置:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ...
在需要的地方注入 DbContext。并像这样在 Unity 中注册:
container.RegisterType<DbContext, YourDbContext>(new HierarchicalLifetimeManager(), ...);
container.RegisterType<IDataContextFactory<MyDataContext>, DefaultDataContextFactory<MyDataContext>>(new PerRequestLifetimeManager());
使用 OperationContext 创建了一个 PerRequestLifetimeManager,但它似乎根本没有调用 setValue 函数,它总是尝试转到 GetValue() 函数,该函数总是返回 null,因为没有设置任何内容。
我的目标是为 dbconetxt 创建一个 lifetimeManager,它将为我的每个方法调用提供一个新的 dbContext。 transient 不是一个选项,因为它不会;不适用于连接查询。
public class WcfOperationContext : IExtension<OperationContext>
{
private readonly IDictionary<string, object> items;
private WcfOperationContext()
{
items = new Dictionary<string, object>();
}
public IDictionary<string, object> Items
{
get { return items; }
}
public static WcfOperationContext Current
{
get
{
WcfOperationContext context = OperationContext.Current.Extensions.Find<WcfOperationContext>();
if (context == null)
{
context = new WcfOperationContext();
OperationContext.Current.Extensions.Add(context);
}
return context;
}
}
public void Attach(OperationContext owner) { }
public void Detach(OperationContext owner) { }
}
public class PerRequestLifetimeManager : LifetimeManager
{
private string key;
public PerRequestLifetimeManager()
{
key = Guid.NewGuid().ToString();
}
public override object GetValue()
{
if (WcfOperationContext.Current == null)
{
return null;
}
else
{
return WcfOperationContext.Current.Items[key];
}
}
public override void RemoveValue()
{
if (WcfOperationContext.Current != null)
{
WcfOperationContext.Current.Items.Remove(key);
}
}
public override void SetValue(object newValue)
{
if (WcfOperationContext.Current != null)
{
WcfOperationContext.Current.Items.Add(key, newValue);
}
}
}
我的解决方案是使用这个 nuget 包:UnityWCF
服务应由 Unity 实例化,每次调用新实例。
为此,请在服务上使用此设置:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ...
在需要的地方注入 DbContext。并像这样在 Unity 中注册:
container.RegisterType<DbContext, YourDbContext>(new HierarchicalLifetimeManager(), ...);