This project is read-only.
1
Vote

problem with PUT POST AND DELETE Requests using API

description

Hi I am new to MVC. I downloaded EFMVC working with EFMVC to understand each step. I am facing problem with WEB API post data from category controller. In Default Command Bus var handler = DependencyResolver.Current.GetService<ICommandHandler<TCommand>>(); returning null value.
Here is my bootstrapper class method:
 private static void SetAutofacWebAPIServices()
    {
        var configuration = GlobalConfiguration.Configuration;
        var builder = new ContainerBuilder();          
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        builder.RegisterType<DefaultCommandBus>().As<ICommandBus>().InstancePerApiRequest();
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerApiRequest();
        builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerApiRequest();
        builder.RegisterAssemblyTypes(typeof(CategoryRepository)
            .Assembly).Where(t => t.Name.EndsWith("Repository"))
        .AsImplementedInterfaces().InstancePerApiRequest();
        var services = Assembly.Load("EFMVC.Domain");
        builder.RegisterAssemblyTypes(services)
        .AsClosedTypesOf(typeof(ICommandHandler<>)).InstancePerApiRequest();
        builder.RegisterAssemblyTypes(services)
        .AsClosedTypesOf(typeof(IValidationHandler<>)).InstancePerApiRequest();
        var container = builder.Build();
        var resolver = new AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
        configuration.DependencyResolver = resolver;           
    }
Here is my category api controller post method:
public HttpResponseMessage Post(CategoryFormModel category)
{
    if (ModelState.IsValid)
    {
        Mapper.CreateMap<CategoryFormModel, CreateOrUpdateCategoryCommand>();
        var command = Mapper.Map<CategoryFormModel, CreateOrUpdateCategoryCommand>(category);
       // var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.Name, category.Description);
        var result = commandBus.Submit(command);
        if (result.Success)
        {                  
            var response = Request.CreateResponse(HttpStatusCode.Created, category);
            string uri = Url.Link("DefaultApi", new { id = category.CategoryId });
            response.Headers.Location = new Uri(uri);
            return response;
        }
    }
    else
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
    }
    throw new HttpResponseException(HttpStatusCode.BadRequest);
}
Default Command Bus:
public class DefaultCommandBus : ICommandBus
{

    public ICommandResult Submit<TCommand>(TCommand command) where TCommand: ICommand
    {    
        var handler = DependencyResolver.Current.GetService<ICommandHandler<TCommand>>();
        if (!((handler != null) && handler is ICommandHandler<TCommand>))
        {
            throw new CommandHandlerNotFoundException(typeof(TCommand));
        }  
        return handler.Execute(command);

    }
    public IEnumerable<ValidationResult> Validate<TCommand>(TCommand command) where TCommand : ICommand
    {
        var handler = DependencyResolver.Current.GetService<IValidationHandler<TCommand>>();
        if (!((handler != null) && handler is IValidationHandler<TCommand>))
        {
            throw new ValidationHandlerNotFoundException(typeof(TCommand));
        }  
        return handler.Validate(command);
    }
}
-Thanks
Sindhu

comments