Debugging MVC3 Source code with Ninject error - asp.net-mvc

I am trying to understand how the Html Helpers are built inside MVC 3. So i downloaded MVC 3 source and added as another project in same solution to debug it.
I followed this steps.
Removed System.web.Mvc reference from my MVC Application
Added a System.web.Mvc Source code project reference for debugging.
In Web.config System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 to System.Web.Mvc
However i have other NInject(NInject, NInject.web.common, WebActivator, Microsoft.web.infrastructure) references added in my MVC 3 application for dependency injection.
Now i am getting an error in System.Web.MVC Source saying 'An error occurred when trying to create a controller of type 'MVCApp.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor. in below MVC Source code of
public IController Create(RequestContext requestContext, Type controllerType)
{
try
{
return (IController)(_resolverThunk().GetService(controllerType)
?? Activator.CreateInstance(controllerType));
}
catch (Exception ex)
{
throw new InvalidOperationException(
String.Format(CultureInfo.CurrentCulture,
MvcResources.DefaultControllerFactory_ErrorCreatingController, controllerType),
ex);
}
}
Checking DependencyResolver.Current property says
DependencyResolver.Current The type
'System.Web.Mvc.DependencyResolver' exists in both
'System.Web.Mvc.dll' and 'System.Web.Mvc.dll'
What more needs to be done in order to debug my MVC3App with MVC3 source code? how can i avoid this error?

Take a look on this solution: Ninject + MVC3 is not injecting into controller
Add this to the Web.config
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="4.0.0.0" />
<bindingRedirect oldVersion="2.0.0.0" newVersion="4.0.0.0" />
<!-- The following line was missing -->
<bindingRedirect oldVersion="3.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
It solved this problem:
Checking DependencyResolver.Current property says
DependencyResolver.Current The type
'System.Web.Mvc.DependencyResolver' exists in both
'System.Web.Mvc.dll' and 'System.Web.Mvc.dll'
Cheers.

Related

StackExchange.Redis.Extensions error after updating nuget

