Problem with Structure Map in MVC 3 Beta. Please, need help. Urgent if possible. Thank You.

463 views
Skip to first unread message

shapper

unread,
Oct 30, 2010, 10:05:01 AM10/30/10
to structuremap-users
Hello,

I am using StructureMap with MVC 3 Beta which had a change:
IMvcServiceLocator to IDependencyResolver.

I had this working with ASP.NET MVC 3 Preview 1 that used
IMvcServiceLocator.

To create the StructureMapDependencyResolver I followed an example for
Unity IOC:
http://blogs.microsoft.co.il/blogs/gilf/archive/2010/10/17/dependency-injection-in-mvc-3-was-made-easier.aspx

So I ended up with the following:

public class StructureMapDependencyResolver : IDependencyResolver {

private static IContainer _container;

public StructureMapDependencyResolver(IContainer container) {
_container = container;
_container.Configure(x => x.Scan(y => {
y.AssembliesFromApplicationBaseDirectory();
y.WithDefaultConventions();
y.AddAllTypesOf<IController>().NameBy(z =>
z.Name.Replace("Controller", "").ToLower());
y.LookForRegistries();
}));

} // ContainerResolver

public Object GetService(Type serviceType) {
return _container.TryGetInstance(serviceType);
} // GetService

public IEnumerable<Object> GetServices(Type serviceType) {
return _container.GetAllInstances<Object>().Where(s =>
s.GetType() == serviceType);
} // GetServices

} // StructureMapDependencyResolver

Then on my Global.asax Application_Start I have the following:

ObjectFactory.Initialize(o => {
o.AddRegistry(new StructureMapRegistry());
});

DependencyResolver.SetResolver(new
StructureMapDependencyResolver(ObjectFactory.Container));

My StructureMapRegistry is the following:

public class StructureMapRegistry : Registry {
public StructureMapRegistry() {
For<IContext>().HttpContextScoped().Use(() => new Context());
Scan(o => {
o.AssemblyContainingType(typeof(IService));
o.AddAllTypesOf(typeof(IService));
o.WithDefaultConventions();
});
} // StructureMapRegistry
}

On my controller I have:

public partial class AnalysisController : Controller {
private readonly IAnalysisService _analysisService;

public AnalysisController(IAnalysisService analysisService) {
_analysisService = analysisService;
} // AnalysisController
}

I keep getting the error:
Exception Details: System.MissingMethodException: No parameterless
constructor defined for this object.

It is like the service is not being injected.

There is also the Controller Activator but it is not necessary to
implement it.
http://blogs.microsoft.co.il/blogs/gilf/archive/2010/10/17/using-the-controlleractivator-in-mvc-3.aspx

I got this answer from a MVC team members:
"You are not required to implement this interface if your DI container
can create arbitrary objects (for example, Unity can do this), since
our fallback strategy is to ask the Dependency Resolver to create the
controller if there is no implementation of IControllerActivator."

I don't know what else to try.

Does anyone knows what am I missing?,

Thank You,
Miguel

Elliott O'Hara

unread,
Oct 30, 2010, 10:14:46 AM10/30/10
to structure...@googlegroups.com
  public IEnumerable<Object> GetServices(Type serviceType) {
     return _container.GetAllInstances<Object>().Where(s =>
s.GetType() == serviceType);
   } // GetServices

errrr??? huh?

_container.GetAllInstances(serviceType) is what you mean, right?


--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To post to this group, send email to structure...@googlegroups.com.
To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/structuremap-users?hl=en.


shapper

unread,
Oct 30, 2010, 10:23:35 AM10/30/10
to structuremap-users
On Oct 30, 3:14 pm, "Elliott O'Hara" <elliott.oh...@gmail.com> wrote:
>   public IEnumerable<Object> GetServices(Type serviceType) {
>      return _container.GetAllInstances<Object>().Where(s =>
> s.GetType() == serviceType);
>    } // GetServices
>
> errrr??? huh?
>
> _container.GetAllInstances(serviceType) is what you mean, right?

With that I get the error:
Cannot implicitly convert type 'System.Collections.IList' to
'System.Collections.Generic.IEnumerable<object>'. An explicit
conversion exists (are you missing a cast?)

Ins't what I have the same?

Thank You,
Miguel

