MVC Contrib VerificationException - c#

I have read this post and I wanted to use ControllerExtensions.RedirectToAction method. But I have System.Security.VerificationException Which says: type argument '[MyController type]' violates the constraint of type parameter 'T'.
My controller is declared as follows:
public class ProductsSearchController : Controller
{
...
}
Help me, please.
Also I tried to download the latest MvcContrib library from here. It didn't help me.
I noticed an interesting fact. I have this exception only when calling from unit tests. But there is no exception when using from web site. However it seems not working correctly. When I pass an object to the action in expression like this:
this.RedirectToAction(x => x.Index(filter))
it just call .ToString of this object! And I get url like this:
ProductsSearch?filter=WebShop.FinderModel.Filters.ProductsFilter
What is wrong?

I've been having this problem.
I was using MvcContrib version 2.0.95.0 alongside System.Web.Mvc version 4.0.30319.
The problem was that MvcContrib references an earlier version of System.Web.Mvc.
If you're using an older version of MvcContrib with Mvc 2 it should be sufficient to download and reference the latest version of MvcContrib. If you're using .NET 4 and Mvc 3 you'll need to update the App.Config file for your unit test project (you may have to add one) with the following:-
<configuration>
...
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
....
</configuration>
Bear in mind that you may need to change the version numbers if you're using a different version of MVC. (e.g. at the time of this edit you'd need to use oldVersion="1.0.0.0-5.1.0.0" and newVersion="5.2.0.0").
You may also need to add this to your web project. If you're only getting the exception in your test project, chances are this section already exists and is correct in your web.config; you can copy and paste it from there.
If you're using Code Analysis, you'll also need to see Assembly Binding Redirection and Code Analysis in order for it to respect the binding redirection.

Related

Assembly.LoadFrom how behaves on multi version dll

I've a dll with 2.0.0.1 version in one server which can be downloaded by accessing http://someipaddress/assembly/test.dll and I'm having another application which need to download this test.dll and have to access those methods.
When surfing for this, i've got three different methods to do,
1. Assembly.LoadFrom()
2. Assembly.LoadFile()
3. Assembly.Load()
I've tried Assembly.LoadFrom("http://someipaddress/assembly/test.dll")
Now i've replaced test.dll with 2.0.0.2 version and
What will happen the application download 2.0.0.2 test.dll and already downloaded test.dll 2.0.0.1.
Application which dll will refer?
Will it use existing test.dll 2.0.0.1 since its already downloaded while accessing test.dll 2.0.0.2?
Please suggest on this.
It'll depend on how you're referencing the assembly. By default if there's no binding redirect, your new dll will cause an exception. You can get around this by specifying an binding redirect rule in your application's configuration file like so;
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Test" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.2" newVersion="2.0.0.2" />
</dependentAssembly>
</assemblyBinding>
</runtime>
But you'd need to update it upon getting the new version of the dll.

Does Swagger.net works with higher .Net frameworks than 4.0

I have developed a WebAPI and trying to document it using Swagger.Net. My .Net framework version is 4.5.1 where as Swagger.Net appears to be written in .Net 4.0. So its always ending up with System.Web.Http version incompatibility error (located assembly manifest definition does not match the assembly reference). Solution compiles successfully but when it opens in the browser, it errors.
Also I have tried to recompile Swagger.net source code in VS 2013 and .Net framework 4.5 but even than it ends up while requiring System.Web.Http 4.0.0.
#theGeekster: FYI, you can now use WebAPI 2.x with this fork:
https://github.com/Gcastelo/Swagger.Net
The original swagger.net project looks dead.
Enjoy!
I do not think Swagger has been updated to be used along with latest versions 5.0 and 5.1 of ASP.NET Web API. Your issue could be solved by binding redirects, but I can imagine Swagger to fail with latest versions of Web API as things have changed a lot since Swagger's last release..try adding the following to your web.config.
<system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
I have ended up with conclusion that using Swagger.Net, we can't use Attribute Routing (or other WebaPI2 features like native CORS support etc.)

C# asp.net Supporting multiple versions of a dll

