views:

438

answers:

2

Hi I am using a custom model binder with asp.net mvc 2.0 , everything works locally but when deployed to the server runing iis 7, i get a weird error which is hard to get information about the error is as follows, I am also attaching my model binding class

[MissingMethodException: Method not found: 'System.Collections.Generic.IDictionary`2 System.Web.Mvc.ModelBindingContext.get_ValueProvider()'.] FID.Binders.documentModelBinder.GetValue(ModelBindingContext bindingContext, String key) in C:\Users\Bich Vu\Documents\Visual Studio 2010\Projects\FID\FID\Binders\DocumentModelBinder.cs:155 FID.Binders.documentModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) in C:\Users\Bich Vu\Documents\Visual Studio 2010\Projects\FID\FID\Binders\DocumentModelBinder.cs:61 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +475 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +152 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +709 System.Web.Mvc.Controller.ExecuteCore() +162 System.Web.Mvc.<>c_DisplayClass8.b_4() +58 System.Web.Mvc.Async.<>c_DisplayClass1.b_0() +20 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371 Below is the code file

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Core; using Core.Extensions; using System.IO; using Core.Services; using Core.Binders; using System.Data.Linq; using Microsoft.Practices.ServiceLocation; using xVal.ServerSide; namespace FID.Binders { public class BinddocumentAttribute : DataBindAttribute { public BinddocumentAttribute() : base(typeof(documentModelBinder)) { } } public class documentModelBinder : DefaultModelBinder {

    ICategoryService _CategoryService=   ServiceLocator.Current.GetInstance<ICategoryService>();
    IFilePersistor _persistor = ServiceLocator.Current.GetInstance<IFilePersistor>();
    IFileTypeService _FiletypeService = ServiceLocator.Current.GetInstance<IFileTypeService>();      

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var doc = base.BindModel(controllerContext, bindingContext) as Document;


        string key = bindingContext.ModelName;

        var errors = Core.xval.DataAnnotationsValidationRunner.GetErrors(doc);
        try
        {
            if (errors.Any())
                throw new xVal.ServerSide.RulesException(errors);


         foreach (string inputTagName in controllerContext.HttpContext.Request.Files)
        {


            HttpPostedFileBase filebase = controllerContext.HttpContext.Request.Files[inputTagName];
             if (filebase.ContentLength==0)
                 break; 
             string  extension= Path.GetExtension(filebase.FileName);

          //   if (string.IsNullOrEmpty(extension))
            //     break; 
                if (filebase.ContentType.Contains("image/"))
                {

                }

                else
                {
                    if (extension != _FiletypeService.GetFiletype(GetValue<short>(bindingContext, "Filetype_id")).extension.Trim())

                    {
                        bindingContext.ModelState.AddModelError("filetype", "Verify that the file type matches the selected file type");                           
                      throw new RulesException("filetype", "Verify that the file type matches the selected file type", doc);

                    }


                    //FileType proposed =_FiletypeService.GetFileTypeByExtension(extension);
                    //if (proposed==null)  // not existing in filetype store
                    //{
                    //    bindingContext.ModelState.AddModelError("filetype", "Verify that the file type matches the selected file type");
                    //    throw new RulesException("filetypee", "Verify that the file type is in the list of available file types", doc);

                    //}

                }





        }



    }

        catch (xVal.ServerSide.RulesException ex)
        {


            return null;
        }



       doc.Category1 = _CategoryService.GetCategory(GetValue<int>(bindingContext, "cat.parent_id"));
        doc.FileType1 = _FiletypeService.GetFiletype(GetValue<short>(bindingContext, "Filetype_id"));
        doc.modifieddate = DateTime.Now;

        if (doc.IsNewDocument)
        {
            doc.CreateParentFolders();
            doc.createdate = DateTime.Now;

        }
        UpdateFiles(controllerContext, doc);
        return doc;
    }

    private void UpdateFiles(ControllerContext controllerContext, Document doc)
    {

        foreach (string inputTagName in controllerContext.HttpContext.Request.Files)
        {
            HttpPostedFileBase filebase = controllerContext.HttpContext.Request.Files[inputTagName];
            if (filebase.ContentLength > 0)
            {

                if (filebase.ContentType.Contains("image/"))
                {


                    Thumb image = new Thumb { type = filebase.ContentType, name = filebase.FileName, PostedFile = filebase, AssociatedDocument = doc, document_id=doc.document_id };
                    image.filepath = _persistor.PersistFile(image);
                    doc.Thumbs.Add(image);
                }

                else
                {
                    doc.PostedFile = filebase;
                    doc.filesize = long.Parse(filebase.ContentLength.ToString());
                    doc.filepath = _persistor.PersistFile(doc);

                }




            }
        }
    }




    private T GetValue<T>(ModelBindingContext bindingContext, string key)
    {
        ValueProviderResult valueResult;
        bindingContext.ValueProvider.TryGetValue(key, out valueResult);
        bindingContext.ModelState.SetModelValue(key, valueResult);
        return (T)valueResult.ConvertTo(typeof(T));
    }

    protected override void OnModelUpdated(ControllerContext controllerContext,
                                      ModelBindingContext bindingContext)
    {
        var document = bindingContext.Model as Document;
        if (String.IsNullOrEmpty(document.title))
        {
            bindingContext.ModelState.AddModelError("Name", "...");
        }
    }


}

}

A: 

A missing method exception sounds like where your code compiled successfully on your push machine, but a referenced DLL didn't make it out to your production server. I'd check to see if the bin contains a different number of DLLs, what versions of each DLL are out there, and if your production machine is missing certain DLLs from the GAC.

Tejs
could a difference in the versions of system.web.mvc be the reason, I was wondering the same thing, my local version is v2.0.50727 while the server version is 2.0.50217.0
Definitely. Always verify the same DLL versions you used to build are the same ones in prod.
Tejs
+2  A: 

First you should make sure your running the correct assemblies

Second In MVC2 the following is not valid. The IValueProvider Interface has changed

bindingContext.ValueProvider.TryGetValue(key, out valueResult); 

should be replaced with

valueResult = bindingContext.ValueProvider.GetValue(key); 
John Hartsock
Thats was the perfect answer, Thanks John. The different versions of asp.mvc was the problem
Not a problem I ran in to a similar issue when going from MVC 1 to MVC 2.
John Hartsock
Thanks very much.
Kayes