How can I use simpleinjector in Such scene? thanks!

Jun 5, 2013 at 7:21 AM
1. in Services class library
...
private readonly IUserRoleRepository userRoleRepository;
    private IUserRoleService userRoleService;

    public UserService()
    {
       //any code for help

    }
2. in httphandler ashx file
public class UserHandler :BaseHttpHandler, IRequiresSessionState
{
    private readonly IUserService userService;
    public UserHandler()
    {
        //any code for help
    }
  1. how to write code in global.asax ?
Coordinator
Jun 5, 2013 at 8:56 AM
Edited Sep 17, 2014 at 6:28 AM
Classes such as your UserService and UserRoleService can be resolved by the container, by simply registering them as follows:
container.Register<IUserRoleService, UserRoleService>();
container.Register<IUserService, UserService>();
To be able to work, these classes should have a single public constructor that takes in the dependencies they require:
    private IUserRoleService userRoleService;

    public UserService(IUserRoleService userRoleService)
    {
       this.userRoleService = userRoleService;
    }
Unfortunately, ASP.NET requires HttpHandler implementations to have a default constructor, because the ASP.NET runtime creates HttpHandlers for us. For wiring HttpHandlers you'll need to do the same trick as is advised for ASP.NET Form classes here:
    private readonly IUserService userService;

    public UserHandler()
    {
       this.userService = Global.GetInstance<IUserService>();
    }
Best is to implement your handler as a Humble Object. In that case the handler should just resolve a single dependency and delegate all its work to that dependency. This dependency can have many dependencies of its own of course. This keeps the application robust, maintainable and testable.
Marked as answer by dot_NET_Junkie on 2/26/2014 at 1:34 PM
Sep 17, 2014 at 12:02 AM
thx,If there are many many interface in service layer, must I register them as container.Register<I**Service, **Service>() manually? is there convenient method?
Coordinator
Sep 17, 2014 at 6:19 AM
Hi Kevin,

You'll find more information about this in the batch registration section of the advanced scenarios documentation page.
Sep 29, 2014 at 12:06 AM
I have googled it ago as https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios#Batch-Registration. But I don't know where to put these codes. Can you write some simple demo in WebForm? There is snippet http://techbrij.com/autofac-ioc-container-asp-net-mvc-di. Thank you advanced.
Coordinator
Sep 29, 2014 at 5:06 AM
What framework are you using? Web forms? MVC?
Sep 29, 2014 at 7:50 AM
.net framework4.0 and Web forms!
The project structure is described as below.

MIS.Infrastructure
MIS.Model
MIS.Repository
----Interface
----Implementation
MIS.Service
----Interface
----Implementation
MIS.Web
----Scripts
----Css
----HttpHandler(ashx)
----ADMIN
---------*.aspx
----Default.aspx
----Global.asax
Coordinator
Sep 29, 2014 at 8:10 AM
We have an integration guide for Web Forms. I hope that helps.
Oct 2, 2014 at 10:36 AM
I meet with error: Error The non-generic method 'System.Reflection.MemberInfo.GetCustomAttributes(bool)' cannot be used with type arguments in Global.asax.cs

.netframework 4
Coordinator
Oct 2, 2014 at 10:50 AM
Are you getting a C# compile error or a runtime exception. If this is an exception, can you post the full stacktrace, and exception type. If it is a compile error, can you post the relevant code?
Oct 4, 2014 at 1:08 AM
It is C# compile error and using System.ComponentModel.Composition already.

in global.asax.cs
private class ImportAttributePropertySelectionBehavior : IPropertySelectionBehavior {
        public bool SelectProperty(Type serviceType, PropertyInfo propertyInfo) {
            // Makes use of the System.ComponentModel.Composition assembly
            return typeof(Page).IsAssignableFrom(serviceType) &&
                propertyInfo.GetCustomAttributes<ImportAttribute>().Any();
        }
    }
error:

Error 3 The non-generic method 'System.Reflection.MemberInfo.GetCustomAttributes(bool)' cannot be used with type arguments
Coordinator
Oct 4, 2014 at 7:44 AM
Add a reference to the System.Reflection namespace at the top of the file. GetCustomAttribute<T> is an extension method that has been added in .NET 4.5.
Oct 4, 2014 at 2:01 PM
A reference of the System.Reflection namespace exists and error still occurs.
Coordinator
Oct 4, 2014 at 2:38 PM
Edited Oct 4, 2014 at 3:09 PM
I'm sorry, you are running .NET 4.0. This extension method has been added in .NET 4.5. Either way, you can use the non-generic overload as well. Take a look at the GetCustomAttribute msdn docs to see how to use it.