>
>
>
>
>
>
>
> On Sat, Oct 30, 2010 at 9:05 AM, shapper <mdmo...@gmail.com> wrote:
> > Hello,
>
> > I am using StructureMap with MVC 3 Beta which had a change:
> > IMvcServiceLocator to IDependencyResolver.
>
> > I had this working with ASP.NET MVC 3 Preview 1 that used
> > IMvcServiceLocator.
>
> > To create the StructureMapDependencyResolver I followed an example for
> > Unity IOC:
>
> >http://blogs.microsoft.co.il/blogs/gilf/archive/2010/10/17/dependency...
> >http://blogs.microsoft.co.il/blogs/gilf/archive/2010/10/17/using-the-...
>
> > I got this answer from a MVC team members:
> > "You are not required to implement this interface if your DI container
> > can create arbitrary objects (for example, Unity can do this), since
> > our fallback strategy is to ask the Dependency Resolver to create the
> > controller if there is no implementation of IControllerActivator."
>
> > I don't know what else to try.
>
> > Does anyone knows what am I missing?,
>
> > Thank You,
> > Miguel
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "structuremap-users" group.
> > To post to this group, send email to structure...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > structuremap-us...@googlegroups.com<structuremap-users%2Bunsubs cr...@googlegroups.com>
> > .

Jeremy D. Miller

unread,
Oct 30, 2010, 11:53:11 AM10/30/10
to structure...@googlegroups.com
What Elliot said is correct.

Your call to _container.GetAllInstances<Object>() is going to return an empty array because that just isn't how StructureMap works.  That code is trying to resolve everything that is explicitly registered as an "object," which is very likely nothing.  StructureMap requires you to *explicitly* register types and configurations against a targeted "PluginType".

You need to be calling to the container as container.GetAllInstances(     serviceType   ).
 
Jeremy D. Miller
The Shade Tree Developer
jeremy...@yahoo.com



From: shapper <mdm...@gmail.com>
To: structuremap-users <structure...@googlegroups.com>
Sent: Sat, October 30, 2010 9:23:35 AM
Subject: [sm-users] Re: Problem with Structure Map in MVC 3 Beta. Please, need help. Urgent if possible. Thank You.
> > structuremap-users+unsub...@googlegroups.com<structuremap-users%2Bunsubs cr...@googlegroups.com>

> > .
> > For more options, visit this group at
> >http://groups.google.com/group/structuremap-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To post to this group, send email to structure...@googlegroups.com.
To unsubscribe from this group, send email to structuremap-users+unsub...@googlegroups.com.

Elliott O'Hara

unread,
Oct 30, 2010, 12:27:10 PM10/30/10
to structure...@googlegroups.com
Also,
I'm not familiar with MVC 3 yet, but it sounds like setting the instance of IDependencyResolver isn't the same as setting a ControllerFactory. The blog is using a Dependency attribute instead of just injecting the dependency (looks like a lot of yucky to me).  Note that on the blog you linked to there is no ctor on the controller  and the dependency has a setter. 

For poops and giggles, mark the service with that Dependency attribute and see if the no default ctor error goes away.

If it does, call someone at MS bad names, then go use a Controller factory :)

/E


To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.

Elliott O'Hara

unread,
Oct 30, 2010, 12:28:05 PM10/30/10
to structure...@googlegroups.com
If anyone is familiar with MVC3 IDependencyResolver and knows that I'm wrong, please speak up! :)

shapper

unread,
Oct 30, 2010, 12:59:40 PM10/30/10
to structuremap-users
Still lost. I have been reading some info on IOC from a MVC team
member:
http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html

And here:
http://bradwilson.typepad.com/blog/2010/10/service-location-pt10-controller-activator.html

When I asked if I needed to implement the controller activator I got
the following answer:
"You are not required to implement this interface if your DI container
can create arbitrary objects (for example, Unity can do this), since
our fallback strategy is to ask the Dependency Resolver to create the
controller if there is no implementation of IControllerActivator."

Have no idea what arbitrary objects are.

In MVC 3 Preview 1 I was using the "old" IMVCServiceLocator replaced
now by IDependencyResolver:

