none
What is the equivalent of Global.asax in WCF RRS feed

  • Question

  • Hi,

    In ASP.NET we have Global.asax where we can put all our initialize settings before we process any request.

    Is there also any equivalent in WCF, where we can put all our initialize settings before the service invoked?

    Real world example:

    I am using Automapper to map our business object and our data contract. Right now my code is like this

     public class EmployeeImpl : IEmployee
     {
           private EmployeeRepository employeeRepository;
    
            public EmployeeImpl()
            {
                employeeRepository = new EmployeeRepository();
                Mapper.CreateMap<Employee, EmployeeDTO>();
            }
            public EmployeeDTO GetEmployee(string id)
            {
                Employee emp = employeeRepository.GetEmployeeById(id);
                EmployeeDTO eDto = Mapper.Map<Employee, EmployeeDTO>(emp);
                return eDto;
            }
     }

    I want actually below code out from EmployeeImpl class and put it in Global class that equivalent to Global.asax. So whenever the service invoked, this below code would be invoked first. The goal is to make easier to maintain. So, whenever any changes in the configuration, I only need to go to Global class.

    Mapper.CreateMap<Employee, EmployeeDTO>();

    Regards

    Friday, July 11, 2014 7:19 AM

All replies

  • I believe AppInitialize() is the method you're looking for. Here's an article on using it to initialise Castle Windsor in a WAS hosted WCF service:

    Castle Windsor and non-HTTP Protocol WCF Services

    The essence of the article is, instead of using Application_Start() which won't get called in WAS:

    protected void Application_Start(object sender, EventArgs e)
    {
       var container = new WindsorContainer("ioc.config");
       DefaultServiceHostFactory.RegisterContainer(container.Kernel);
    }

    Use:

    public class InitialiseService
    {
       /// <summary>
       /// Application initialisation method where we register our IOC container.
       /// </summary>
       public static void AppInitialize()
       {
          var container = new WindsorContainer("ioc.config");
          DefaultServiceHostFactory.RegisterContainer(container.Kernel);
       }
    }

    To quote Matt:

    I confess I spent a while looking at the Host Factory in more detail, looking to wrap the DefaultServiceHostFactory. However, there appears to be a far simpler solution and that is to make use of the little documented AppInitialize method. If you create a class (any class), put it into the ASP.NET App_Code folder in your project and give it a method signature as defined below, this little baby will get fired exactly when you want it to. You can then initialise your IoC container in there.

    Monday, July 14, 2014 1:19 PM
  • Hi,

    Thanks for the answer.

    The problem is right now Iam using Unity as IoC-Container and I think Castle Windsor is also another IoC container. I don't know yet, should I am using both IoC-Container (Unity and Castle Windsor) or better I am looking for another approach?

    Regards

    Monday, July 14, 2014 1:44 PM