WCF应用。结构图Objectfactory丢失Object的实例

时间:2012-02-29 23:29:51

标签: wcf structuremap

我正在尝试使用Structuremap来使用WCF。我从Jimmy Bogard的博客中获取了大部分代码 Integrating StructureMap with WCF

我正在重复使用为ASP.nET MVC应用程序创建的所有对象(Domain,NHIBERNATE,Service层)。我有一个单独的IoC库,我在其中引导所有注册表

我添加了global.asax,在Application Start Event中这就是我所拥有的:

var webServiceRegistry = new WebServiceRegistry();
var bootStrapper = new IoC.BootStrapper();
bootStrapper.Start();

WebServiceRegistry是一个普通类,在其构造函数中我有以下内容:

 var appUser = new AppUser
            {
                Role = userDto.Role,
                Email = userDto.Email,
                Id = userDto.Id,
                CompanyName = dealerDto.Name,
                Name = userDto.Name,
                IsImpersonated = false,
                Impersonater = Guid.Empty
            };
            System.Threading.Thread.CurrentPrincipal = System.Web.HttpContext.Current.User = appUser;
            ObjectFactory.Configure(x => x.For<IAppUser>().Use(appUser));

一切顺利 - 没有错误 <击> 当我的wcf服务类被实例化时,事情开始失败,它在构造函数中有一个带有202错误的Domainservice对象(IDomainServiceObject domainServiceObject)。

失败发生在DomainService Registry中,它无法获得 IAppUser 的默认实例

修改

最初的问题是我在StructureMapServiceHost的构造函数中遇到错误。

现在我没有得到任何错误,但我没有得到我注入的IAppUser实例。代替它,它创建了一个新的AppUser实例。

有没有人知道为什么会这样?

我尝试过添加HttpContextScope,HybridHttpOrThreadLocalScoped但结果仍然相同

我已经确认当时在引导程序中调用所有注册表的IAppUser实例是一次提供了其中的值(而不是由StructureMap创建) 在“应用程序启动”中,它会创建整个图形。

一旦进入webmethod,ILMSUser就是一个新实例。

谢谢

1 个答案:

答案 0 :(得分:0)

因为每次你都可以使用单身

时想要回到同一个对象
this.For<IAppUser>().Singleton().Use(appUser);