public class ServiceLocator : IMvcServiceLocator {

const String HttpContextKey =
"__StructureMapServiceLocator_Container";
private readonly IMvcServiceLocator _default;
private readonly IContainer _container;

protected Container Container {
get {
if (!HttpContext.Current.Items.Contains(HttpContextKey)) {
HttpContext.Current.Items.Add(HttpContextKey, Container);
}
return (Container)HttpContext.Current.Items[HttpContextKey];
}
}

public ServiceLocator() {

ServiceLocator locator = MvcServiceLocator.Current as
ServiceLocator;
if (locator != null)
_container = locator.Container;

_default = MvcServiceLocator.Default;

} // ServiceLocator

public ServiceLocator(IContainer container)
: this(container, MvcServiceLocator.Default) {
} // ServiceLocator

public ServiceLocator(IContainer container, IMvcServiceLocator
locator) {
_container = container;
_default = locator;
} // ServiceLocator

public void Release(Object instance) {

if (instance != null)
GC.SuppressFinalize(instance);

} // Release

private object Resolve(Type type, String key = null) {

Object instance = _container.GetInstance(type);
if (instance != null)
return instance;

Object defaultInstance = _default.GetInstance(type, key);
if (defaultInstance != null)
return defaultInstance;

throw new ActivationException(String.Format("Could not resolve
service type {0}.", type.FullName));

} // Resolve

private IEnumerable<Object> ResolveAll(Type type) {

IEnumerable<Object> instances =
_container.GetAllInstances(type).Cast<IEnumerable<Object>>();
if (instances.Count() > 0)
return instances;

var defaultInstances = _default.GetAllInstances(type);
if (defaultInstances != null)
return defaultInstances;

throw new ActivationException(String.Format("Could not resolve
service type {0}.", type.FullName));

} // ResolveAll

public IEnumerable<Object> GetAllInstances(Type serviceType) {
return ResolveAll(serviceType);
} // GetAllInstances

public IEnumerable<TService> GetAllInstances<TService>() {
IEnumerable<Object> instances = ResolveAll(typeof(TService));
return from TService instance in instances select instance;
} // GetAllInstances

public Object GetInstance(Type type, string key) {
return Resolve(type, key);
} // GetInstance

public object GetInstance(Type type) {
return Resolve(type);
} // GetInstance

public TService GetInstance<TService>(String key) {
return (TService)Resolve(typeof(TService), key);
} // GetInstance

public TService GetInstance<TService>() {
return (TService)Resolve(typeof(TService));
} // GetInstance

public Object GetService(Type type) {
return Resolve(type);
} // GetService

} // ServiceLocator

And a ControllerFactory which I think now is not needed because of
ControllerActivador:

public class StructureMapControllerFactory : IControllerFactory {

private readonly IContainer _container;
private readonly IControllerFactory _factory;

public StructureMapControllerFactory(IContainer container)
: this(container, new DefaultControllerFactory()) {
} // StructureMapControllerFactory

public StructureMapControllerFactory(IContainer container,
IControllerFactory factory) {

_container = container;
_factory = factory;

} // StructureMapControllerFactory

public IController CreateController(RequestContext context, String
name) {

try {
return
_container.GetInstance<IController>(name.ToLowerInvariant());
} catch (Exception) {
return _factory.CreateController(context, name);
}

} // CreateController

public void ReleaseController(IController controller) {

GC.SuppressFinalize(controller);

} // ReleaseController

} // ControllerFactory


These are the Release Notes for MVC 3 Beta that concerns IOC:

-------------------------

Building on the ASP.NET MVC 3 Preview 1 release, the current release
includes added support for two new services and four existing
services, and improved support for dependency resolution and the
Common Service Locator. New IControllerActivator Interface for Fine-
Grained Controller Instantiation

The new IControllerActivator interface provides more fine-grained
control over how controllers are instantiated via dependency
injection. The following example shows the interface:

namespace System.Web.Mvc {
using System.Web.Routing;

public interface IControllerActivator {
IController Create(RequestContext requestContext, Type
controllerType);
}
}

Contrast this to the role of the controller factory. A controller
factory is an implementation of the IControllerFactory interface,
which is responsible both for locating a controller type and for
instantiating an instance of that controller type.

Controller activators are responsible only for instantiating an
instance of a controller type. They do not perform the controller type
lookup. After locating a proper controller type, controller factories
should delegate to an instance of IControllerActivator to handle the
actual instantiation of the controller.

The DefaultControllerFactory class has a new constructor that accepts
an IControllerFactory instance. This lets you apply Dependency
Injection to manage this aspect of controller creation without having
to override the default controller-type lookup behavior.

IServiceLocator Interface Replaced with IDependencyResolver

Based on community feedback, the ASP.NET MVC 3 Beta release has
replaced the use of the IServiceLocator interface with a slimmed-down
IDependencyResolver interface specific to the needs of ASP.NET MVC.
The following example shows the new interface:

namespace System.Web.Mvc {
using System.Collections.Generic;

public interface IDependencyResolver {
object GetService(Type serviceType);
IEnumerable<object> GetServices(Type serviceType);
}
}
As part of this change, the ServiceLocator class was also replaced
with the DependencyResolver class. Registration of a dependency
resolver is similar to earlier versions of ASP.NET MVC:

DependencyResolver.SetResolver(myResolver);
Implementations of this interface should simply delegate to the
underlying dependency injection container to provide the registered
service for the requested type.

When there are no registered services of the requested type, ASP.NET
MVC expects implementations of this interface to return null from
GetService and to return an empty collection from GetServices.

-------------------------

I have been trying to find a source example with StructureMap 3 and
MVC 3 Beta and no luck.

Does this info helps?

Sorry, I am completely lost on this.



On Oct 30, 5:28 pm, "Elliott O'Hara" <elliott.oh...@gmail.com> wrote:
> If anyone is familiar with MVC3 IDependencyResolver and knows that I'm
> wrong, please speak up! :)
>
> On Sat, Oct 30, 2010 at 11:27 AM, Elliott O'Hara <elliott.oh...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Also,
> > I'm not familiar with MVC 3 yet, but it sounds like setting the instance of
> > IDependencyResolver isn't the same as setting a ControllerFactory. The blog
> > is using a Dependency attribute instead of just injecting the dependency
> > (looks like a lot of yucky to me).  Note that on the blog you linked to
> > there is no ctor on the controller  and the dependency has a setter.
>
> > For poops and giggles, mark the service with that Dependency attribute and
> > see if the no default ctor error goes away.
>
> > If it does, call someone at MS bad names, then go use a Controller factory
> > :)
>
> > /E
>
> > On Sat, Oct 30, 2010 at 10:53 AM, Jeremy D. Miller <
> > jeremydmil...@yahoo.com> wrote:
>
> >> What Elliot said is correct.
>
> >> Your call to _container.GetAllInstances<Object>() is going to return an
> >> empty array because that just isn't how StructureMap works.  That code is
> >> trying to resolve everything that is explicitly registered as an "object,"
> >> which is very likely nothing.  StructureMap requires you to *explicitly*
> >> register types and configurations against a targeted "PluginType".
>
> >> You need to be calling to the container as container.GetAllInstances(
> >> serviceType   ).
>
> >> Jeremy D. Miller
> >> The Shade Tree Developer <http://codebetter.com/blogs/jeremy.miller>
> >> jeremydmil...@yahoo.com
>
> >> ------------------------------
> >> *From:* shapper <mdmo...@gmail.com>
> >> *To:* structuremap-users <structure...@googlegroups.com>
> >> *Sent:* Sat, October 30, 2010 9:23:35 AM
> >> *Subject:* [sm-users] Re: Problem with Structure Map in MVC 3 Beta.
> >> > > structuremap-us...@googlegroups.com<structuremap-users%2Bunsubs
> >> cr...@googlegroups.com>
> >> > > .
> >> > > For more options, visit this group at
> >> > >http://groups.google.com/group/structuremap-users?hl=en.
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "structuremap-users" group.
> >> To post to this group, send email to structure...@googlegroups.com.
> >> To unsubscribe from this group, send email to structuremap-users+
> >> unsub...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/structuremap-users?hl=en.
>
> >>  --
> >> You received this message because you are subscribed to the Google Groups
> >> "structuremap-users" group.
> >> To post to this group, send email to structure...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> structuremap-us...@googlegroups.com<structuremap-users%2Bunsubs cr...@googlegroups.com>

shapper

unread,
Oct 30, 2010, 1:01:28 PM10/30/10
to structuremap-users
On Oct 30, 5:27 pm, "Elliott O'Hara" <elliott.oh...@gmail.com> wrote:
> Also,
> I'm not familiar with MVC 3 yet, but it sounds like setting the instance of
> IDependencyResolver isn't the same as setting a ControllerFactory. The blog
> is using a Dependency attribute instead of just injecting the dependency
> (looks like a lot of yucky to me).  Note that on the blog you linked to
> there is no ctor on the controller  and the dependency has a setter.
>
> For poops and giggles, mark the service with that Dependency attribute and
> see if the no default ctor error goes away.

I think the Dependency attribute is from Unity. Not?
It is the MSFT IOC library ... I prefer StructureMap so ...

>
> If it does, call someone at MS bad names, then go use a Controller factory
> :)

Yes, I did ... :-)