I have updated to the latest StackExchange.Redis.Extensions.Core package and I am getting the following error.
The previous version of StackExchange.Redis.Extensions.Core is - 2.3.0 and the updated new version is - 3.4.0
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'StackExchange.Redis.Extensions.Core.StackExchangeRedisCacheClient' can be invoked with the available services and parameters:
Cannot resolve parameter 'StackExchange.Redis.Extensions.Core.Configuration.RedisConfiguration configuration' of constructor 'Void .ctor(StackExchange.Redis.Extensions.Core.ISerializer, StackExchange.Redis.Extensions.Core.Configuration.RedisConfiguration)'.
Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(StackExchange.Redis.Extensions.Core.ISerializer, System.String, System.String)'.
Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(StackExchange.Redis.Extensions.Core.ISerializer, System.String, Int32, System.String)'.
Cannot resolve parameter 'StackExchange.Redis.IConnectionMultiplexer connectionMultiplexer' of constructor 'Void .ctor(StackExchange.Redis.IConnectionMultiplexer, StackExchange.Redis.Extensions.Core.ISerializer, System.String)'.
Cannot resolve parameter 'StackExchange.Redis.IConnectionMultiplexer connectionMultiplexer' of constructor 'Void .ctor(StackExchange.Redis.IConnectionMultiplexer, StackExchange.Redis.Extensions.Core.ISerializer, StackExchange.Redis.Extensions.Core.Configuration.ServerEnumerationStrategy, System.String)'.
Cannot resolve parameter 'StackExchange.Redis.IConnectionMultiplexer connectionMultiplexer' of constructor 'Void .ctor(StackExchange.Redis.IConnectionMultiplexer, StackExchange.Redis.Extensions.Core.ISerializer, Int32, System.String)'.
Cannot resolve parameter 'StackExchange.Redis.IConnectionMultiplexer connectionMultiplexer' of constructor 'Void .ctor(StackExchange.Redis.IConnectionMultiplexer, StackExchange.Redis.Extensions.Core.ISerializer, StackExchange.Redis.Extensions.Core.Configuration.ServerEnumerationStrategy, Int32, System.String)'.
I have installed following nuget packages:
Microsoft.Web.RedisSessionStateProvider
StackExchange.Redis
StackExchange.Redis.StrongName
StackExchange.Redis.Extensions.Core
StackExchange.Redis.Extensions.LegacyConfiguration
StackExchange.Redis.Extensions.Newtonsoft
My web.config is as follows:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="redisCacheClient" type="StackExchange.Redis.Extensions.LegacyConfiguration.RedisCachingSectionHandler, StackExchange.Redis.Extensions.LegacyConfiguration" />
</configSections>
<redisCacheClient allowAdmin="true" ssl="True" connectTimeout="3000" database="15" password="IlQZ--------90=">
<serverEnumerationStrategy mode="Single" targetRole="PreferSlave" unreachableServerAction="IgnoreIfOtherAvailable" />
<hosts>
<add host="xxxxxx.redis.cache.windows.net" cachePort="6380" />
</hosts>
</redisCacheClient>
<connectionStrings>
<add name="RedisConnectionString" connectionString="xxxx.redis.cache.windows.net:6380,password=IlQZ--------90=,ssl=True,synctimeout=15000,abortConnect=False" />
</connectionStrings>
<appSettings>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
<runtime>
<assemblyBinding
xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.8.1.0" newVersion="4.8.1.0" />
</dependentAssembly>
<!-- more dependentAssembly -->
<dependentAssembly>
<assemblyIdentity name="StackExchange.Redis.Extensions.Core" publicKeyToken="d7d863643bcd13ef" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.4.0.0" newVersion="3.4.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
And my OWIN startup class with autofac is as follows :
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<NewtonsoftSerializer>()
.AsSelf()
.AsImplementedInterfaces()
.SingleInstance();
builder.RegisterType<StackExchangeRedisCacheClient>()
.AsSelf()
.AsImplementedInterfaces()
.SingleInstance();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
app.UseAutofacMiddleware(container);
app.UseAutofacMvc();
}
What am i doing wrong here?
Autofac can't resolve anything you don't tell it about. You've told it about:
Your controllers.
NewtonsoftSerializer
StackExchangeRedisClient
But read the exception - there's no parameterless constructor for StackExchangeRedisClient and you didn't tell Autofac about anything else. Autofac doesn't just "read the web.config" or anything. You have to put that together. Read the exception again and register enough stuff that one of the constructors will match.
This is not real code but gives you an idea:
// THIS ISN'T TESTED, FOR IDEA PURPOSES ONLY
builder.Register(c => {
var config = ConfigurationManager.GetSection("RedisCacheClient");
var connectionString = config.ConnectionStrings[0];
return new StackExchangeRedisClient(connectionString);
}).AsSelf()
.AsImplementedInterfaces()
.SingleInstance();
Point being, you have to read the config, or register something in the container that will otherwise satisfy the parameters required to create the Redis client.
The problem is that Autofac does not have the proper information to build the object so I agree with #travis-illig. The way I resolved it was to register the required types first:
builder.RegisterType<JilSerializer>()
.As<ISerializer>()
.SingleInstance();
RedisConfiguration redisConfiguration = RedisUtil.GetConfig();
builder.RegisterInstance(redisConfiguration);
builder.RegisterType<StackExchangeRedisCacheClient>()
.As<ICacheClient>()
.SingleInstance();
Note I have got a Util class that constructs the Redisconfiguration.

Integrating stimulsoft to MVC4 application

I'm trying to integrate stimulsoft designer in my web application (MVC4, Razor 2 and Entity Framework 4) but I have encountred some problems.
I have integrated the dlls and the assemblies, but I have this error :
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source Error: #Html.Stimulsoft().StiMvcViewer(new StiMvcViewerOptions() {
The problem was resolved by making following changes in the Web.Config file:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="2.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

OpenID doesnt work with DotNetOpenAuth after upgrade to ver4

I have just upgraded my DotNetOpenAuth version from 3.4.7.11121 to 4.0.0.12079-RC1 to get support for OAuth2. The problem is that after the upgrade my OpenID-requests not longer works. The last row below generates a FileNotFoundException (Message=Could not load file or assembly 'System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.)
OpenIdRelyingParty openid = new OpenIdRelyingParty()
var request = openid.CreateRequest(requestedIdentifier, Realm.AutoDetect, returnUrl);
return request.RedirectingResponse.AsActionResult();
I can't really understand why it looks for that file. I use the DotNetOpenAuth library in a MVC3 application, and thus the 3.0-version of the System.Web.Mvc-file exists in the project.
Anyone that has any ideas what I can do to make it work?
Updated: The stable 4.0.0.12084 version was released a few hours after I posted this question. Have installed that instead of the RC, but it doesn't make any difference. I tried to install MVC ver 1.0 in order to get the required dll. After the installation no exception is thrown, but instead a white page containing the text "DotNetOpenAuth.Messaging.OutgoingWebResponseActionResult" is displayed instead of the redirect to the OpenID-service provider. I don't understand this..
//Per Andersson
Yeay, I found a solution. Just add a few lines in web.config
<runtime>
<legacyHMACWarning enabled="0" />
<!-- When targeting ASP.NET MVC 3, this assemblyBinding makes MVC 1 and 2 references relink
to MVC 3 so libraries such as DotNetOpenAuth that compile against MVC 1 will work with it. -->
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

error with #MvcHtmlString.Create(...)) - system.web.mvc

I create a new asp.net mvc 4 version and
include dotnetopenauth implementation from Nerddinner project into it.
registration and authentication using formsauthentication works ok. On
the other hand when I include Logon details for openauth -
logonopenid.cshtml - implementation I received an error:
"Could not load file or assembly 'System.Web.Mvc, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its
dependencies. The located assembly's manifest definition does not
match the assembly reference. (Exception from HRESULT: 0x80131040) "
Source Error:
Line 38: #MvcHtmlString.Create(Html.OpenIdSelectorScripts(options,
null))
I checked and I´m using system.web.mvc version= 4 and includes a
definition for mvchtmlstring and html helpers includes a definition
for openidselectorscripts. truly I dont know why is requesting
assembly 1.0.0.
any help will be appreciated. brgds,
sebastian.
You need to add this to your web.config file:
<runtime>
<!-- When targeting ASP.NET MVC 4, this assemblyBinding makes MVC 1 references relink
to MVC 4 so libraries such as DotNetOpenAuth that compile against MVC 1 will work with it. -->
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
sebastian_h,
I've seen this happening before. 'distant' memory recalls the issue being related to entries in the web.config that is located under the /Views folder (as opposed to the root web.config file).
I may be way off here, but this rings a bell. Certainly worth a quick peek.

ActionResult redirect renders using ToString()

Using the following sample: http://www.west-wind.com/Weblog/posts/899303.aspx
The following line...
return req.RedirectingResponse.AsActionResult();
renders the string "DotNetOpenAuth.Messaging.OutgoingWebResponseActionResult".
This is the class being returned as ActionResult in the line as posted. Does anyone know why I get the class name as string instead of a actual redirect?
Thnx in advance!
Could it be that you have multiple versions of MVC in your Bin directory and your web server? It strikes me that if you had multiple assemblies (different versions) of the MVC types like ActionResult loaded into your AppDomain, and the AsActionResult method returned one version and your MVC web app used a different version, that it might just bail out with a ToString. What version of ASP.NET MVC are you running? DotNetOpenAuth's AsActionResult method was compiled against the 1.0 RTM version of the MVC framework. If you're using ASP.NET MVC 2 (included with .NET 4.0), I could see this maybe being a problem.
I believe if you add this snippet to your MVC 2's web.config file, that it will allow you to use the official build of DotNetOpenAuth so you don't have to build your own:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Another solution, working for me with .Net 3.5 and MVC 2.
Instead of
var authRequest = relyingParty.CreateRequest(....);
....
return authRequest.RedirectingResponse.AsActionResult();
use
var authRequest = relyingParty.CreateRequest(....);
....
authRequest.RedirectToProvider();
Response.End();
return new EmptyResult();

Resources