user1376198
user1376198

Reputation: 61

autofac mvc pass by ref error

I am using MVC 5 with autofac and in my my controller I call an action method that calls a method with ref parameter. When i try to browse to browse to the action method I get the below error that suggest that I can not use ref parameters. Is that correct?

Cannot call action method 'AllThings.WebUI.Models.SearchCriteriaViewModel SetListData(System.String, AllThings.WebUI.Models.SearchCriteriaViewModel ByRef, AllThings.Entities.Abstracts.ISimpleService, Double)' on controller 'AllThings.WebUI.Controllers.PostController' because the parameter 'AllThings.WebUI.Models.SearchCriteriaViewModel& searchCriteriaViewModel' is passed by reference. Parameter name: methodInfo

cut down the code and the main code looks like this:

[Route("~/{site}/{CategoryUrl?}")]
    [Route("~/{country:maxlength(2)}/{site}/{CategoryUrl?}", Name="ResultList")]
    [AllowAnonymous]
    public ActionResult List(string country, string site, SearchCriteriaViewModel searchCriteriaViewModel)
    {
        SetListData(site, ref searchCriteriaViewModel, simpleService, CommonLibrary.Helpers.GetCountryTimeZoneOffSet(countryIso2));

        return View("List", searchCriteriaViewModel);
    }

 public SearchCriteriaViewModel SetListData(string site, ref SearchCriteriaViewModel searchCriteriaViewModel, ISimpleService simpleService, double utcOffset)
    {
        SearchCriteria searchCriteria = EntityFactory.GetSearchCriteria();

        if (searchCriteriaViewModel.Category == null)
        {
            searchCriteriaViewModel.Category = new Category();
        }


        return searchCriteriaViewModel;
    }

my autofac config looks like this

public static void Initialize()
    {
        var builder = new ContainerBuilder();
        DependencyResolver.SetResolver(new AutoFacDependencyResolver(RegisterServices(builder)));
    }

    private static IContainer RegisterServices(ContainerBuilder builder)
    {
        builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly);

        //builder.RegisterType<AllThings.Entities.Concrete.ContextFactory>().As<IContextFactory>().SingleInstance();
        builder.RegisterType<AllThings.Entities.Concrete.SimpleService>().As<ISimpleService>().InstancePerRequest();

        builder.RegisterType<AllThings.Entities.Concrete.EFSimpleRepository>().As<ISimpleRepository>().InstancePerRequest();


        //builder.RegisterInstance(GetMockContext()).As<IContextFactory>(); 
        return builder.Build();
    }

Upvotes: 0

Views: 264

Answers (1)

CodeCaster
CodeCaster

Reputation: 151594

What do you expect the ref parameter to do? Your action method gets called using reflection, based on an incoming HTTP request. It's not like if you modify the ref parameter, the object on the client changes too - it's entirely disconnected through the nature of HTTP.

Just remove the ref modifier.

Upvotes: 1

Related Questions