But they changed everything so I would like to follow the new
approach.
Never knows what is coming down the road.

>
> /E
>
> On Sat, Oct 30, 2010 at 10:53 AM, Jeremy D. Miller
> <jeremydmil...@yahoo.com>wrote:
>
>
>
>
>
>
>
> > What Elliot said is correct.
>
> > Your call to _container.GetAllInstances<Object>() is going to return an
> > empty array because that just isn't how StructureMap works.  That code is
> > trying to resolve everything that is explicitly registered as an "object,"
> > which is very likely nothing.  StructureMap requires you to *explicitly*
> > register types and configurations against a targeted "PluginType".
>
> > You need to be calling to the container as container.GetAllInstances(
> > serviceType   ).
>
> > Jeremy D. Miller
> > The Shade Tree Developer <http://codebetter.com/blogs/jeremy.miller>
> > jeremydmil...@yahoo.com
>
> > ------------------------------
> > *From:* shapper <mdmo...@gmail.com>
> > *To:* structuremap-users <structure...@googlegroups.com>
> > *Sent:* Sat, October 30, 2010 9:23:35 AM
> > *Subject:* [sm-users] Re: Problem with Structure Map in MVC 3 Beta.
> > > > structuremap-us...@googlegroups.com<structuremap-users%2Bunsubs
> > cr...@googlegroups.com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/structuremap-users?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "structuremap-users" group.
> > To post to this group, send email to structure...@googlegroups.com.
> > To unsubscribe from this group, send email to structuremap-users+
> > unsub...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/structuremap-users?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "structuremap-users" group.
> > To post to this group, send email to structure...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > structuremap-us...@googlegroups.com<structuremap-users%2Bunsubs cr...@googlegroups.com>

