【发布时间】:2015-01-21 08:47:04
【问题描述】:
我有许多被 angularjs 使用的 WCF 服务设置。目前我遇到了一个我正在努力解决的问题。调用 WCF 服务时,我不断收到以下错误消息。任何有关如何解决该问题的指示将不胜感激。更详细的错误信息可以在底部找到
无法解析实例,也无法从此 LifetimeScope 创建嵌套生命周期,因为它已被释放。
编辑:当我在 global.asax Application_BeginReques() 中构建容器时,它似乎也有效,因此不仅仅是 Application_Start。不知道为什么,但至少这比以前好....但是我以前偶尔会出现这个错误,所以我不相信这个丑陋的解决方法
这是我的 global.asax
..
..
protected void Application_Start(object sender, EventArgs e)
{
var containerBuilder = new IoC();
// Register your service implementations.
containerBuilder.Builder.RegisterType<Konstrukt.SL.Services.GetBudgetData>().Named<object>("GetBudgetDataService");
containerBuilder.Builder.RegisterType<Konstrukt.SL.Services.GetReferenceData>().Named<object>("GetReferenceDataService");
containerBuilder.Builder.RegisterType<Konstrukt.SL.Services.UpdateBudgetData>().Named<object>("UpdateBudgetDataService");
..
..
containerBuilder.Builder.RegisterType<Konstrukt.SL.Services.DimValue>().Named<object>("DimValueService");
containerBuilder.Builder.RegisterType<Konstrukt.SL.Services.DisplayNameMapping>().Named<object>("DisplayNameMappingService");
..
..
// use only one context object per scope/request
containerBuilder.Builder.RegisterType<Konstrukt.DAL.KonstruktEntities>().InstancePerLifetimeScope();
AutofacHostFactory.Container = containerBuilder.Builder.Build();
}
这是我的 IoC 类,它注册了所有接口
public class IoC
{
private ContainerBuilder _builder;
public ContainerBuilder Builder
{
get
{
if (_builder == null)
{
_builder = new ContainerBuilder();
_builder.RegisterType<BL.App.App>().As<BL.App.IApp>();
_builder.RegisterType<BL.AppConfig.AppConfig>().As<BL.AppConfig.IAppConfig>();
_builder.RegisterType<BL.AppConfig.ComponentSetting>().As<BL.AppConfig.IComponentSetting>();
_builder.RegisterType<BL.AppConfig.Component>().As<BL.AppConfig.IComponent>();
_builder.RegisterType<BL.AppConfig.AppSetting>().As<BL.AppConfig.IAppSetting>();
_builder.RegisterType<BL.AppConfig.AppDataSet>().As<BL.AppConfig.IAppDataSet>();
_builder.RegisterType<BL.AppConfig.AttributeData>().As<BL.AppConfig.IAttributeData>();
_builder.RegisterType<BL.AppConfig.Constants>().As<BL.AppConfig.IConstants>();
..
..
_builder.RegisterType<BL.DimensionData.DisplayNameMapping>().As<BL.DimensionData.IDisplayNameMapping>();
..
..
//DAL registrations, etc
..
..
}
return _builder;
}
set { _builder = value; }
}
}
示例 WCF 服务实现
public class DimValue : IDimValue
{
private BL.DimensionData.IDimValue _dimValue;
public DimValue(ILifetimeScope container, BL.DimensionData.IDimValue dimValue)
{
AutofacHostFactory.Container = container;
_dimValue = dimValue;
}
public IList<Shared.Poco.DimValue> GetDimValueList(int budgetId, string dimensionFilterJSON, string userId)
{
Dictionary<Shared.Poco.Dimension, List<string>> dimensionFilters = null;
Shared.Poco.User user = new Shared.Poco.User { UserId = userId };
IList<Shared.Poco.DimValue> list = new List<Shared.Poco.DimValue>();
using (var scope = AutofacHostFactory.Container.BeginLifetimeScope())
{
dimensionFilters = JsonConvert.DeserializeObject<Dictionary<Shared.Poco.Dimension, List<string>>>(dimensionFilterJSON);
foreach (KeyValuePair<Shared.Poco.Dimension, List<string>> kvp in dimensionFilters)
{
foreach (Shared.Poco.DimValue dv in _dimValue.GetDimValueList(budgetId, kvp.Key, kvp.Value, user))
{
list.Add(dv);
}
}
}
return list;
}
}
[ServiceContract]
public interface IDimValue
{
[OperationContract]
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "GetDimValueList?budgetId={budgetId}&dimensionFilterJSON={dimensionFilterJSON}&userId={userId}")]
IList<Shared.Poco.DimValue> GetDimValueList(int budgetId, string dimensionFilterJSON, string userId);
}
服务标记
<%@ ServiceHost Language="C#"
Debug="true"
Service="DimValueService"
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"
CodeBehind="DimValue.svc.cs" %>
调用 DimValue.svc/GetDimValueList 时的详细错误
服务器在处理请求时遇到错误。异常消息是“无法解析实例,并且无法从此 LifetimeScope 创建嵌套生命周期,因为它已被释放。”。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是:在 Autofac.Core.Lifetime.LifetimeScope.CheckNotDisposed() 在 Autofac.Core.Lifetime.LifetimeScope.BeginLifetimeScope(Object tag) 在 Autofac.Core.Lifetime.LifetimeScope.BeginLifetimeScope() 在 Autofac.Integration.Wcf .AutofacInstanceContext..ctor(ILifetimeScope container) 在 Autofac.Integration.Wcf.AutofacInstanceProvider.GetInstance(InstanceContext instanceContext, Message message) 在 System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance(InstanceContext instanceContext, Message request) 在 System.ServiceModel.InstanceContext。 GetServiceInstance(Message message) 在 System.ServiceModel.Dispatcher.InstanceBehavior.EnsureServiceInstance(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) 在 System。 System.ServiceModel.Dispatch 中的 ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) er.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) ) 在 System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
【问题讨论】: