SInglePageApplication - DbDataController and HttpControllerConfigurationAttribute error - .net-framework-version

I am trying to work through the example from Steve Sandersons SPA video.
http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2159
Much like the fellow in this question - Could not load type HttpControllerConfigurationAttribute after updating to Asp.Net MVC 4 RTM - My version does work with ApiController, but I need either DbDataController or LinqToEntitiesController, however during runtime both cause the exception below:
Could not load type 'System.Web.Http.Controllers.HttpControllerConfigurationAttribute' from
assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
I'm not explicitly using HttpControllerConfigurationAttribute. Here is my code.
namespace MvcApplication5.Controllers
{
public class DataServiceController : DbDataController<MSEnterpriseEntities>
{
public IQueryable<MS_Substations> GetUtilities()
{
return DbContext.MS_Substations.OrderBy(x => x.SubId);
}
public void InsertUtility(MS_Substations utility) { InsertEntity(utility); }
public void DeleteUtility(MS_Substations utility) { DeleteEntity(utility); }
public void UpdateUtility(MS_Substations utility) { UpdateEntity(utility); }
}
}
I know HttpControllerConfigurationAttribute has been removed from System.Web.Http - I Don't know how to fix the problem though.
What exactly (for dummies version) do I need to do to fix this problem?

Related

Call service layer from context

Firstly let me say that I might go down the wrong road with this but can't get my head around how to get this to work.
What I'm trying to achieve is to call my service layer, to execute some functions, after I insert/update/delete database entries.
My initial thoughts was to inject my service into my context but not sure if this is possible, or wise for that matter.
This is my context where I inject the service "serviceBus":
public interface IPCContext
{ }
public class PCContext : IdentityDbContext<User>, IPCContext
{
private readonly IClientBusServices _serviceBus;
public PCContext(IClientBusServices serviceBus)
: base("PayComplimentContext")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<PCContext, Configuration>());
_serviceBus = serviceBus;
}
public override int SaveChanges()
{
var count = base.SaveChanges();
_serviceBus.SyncDw.Value.SyncDw(1, "Feedback", 1);
return count;
}
}
And to register the "serviceBus" I use Autofac like this:
class ClientBusModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(ThisAssembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces();
builder.RegisterAggregateService<IClientBusServices>();
builder.RegisterType<ClientSearchIndexService>().As<IClientSearchIndexService>().InstancePerLifetimeScope();
builder.RegisterType<ClientSyncDataWarehouseService>().As<IClientSyncDataWarehouseService>().InstancePerLifetimeScope();
}
}
I use dependency injection in other parts of my application which is working fine but not when I use it in my context. The error I get is:
The target context 'PayCompliment.Data.DbContexts.PCContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.
Am I approaching this the wrong way? Any thoughts or comments are much appreciated.
It's an MVC website using EF6 code first.

Visibility ValueConverter Update Logic to MvvmCross v3

I updated an older android project from mvvmcross v2 to mvvmcross v3.
Got one more problem now.
The visibility doesn't work, its doing nothing.
Old solution looked like this (worked fine):
In Setup.cs
protected override IEnumerable<Type> ValueConverterHolders
{
get { return new[] { typeof(Converters) }; }
}
Converters.cs
using Cirrious.MvvmCross.Converters.Visibility;
namespace Test.Droid
{
public class Converters
{
public readonly MvxVisibilityConverter Visibility = new MvxVisibilityConverter();
}
}
Any .axml (change visibility of LinearLayout):
<LinearLayout style="#style/LinearLayoutSmall" local:MvxBind="{'Visibility':{'Path':'TestIsVisible','Converter':'Visibility'}}">
New solution (doesn't work):
In Setup.cs
protected override List<Type> ValueConverterHolders
{
get { return new List<Type> { typeof(Converters) }; }
}
Converters.cs
using Cirrious.MvvmCross.Plugins.Visibility;
namespace Test.Droid
{
public class Converters
{
public readonly MvxVisibilityValueConverter Visibility = new MvxVisibilityValueConverter();
}
}
Any .axml
<LinearLayout style="#style/LinearLayoutSmall" local:MvxBind="Visibility TestIsVisible, Converter=Visibility">
There's probably a problem with the swissbinding syntax or I'm using false classes?
Any help appreciated!
UPDATE
I forgot these lines:
public override void LoadPlugins(IMvxPluginManager pluginManager)
{
pluginManager.EnsurePluginLoaded<PluginLoader>();
pluginManager.EnsurePluginLoaded<Cirrious.MvvmCross.Plugins.Visibility.PluginLoader>();
base.LoadPlugins(pluginManager);
}
I guess its necessary but now I'm having following error:
(from the MvxPluginManager Class)...
I checked all references and the dll/project *.Visibility.Droid.dll is referenced in my mainproject and everywhere else...
Without running and debugging a complete sample of your code I can't see what the problem is. One guess is that it could be in the plugin setup for visibility, but that is only a guess. The debug trace for your app might reveal some information on this.
Alternatively, it might be easier to simply try setting up a new project and getting visibility working in that, then comparing that code back to your existing app.
Value Converters in v3 are documented in https://github.com/MvvmCross/MvvmCross/wiki/Value-Converters.
The preferred way of referencing them is simply to let MvvmCross find them by reflection - see the section on https://github.com/MvvmCross/MvvmCross/wiki/Value-Converters#referencing-value-converters-in-touch-and-droid
A sample app, including visibility, is in: https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/ValueConversion - e.g. https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ValueConversion/ValueConversion.UI.Droid/Resources/Layout/View_Visibility.axml

asp.net ChangeAction - simple tutorial question

partial void OnValidate(ChangeAction action) {
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
i am following this tutorial:
http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf
and am not able to compile the above code. it is in my NerdDinner.Models.Dinner
am i placing it in the wrong location?
here's the class:
public partial class Dinner {
public bool IsValid {
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations() {
yield break;
}
partial void OnValidate(ChangeAction action) {
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
}
here's the error im getting
Error 1 The type or namespace name 'ChangeAction' could not be found (are you missing a using directive or an assembly reference?)
please excuse the beginner's question and please let me know if you need any other details. im almost certain that im missing something fundamental here. can anyone please help with this? just run me through the necessary steps?
You're referencing ChangeAction by its unqualified name, so you have to make its namespace visible from your module:
using System.Data.Linq;
If the above gives an error, you probably also should add the System.Data.Linq assembly to your project's references.

Best practice for ASP.NET MVC resource files

What are the best usage of the following resource files.
Properties → Resources (Phil used this resource for localization in DataAnnotation)
App_GlobalResources folder
App_LocalResources folder
I also would like to know what is the difference between (1) and (2) in asp.net mvc application.
You should avoid App_GlobalResources and App_LocalResources.
Like Craig mentioned, there are problems with App_GlobalResources/App_LocalResources because you can't access them outside of the ASP.NET runtime. A good example of how this would be problematic is when you're unit testing your app.
K. Scott Allen blogged about this a while ago. He does a good job of explaining the problem with App_GlobalResources in ASP.NET MVC here.
If you go with the recommended solution (1) (i.e. as in K. Scott Allen's blog):
For those of you trying to use explicit localization expressions (aka declarative resource binding expressions), e.g. <%$ Resources, MyResource:SomeString %>
public class AppResourceProvider : IResourceProvider
{
private readonly string _ResourceClassName;
ResourceManager _ResourceManager = null;
public AppResourceProvider(string className)
{
_ResourceClassName = className;
}
public object GetObject(string resourceKey, System.Globalization.CultureInfo culture)
{
EnsureResourceManager();
if (culture == null)
{
culture = CultureInfo.CurrentUICulture;
}
return _ResourceManager.GetObject(resourceKey, culture);
}
public System.Resources.IResourceReader ResourceReader
{
get
{
// Not needed for global resources
throw new NotSupportedException();
}
}
private void EnsureResourceManager()
{
var assembly = typeof(Resources.ResourceInAppToGetAssembly).Assembly;
String resourceFullName = String.Format("{0}.Resources.{1}", assembly.GetName().Name, _ResourceClassName);
_ResourceManager = new global::System.Resources.ResourceManager(resourceFullName, assembly);
_ResourceManager.IgnoreCase = true;
}
}
public class AppResourceProviderFactory : ResourceProviderFactory
{
// Thank you, .NET, for providing no way to override global resource providing w/o also overriding local resource providing
private static Type ResXProviderType = typeof(ResourceProviderFactory).Assembly.GetType("System.Web.Compilation.ResXResourceProviderFactory");
ResourceProviderFactory _DefaultFactory;
public AppResourceProviderFactory()
{
_DefaultFactory = (ResourceProviderFactory)Activator.CreateInstance(ResXProviderType);
}
public override IResourceProvider CreateGlobalResourceProvider(string classKey)
{
return new AppResourceProvider(classKey);
}
public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
{
return _DefaultFactory.CreateLocalResourceProvider(virtualPath);
}
}
Then, add this to your web.config:
<globalization requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8" culture="en-US" uiCulture="en"
resourceProviderFactoryType="Vendalism.ResourceProvider.AppResourceProviderFactory" />
Properties → Resources can be seen outside of your views and strong types are generated when you compile your application.
App_* is compiled by ASP.NET, when your views are compiled. They're only available in the view. See this page for global vs. local.

SerializationException with custom GenericIdentiy?

I'm trying to implement my own GenericIdentity implementation but keep receiving the following error when it attempts to load the views (I'm using asp.net MVC):
System.Runtime.Serialization.SerializationException was unhandled
by user code Message="Type is not resolved for member
'OpenIDExtendedIdentity,Training.Web, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null'."
Source="WebDev.WebHost"
I've ended up with the following class:
[Serializable]
public class OpenIDExtendedIdentity : GenericIdentity {
private string _nickName;
private int _userId;
public OpenIDExtendedIdentity(String name, string nickName, int userId)
: base(name, "OpenID") {
_nickName = nickName;
_userId = userId;
}
public string NickName {
get { return _nickName; }
}
public int UserID {
get { return _userId; }
}
}
In my Global.asax I read a cookie's serialized value into a memory stream and then use that to create my OpenIDExtendedIdentity object. I ended up with this attempt at a solution after countless tries of various sorts. It works correctly up until the point where it attempts to render the views.
What I'm essentially attempting to achieve is the ability to do the following (While using the default Role manager from asp.net):
User.Identity.UserID
User.Identity.NickName
... etc.
I've listed some of the sources I've read in my attempt to get this resolved. Some people have reported a Cassini error, but it seems like others have had success implementing this type of custom functionality - thus a boggling of my mind.
http://forums.asp.net/p/32497/161775.aspx
http://ondotnet.com/pub/a/dotnet/2004/02/02/effectiveformsauth.html
http://social.msdn.microsoft.com/Forums/en-US/netfxremoting/thread/e6767ae2-dfbf-445b-9139-93735f1a0f72
I'm not sure if this is exactly the same issue, but I've run into the same issue when trying to create my own identity implementation.
This blog solved my problem.
It seems that the problem is there's an issue with identity serialization in Cassini, but you can get around it by deriving your class from MarshalByRefObject:
[Serializable]
public class MyUser : MarshalByRefObject, IIdentity
{
public int UserId ...
You can't inherit from GenericIdentity then, of course, but you can still implement the IIdentity interface that GenericIdentity implements, so you can use the thing in most places that expect an IIdentity at least.
It seems to be a limitation or a bug of the VisualStudio (Web Development Server), when I used the IIS Express in VS2012 or the full IIS config, the problem was fixed. Like suggested here: https://stackoverflow.com/a/1287129/926064
Solution from "baggadonuts" at This post solved my problem. Copied code below.
[Serializable]
public class StubIdentity : IIdentity, ISerializable
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (context.State == StreamingContextStates.CrossAppDomain)
{
GenericIdentity gIdent = new GenericIdentity(this.Name, this.AuthenticationType);
info.SetType(gIdent.GetType());
System.Reflection.MemberInfo[] serializableMembers;
object[] serializableValues;
serializableMembers = FormatterServices.GetSerializableMembers(gIdent.GetType());
serializableValues = FormatterServices.GetObjectData(gIdent, serializableMembers);
for (int i = 0; i < serializableMembers.Length; i++)
{
info.AddValue(serializableMembers[i].Name, serializableValues[i]);
}
}
else
{
throw new InvalidOperationException("Serialization not supported");
}
}

Resources