Elliott O'Hara

unread,
Oct 30, 2010, 1:19:06 PM10/30/10
to structure...@googlegroups.com
"Developers who previously implemented IControllerFactory by deriving from DefaultControllerFactory just to override the GetControllerInstance method for dependency injection purposes should now implement IControllerActivator instead."
 
Totally guessing, but I think if you implement that the same way we did a SM ControllerFactory, you'll be golden. 
It looks like the issue is your dependencyresolver class is looking for a IControllerActivator, since it doesn't find one it's just trying to create an instance of the Controller (default ctor), you'd think that it would just use the DependencyResolver it has and relegate the construction of a controller to the it, but I guess not :)




To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.

shapper

unread,
Oct 30, 2010, 5:16:08 PM10/30/10
to structuremap-users
I was able to solve it! Finally ...

I am posting my entire code. Please, let me know any tips to improve
it.

I am using StructureMap in a MVC 3 Beta project with Fluent Validation
and AutoMapper and EF4.

StructureMapDependencyResolver
-------------------------------------------------------------

public class StructureMapDependencyResolver : IDependencyResolver {

private static IContainer _container;

public StructureMapDependencyResolver(IContainer container) {
_container = container;

_container.Configure(x => x.Scan(y => {
y.AssembliesFromApplicationBaseDirectory();
y.WithDefaultConventions();
y.LookForRegistries();
}));

} // ContainerResolver

public Object GetService(Type serviceType) {
return _container.TryGetInstance(serviceType);
} // GetService

public IEnumerable<Object> GetServices(Type serviceType) {
return _container.GetAllInstances<Object>().Where(s =>
s.GetType() == serviceType);
} // GetServices

} // StructureMapDependencyResolver


StructureMapControllerActivator
-------------------------------------------------------------

public class StructureMapControllerActivator : IControllerActivator
{

private readonly IContainer _container;

public StructureMapControllerActivator(IContainer container) {
_container = container;
} // StructureMapControllerActivator

public IController Create(RequestContext requestContext, Type
controllerType) {
return _container.GetInstance(controllerType) as IController;
} // Create

} // StructureMapControllerActivator


