Search code examples
visual-studioasp.net-mvc-4nugetknockout-mvc

kMVC Nuget Package Installation "methods is null" Error


I have been trying out Knockout MVC (kMVC) for use in our intranet apps for a couple of weeks now and I am getting the error ("methods is null") when installing the kMVC package via nuget on all of our existing and newly created sample ASP.NET MVC 4 projects. I tried it on a couple of different machines one with VS 2017 and the other VS 2019 I still get the same error message.


Solution

  • I have been trying out Knockout MVC (kMVC) for use in our intranet apps for a couple of weeks now and I am getting the error ("methods is null") when installing the kMVC package via nuget.

    This package is too old(never update since 8/12/2014) so it's not that compatible with VS2015~VS2019.

    Details:

    You can find a packages folder in your solution directory, there you can find the kMVC.0.6.0 folder. This folder contains the PerpetuumSoft.Knockout.dll in lib sub-folder and install.ps1 in tools sub-folder.

    When we install that nuget package, nuget will help our project to reference that assembly, and run the powershell script automatically. The method is null issue results from that.

    See content of Install.ps1 in that nuget package:

    param($installPath, $toolsPath, $package, $project)
    
    $projectIsVB = $false
    try {
        $item = $project.ProjectItems.Item("global.asax").ProjectItems.Item("global.asax.cs")
    } catch {
        $item = $project.ProjectItems.Item("global.asax").ProjectItems.Item("global.asax.vb")
        $projectIsVB = $true
    }
    
    if ($projectIsVB) {
        $class = $item.FileCodeModel.CodeElements | where-object {$_.Kind -eq 1}
    } else {
        $namespace = $item.FileCodeModel.CodeElements | where-object {$_.Kind -eq 5}
        $class = $namespace.Children | where-object {$_.Kind -eq 1}
    }
    
    $method = $class.Children | where-object {$_.Name -eq "Application_Start"}
    if (!$method)
    {
        [system.windows.forms.messagebox]::show("methods is null")
    }
    
    $edit = $method.StartPoint.CreateEditPoint();
    $edit.LineDown()
    $edit.CharRight(1)
    $edit.Insert([Environment]::NewLine)
    if ($projectIsVB) {
        $edit.Insert("      ModelBinders.Binders.DefaultBinder = new PerpetuumSoft.Knockout.KnockoutModelBinder()")
        $edit.Insert([Environment]::Newline)
    } else {
        $edit.Insert("      ModelBinders.Binders.DefaultBinder = new PerpetuumSoft.Knockout.KnockoutModelBinder();")
    }
    

    So if we install this nuget package in MVC project, it will try to add one line ModelBinders.Binders.DefaultBinder = new PerpetuumSoft.Knockout.KnockoutModelBinder(); to Application_Start method in global.asax.cs file.

    I tried it in VS2012, it works well without any issue. But in VS2015~VS2019, it throws method is null warning. I think this is because the project system or VS SDK has changed a lot in these versions, so the code in install.ps1 can't work well any more.

    Here're some suggestions to your original issue:

    1.You can try to contact the author to ask if they plan to update this package to support VS2017~VS2019.

    2.It's just a little message from the author, it won't actually affect anything in your project.(It won't affect your build or development) So you can easily ignore this messagebox.

    And all the install.ps1 wants to do is to add the ModelBinders.Binders.DefaultBinder = new KnockoutModelBinder(); to Application_Start method, so just ignore the warning and add this line manually.

    As for your another issue How do I set the default binder to both?

    The line2 will cover line1 in Application_Start method, so only line2 works in that situation. And to find a workaround to set them both(related to coding), I suggest you can open an new thread with more details since it's not related to your original issue(related to nuget package). Please avoid asking two different questions in one issue.

    Hope all above makes some help:)