StructureMap异常代码:202

时间:2011-12-24 01:01:41

标签: asp.net-mvc-3 nhibernate structuremap

大家,我在使用MVC3代码时遇到的问题如下

 public SystemController(IRepository repository)
            :this
        (
            repository,
            new AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider),
                new AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider),
                new AspNetRoleProviderWrapper(Roles.Provider),
            new SmtpClientProxy(new SmtpClient(Utils.Setting.EmailServer,
                                               int.Parse(Utils.Setting.EmailPort))
                                    {
                                        EnableSsl = true,
                                        UseDefaultCredentials = true,
                                        Credentials = new NetworkCredential(Utils.Setting.EmailAccount,
                                                                            Utils.Setting.EmailPassword),
                                        DeliveryMethod = SmtpDeliveryMethod.Network
                                    })
       ){}



        public SystemController(IRepository repository,
            IUserService userService,
            IPasswordService passwordService,
            IRolesService rolesService,
            ISmtpClient smtpClient)
            : base(repository)
        {
            _userService = userService;
            _passwordService = passwordService;
            _rolesService = rolesService;
            _smtpClient = smtpClient;
        }

public class SmtpClientProxy : ISmtpClient
    {
        private readonly SmtpClient _smtpClient;

        public SmtpClientProxy(SmtpClient smtpClient)
        {
            _smtpClient = smtpClient;
        }

        #region ISmtpClient Members

        public void Send(MailMessage mailMessage)
        {
            _smtpClient.Send(mailMessage);
        }

        #endregion
    }

 ObjectFactory.Initialize(x =>
                                 {
                                     x.Scan(scanner =>
                                                {
                                                    scanner.TheCallingAssembly();
                                                    scanner.WithDefaultConventions();
                                                });
                                     x.For<ISessionFactory>()
                                         .Singleton()
                                         .Use(GetSessionFactory());
                                     x.For<ISession>()
                                         .HybridHttpOrThreadLocalScoped()
                                         .Use(y => y.GetInstance<ISessionFactory>().OpenSession());
                                     x.For<IUserService>()
                                         .Use<AspNetMembershipProviderWrapper>();
                                     x.For<IPasswordService>()
                                         .Use<AspNetMembershipProviderWrapper>();
                                     x.For<IPasswordService>()
                                         .Use<AspNetMembershipProviderWrapper>();
                                     x.For<IRolesService>()
                                         .Use<AspNetRoleProviderWrapper>();
                                     x.For<ISmtpClient>()
                                         .Use<SmtpClientProxy>().Ctor<SmtpClient>();
                                     x.For<MembershipProvider>()
                                         .Use(System.Web.Security.Membership.Provider);
                                     x.For<RoleProvider>()
                                       .Use(Roles.Provider);
                                 });

错误信息:

StructureMap异常代码:202 没有为PluginFamily定义的默认实例System.Net.Mail.SmtpClient,System,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089


我认为问题在于:

x.For <ISmtpClient> ()
. Use <SmtpClientProxy> (). Ctor <SmtpClient> ();

我问你怎么写呢?

1 个答案:

答案 0 :(得分:2)

您的SmtpClientProxy类在其构造函数中需要SmtpClient类。您没有为SmtpClient注册任何内容。

尝试将此添加到您的注册中:

x.For<SmtpClient>().Use<SmtpClient>(); 

这假设SmtpClient不在构造函数中使用依赖项。如果是这样,您可能会收到错误,其中一个依赖项未在默认实现中注册。

或者,您可以将构造函数代码更改为此(没有构造函数依赖项):

private readonly SmtpClient _smtpClient = new SmtpClient();  

public SmtpClientProxy()  
{  
}  

不知道你想要做什么,很难明确回答。