Then on my MVC project I have:

StructureMapManager
-------------------------------------------------------------

public class StructureMapManager {

public static void Configure() {

ObjectFactory.Initialize(o => { o.AddRegistry(new
StructureMapRegistry()); });
DependencyResolver.SetResolver(new
StructureMapDependencyResolver(ObjectFactory.Container));
ControllerBuilder.Current.SetControllerFactory(new
DefaultControllerFactory(new
StructureMapControllerActivator(ObjectFactory.Container)));

ModelValidatorProviders.Providers.Add(new
FluentValidationModelValidatorProvider(new
StructureMapValidatorFactory()));

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes
= false;

FilterProviders.Providers.Remove(FilterProviders.Providers.Single(f =>
f is FilterAttributeFilterProvider));
FilterProviders.Providers.Add(new
StructureMapFilterProvider(ObjectFactory.Container));

} // Configure

public static void Dispose() {
ObjectFactory.GetInstance<Domain.IContext>().Save();
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
} // Dispose

} // StructureMapManager


StructureMapRegistry
-------------------------------------------------------------

public class StructureMapRegistry : Registry {

public StructureMapRegistry() {


AssemblyScanner.FindValidatorsInAssembly(Assembly.GetExecutingAssembly()).ForEach(result
=>
{ For(result.InterfaceType).Singleton().Use(result.ValidatorType); });


For<Configuration>().Singleton().Use<Configuration>().Ctor<IEnumerable<IObjectMapper>>().Is(x
=> x.ConstructedBy(AutoMapperRegistry.AllMappers));
For<IConfigurationProvider>().Use(x =>
x.GetInstance<Configuration>());
For<IConfiguration>().Use(x => x.GetInstance<Configuration>());


For<FlyOnDreams.FlyOnPages.Domain.IContext>().HttpContextScoped().Use(()
=> new Context());

Scan(o => {
o.AssemblyContainingType(typeof(IRepository<>));
o.AddAllTypesOf(typeof(IRepository<>));
o.ConnectImplementationsToTypesClosing(typeof(Repository<>));
o.WithDefaultConventions();
});

Scan(o => {
o.AssemblyContainingType(typeof(IService));
o.AddAllTypesOf(typeof(IService));
o.WithDefaultConventions();
});

} // StructureMapRegistry

} // StructureMapRegistry


Finally, in my Global.asax file I have:

protected void Application_Start() {
StructureMapManager.Configure();
} // Application_Start

protected void Application_EndRequest() {
StructureMapManager.Dispose();
} // Application_EndRequest

And that is it!

What do you think?
> > > > structuremap-us...@googlegroups.com<structuremap-users%2Bunsubs cr...@googlegroups.com><structuremap-users%2Bunsubs

Jeremy D. Miller

unread,
Oct 30, 2010, 5:58:16 PM10/30/10
to structure...@googlegroups.com
You still need to get rid of this code:

    public IEnumerable<Object> GetServices(Type serviceType) {
      return _container.GetAllInstances<Object>().Where(s =>
s.GetType() == serviceType);
    } // GetServices

TO

    public IEnumerable<Object> GetServices(Type serviceType) {
      return _container.GetAllInstances(serviceType);
    } // GetServices
 
Jeremy D. Miller
The Shade Tree Developer

From: shapper <mdm...@gmail.com>
To: structuremap-users <structure...@googlegroups.com>
Sent: Sat, October 30, 2010 4:16:08 PM
Subject: [sm-users] Re: Problem with Structure Map in MVC 3 Beta. Please, need help. Urgent if possible. Thank You.
> > > > > > structuremap-users+unsub...@googlegroups.com<structuremap-users%2Bunsubs cr...@googlegroups.com>

> > <structuremap-users%2Bunsubs
> > > > cr...@googlegroups.com>
> > > > > > .
> > > > > > For more options, visit this group at
> > > > > >http://groups.google.com/group/structuremap-users?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "structuremap-users" group.
> > > > To post to this group, send email to
> > structure...@googlegroups.com.
> > > > To unsubscribe from this group, send email to structuremap-users+
> > > > unsub...@googlegroups.com.
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/structuremap-users?hl=en.
>
> > > >  --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "structuremap-users" group.
> > > > To post to this group, send email to
> > structure...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > structuremap-users+unsub...@googlegroups.com<structuremap-users%2Bunsubs cr...@googlegroups.com><structuremap-users%2Bunsubs