I have the following dll hell:
a ASP.Net project
references WebGrease
which references Antlr3.Runtime.dll 3.3.1.7705 [stored in /bin/ folder of the asp.net app]
references Custom project
which references NCalc.codeplex.com
which references Antlr3.Runtime.dll 3.1.3.22795 [stored in /bin/CustomProject/ folder of the asp.net app]
unsurprisingly these two version of Antlr are not working well together and I get "The located assembly's manifest definition does not match the assembly reference" errors
I am unwilling to modify the WebGrease project.
I am attempting to upgrade the NCalc project to use 3.3.1.7705 however I am struggling with this
Do you have any suggestions on how to get these two DLL's to work together?
EDIT unfortunately the NCalc code is not compatible with the newer version of antlr so I cannot used binding redirects
Thank you
Providing NCalc can use the later version of Antlr3.Runtime - ie there are no breaking changes you should be able to use a binding redirect to direct it to load the later version
eg in the web.config file ass something like
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NCalc" publicKeyToken="xxxxxxxxx" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.1.3.0" newVersion="3.3.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
I don't think the 4th digit on the versions is used

Unit testing c# code in a ScriptSharp project

Im using ScriptSharp to create a RIA app.
Works nice besides some oddities.
However finding and fixing problems using Firebug isn't really convinient.
Since scriptsharp also delivers a Dll I was hoping to use a separate testproject using Nunit to test some parts of my code.
Issue that arises is that the generated dll references mscorlib 0.7 resulting in conflict with mscorlib 4 in the test project.
A simple solution is to create a second plain C# project and copy codefiles around. But maintaining 2 projects with the same code base...
Curious if there is another way to do this. Anybody?
EDIT:
Solution as proposed by Christian Dalager works.
Small thing is that ScriptSharp has redefined System.Diagnostics in mscorlib. No more Debug.Assert/Writeline. But there is almost no more need for it now.
You might try using assembly binding redirects
You would put something like this in the app.config on your testproject.
Havent tested this particular configuration, so you will need to adjust it.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v1.0.3705">
<dependentAssembly>
<assemblyIdentity name="mscorlib" publicKeyToken="b77a5c561934e089" culture="neutral"/>
<bindingRedirect oldVersion="0.7.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

Add-BindingRedirect not resolving NuGet vs issue

So I am having a problem like I see other posters have had after using NuGet to install FluentNhibernate:
ERROR
Could not load file or assembly 'NHibernate, Version=3.0.0.2001, Culture=neutral,
PublicKeyToken=aa95f207798dfdb4' or one of its dependencies. The located assembly's
manifest definition does not match the assembly reference. (Exception from HRESULT:
0x80131040)
I saw that on stackOverflow answers and other blogs the answer was:
Add-BindingRedirect
However, this has not resolved my issue and all the results of running that command is:
PM> Add-BindingRedirect
Name OldVersion NewVersion
---- ---------- ----------
NHibernate 0.0.0.0-3.0.0.4000 3.0.0.4000
When I am installing Fluent Nhibernate from NuGet, it says its dependencies are Nhibernate 3.0.0.2001 but it installs Nhibernate 3.0.0.4000
What am I doing wrong?
UPDATE
I see that it adds the following section in the node:
<dependentAssembly>
<assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.4000" newVersion="3.0.0.4000" />
</dependentAssembly>
However, I still get the same error. Which just doesn't look right. If Fluent is wanting 3.0.0.2001 and NHibernate 3.0.0.4000 was installed, this binding redirect, doesn't appear to me to be correct.
To be as specific as possible. I just used NuGet to install Fluent Nhibernate in both my Web Project and a C# library project for tests. Then ran a test using NUnit which continues to give me this error.
Add-BindingRedirect is the correct command to run, and the output you get is what's expected. After you run it, you should see some binding redirect entries in your web.config (or app.config). Can you confirm that?
That will then allow the assembly to be loaded even though FluentNH asks for an older build of NHibernate.
If that doesn't work for you, please include more details in your question about what you are doing. e.g. what is the complete sequence of steps that leads to seeing the error you report (starting with project creation).
I had the same problem with a different package running MSpec tests.
The solution for me was to manually add an app.config file to each web project with the same binding redirects as in the Web.config file.
The Add-BindingRedirect command had added the entries to the Web.config files correctly, but apparently the test runner evaluates only the generated Bla.Bla.dll.config file.
sounds stupid but check in your source files and your packages sources and check there's no references to the old version there - if there is remove all version and re download the version you want via package manager console.
Edit: make sure you check all files because of how the referencing and binding works it can get very muddled and a reference in any of your files could cause the binding to redirect or still be getting the wrong version.
I had the same problem, my webservice was running but the BindingRedirect was not working for my tests.
The solution was to copy the configuration added by the Add-BindingRedirect command from my 'project'.config file to the machine.config file of the computer
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
<bindingRedirect newVersion="3.3.1.4000" oldVersion="0.0.0.0-3.3.1.4000" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Once I copied that my tests started working

Categories