> > cr...@googlegroups.com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/structuremap-users?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "structuremap-users" group.
> > To post to this group, send email to structure...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > structuremap-users+unsub...@googlegroups.com<structuremap-users%2Bunsubs cr...@googlegroups.com>

Elliott O'Hara

unread,
Oct 30, 2010, 9:07:23 PM10/30/10
to structure...@googlegroups.com
Check out Jimmy Bogards and Jeremys blogs on how they manage their Http scoped stuff. It's a lot nicer than having that "Dispose" method that really isn't a dispose :)

And yeah, if Jeremy tells you something about SM, you can probably bet that he's right.

/E

To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.

shapper

unread,
Oct 30, 2010, 11:28:30 PM10/30/10
to structuremap-users
Yes,

I changed it to:

public IEnumerable<Object> GetServices(Type serviceType) {
return _container.GetAllInstances(serviceType) as
IEnumerable<Object>;
} // GetServices

A casted is needed otherwise I can an error.

On Oct 31, 1:07 am, "Elliott O'Hara" <elliott.oh...@gmail.com> wrote:
> Check out Jimmy Bogards and Jeremys blogs on how they manage their Http
> scoped stuff. It's a lot nicer than having that "Dispose" method that really
> isn't a dispose :)

I followed those two blogs to create my code ... And a few tips from
this newsgroup.

And the same for the Dispose (Yes, maybe that is a bad naming).

The idea was to save the context on application end and release all
objects.

Am I doing something wrong?I thought that was the way to do it.

What alternative do you suggest?

Thank You,
Miguel

>
> And yeah, if Jeremy tells you something about SM, you can probably bet that
> he's right.
>
> /E
>
> On Sat, Oct 30, 2010 at 4:58 PM, Jeremy D. Miller
> <jeremydmil...@yahoo.com>wrote:
>
>
>
>
>
>
>
> > You still need to get rid of this code:
>
> >     public IEnumerable<Object> GetServices(Type serviceType) {
> >       return _container.GetAllInstances<Object>().Where(s =>
> > s.GetType() == serviceType);
> >     } // GetServices
>
> > TO
>
> >     public IEnumerable<Object> GetServices(Type serviceType) {
> >       return _container.GetAllInstances(serviceType);
> >     } // GetServices
>
> > Jeremy D. Miller
> > The Shade Tree Developer <http://codebetter.com/blogs/jeremy.miller>
> > jeremydmil...@yahoo.com
>
> > ------------------------------
> > *From:* shapper <mdmo...@gmail.com>
>
> > *To:* structuremap-users <structure...@googlegroups.com>
> > *Sent:* Sat, October 30, 2010 4:16:08 PM
>
> > *Subject:* [sm-users] Re: Problem with Structure Map in MVC 3 Beta.
> > Please, need help. Urgent if possible. Thank You.
>
> > For<Configuration>().Singleton().Use<Configuration>().Ctor<IEnumerable<IObj ectMapper>>().Is(x
> ...
>
> read more »

shapper

unread,
Oct 31, 2010, 10:36:01 AM10/31/10
to structuremap-users

In relation to my dispose method I have:

ObjectFactory.GetInstance<Domain.IContext>().Save();

You mean there is a way to include this save when I set the context?

For<Domain.IContext>().HttpContextScoped().Use(() => new
Context());

The second code line in my dispose is:

ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();

I though this was needed. I think I got this info here and that is why
I added it.


On Oct 31, 1:07 am, "Elliott O'Hara" <elliott.oh...@gmail.com> wrote:
> Check out Jimmy Bogards and Jeremys blogs on how they manage their Http
> scoped stuff. It's a lot nicer than having that "Dispose" method that really
> isn't a dispose :)
>
> And yeah, if Jeremy tells you something about SM, you can probably bet that
> he's right.
>
> /E
>
> On Sat, Oct 30, 2010 at 4:58 PM, Jeremy D. Miller
> <jeremydmil...@yahoo.com>wrote:
>
>
>
>
>
>
>
> > You still need to get rid of this code:
>
> >     public IEnumerable<Object> GetServices(Type serviceType) {
> >       return _container.GetAllInstances<Object>().Where(s =>
> > s.GetType() == serviceType);
> >     } // GetServices
>
> > TO
>
> >     public IEnumerable<Object> GetServices(Type serviceType) {
> >       return _container.GetAllInstances(serviceType);
> >     } // GetServices
>
> > Jeremy D. Miller
> > The Shade Tree Developer <http://codebetter.com/blogs/jeremy.miller>
> > jeremydmil...@yahoo.com
>
> > ------------------------------
> > *From:* shapper <mdmo...@gmail.com>
>
> > *To:* structuremap-users <structure...@googlegroups.com>
> > *Sent:* Sat, October 30, 2010 4:16:08 PM
>
> > *Subject:* [sm-users] Re: Problem with Structure Map in MVC 3 Beta.
> > Please, need help. Urgent if possible. Thank You.
>
> > For<Configuration>().Singleton().Use<Configuration>().Ctor<IEnumerable<IObj ectMapper>>().Is(x
> ...
>
> read more »

Elliott O'Hara

unread,
Oct 31, 2010, 10:44:53 AM10/31/10
to structure...@googlegroups.com
With your approach those lines of code are needed...I was just saying that the method StructureMapManager.Dispose() implies that StructureMapManager implements IDisposable. When I saw that line I questioned why you where disposing of your container. You aren't, and SMManager doesn't implement IDisposable.

At least rename it to EndHttpRequest() or something :)

When I saw that, I just reminded me of Miller and Bogards blog on how they manage those things, so I figured I'd mention it. Both are much cleaner - they just use ControllerActionInvokers and nested containers so they don't have to bind to HTTPRequests. 

All, that being said, everything you have works now.... I was just giving you a little bit of feedback.

/E


--

shapper

unread,
Oct 31, 2010, 11:24:50 AM10/31/10
to structuremap-users


On Oct 31, 2:44 pm, "Elliott O'Hara" <elliott.oh...@gmail.com> wrote:
> With your approach those lines of code are needed...I was just saying that
> the method StructureMapManager.Dispose() implies that
> StructureMapManager implements IDisposable. When I saw that line I
> questioned why you where disposing of your container. You aren't, and
> SMManager doesn't implement IDisposable.
>
> At least rename it to EndHttpRequest() or something :)
>
> When I saw that, I just reminded me of Miller and Bogards blog on how they
> manage those things, so I figured I'd mention it. Both are much cleaner -
> they just use ControllerActionInvokers and nested containers so they don't
> have to bind to HTTPRequests.
>
> All, that being said, everything you have works now....

Not quite. :-) when I use:

public IEnumerable<Object> GetServices(Type serviceType) {
return _container.GetAllInstances(serviceType) as
IEnumerable<Object>;
} // GetServices

I get the following error:
System.InvalidCastException: Unable to cast object of type
'System.Collections.ArrayList' to type
'System.Collections.Generic.IEnumerable`1[System.Object]'.

It seems by way of solving the problem does not work.

When I use simply:
public IEnumerable<Object> GetServices(Type serviceType) {
return _container.GetAllInstances(serviceType);
} // GetServices

I get:
Cannot implicitly convert type 'System.Collections.IList' to
'System.Collections.Generic.IEnumerable<object>'. An explicit
conversion exists (are you missing a cast?)



> ...
>
> read more »

Elliott O'Hara

unread,
Oct 31, 2010, 11:28:46 AM10/31/10
to structure...@googlegroups.com
love some covariance :)

foreach(var item in _container.GetAlInstances(serviceType)
{yeild return item;}

should solve the problem.

> ...
>
> read more »

shapper

unread,
Oct 31, 2010, 11:37:16 AM10/31/10
to structuremap-users
Thank You! That solved it.

In relation to ControllerActionInvoker I have read sometime ago the
following:
http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult.aspx

To be honest I got a little bit lost ...

I think it would be great to have an example application with MVC,
StructureMap and AutoMapper.

Just something that people could follow.

Thank You,
Miguel
> ...
>
> read more »

Elliott O'Hara

unread,
Oct 31, 2010, 11:52:16 AM10/31/10
to structure...@googlegroups.com
Yeah,
That's the exact article I was talking about, and what we use in our system. In fact, we married it up with a CQRS system, works wonderfully.

It's nice having a container per request that we can stuff things in as needed.


/E



> ...
>
> read more »

Reply all
Reply to author
Forward
0 new messages