Prove me wrong: VB.NET HtmlHelper Extension Method NOT Working in MVC 4 with VS 2012 - vb.net

No matter how many times I try, I cannot get my HTML Helper extension method to work. Here's the test steps I've created, if someone wants to try it out themselves:
Using Visual Studio 2012, I create a new "Visual Basic ASP.NET MVC 4 Internet Web Application" using the "Internet Application" project template.
I create a folder "~/Views/Helpers"
I create a file "DisplayForPropertyHelper.vb" and add the following code:
Namespace TestProject.Extensions
Public Module HtmlHelperExtensions
<Extension()>
Public Function DisplayForProperty(helper As HtmlHelper) As MvcHtmlString
Return MvcHtmlString.Create("TEST")
End Function
End Module
End Namespace
I open up "~/Views/Web.config" and change the following (I add the extensions namespace):
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="TestProject.Extensions"/>
</namespaces>
</pages>
</system.web.webPages.razor>
Compile the project
Choose ANY razor view file, and type #Html.Display-and you will see that the extension method does not show up.
Add to the razor view file #Imports TestProject.Extensions then save and close the file.
Reopen file, and type #Html.Display-and you will see that the extension method does not show up.
You can even try closing VS2012 and reopening the project. Won't make a difference.
I have been battling with this for weeks now. All answers I've found on here and elsewhere are NO help. Someone must have an answer.

Make sure that you prefixed your namespace with your application name. So for example if the application you created was called MvcApplication1 in your ~/Views/web.config you should put:
<add namespace="MvcApplication1.TestProject.Extensions"/>
and not just:
<add namespace="TestProject.Extensions"/>
Yeah, I guess, it's one of those VB.NET thingies :-) Oh and don't forget to close and open the Razor view after making changes to the ~/Views/web.config file, otherwise your changes won't be taken into account immediately (if you run the application it will work of course).

This is not really an answer, but I write it here in hopes to only add (a little) the overall picture.
For me, I discovered that my 'Module' class was not explicitly being modified as 'Public' (the default is Friend when you don't specify a modifier, and that was what was killing mine)
Yes, I realize the O.P.'s example Module is already declared Public already.. but, I just didn't catch it in my own code stack and figured I'd highlight that requirement. As a native C-sharper having to play VB.Netter for the past five years, I get tired of translating C# snippets manually.. and tend to rely on code converters. So, the nifty HtmlHelper I found and converted didn't quite come out right and I neglected to look for this issue in the code snippet that had been generated for me.
As noted in the prior comments, I'd also like to join the chorus and state the following:
I was doing all of the above (so I thought, as I checked the O.P.'s checklist over and over)
I also find writing HtmlHelpers nearly impossible for VB.NET MVC 4 (until today) - the requirements seem to be supported in such a fragile manner
I also despise having to use VB.NET for developing MVC sites (but, I must live with it.. it's part of the legacy code I inherited coming on board my company)
As much as I despise it (for reasons like this one) I will say everything does seem to be supported if you are determined enough to figure it out. (thanks StackOverflow!)
Prior to this discovery, I thought maybe it wasn't working because my application name had dots in it (MyCorp.Web.WorkOrders) vs. (MvcApplication1).. but, my tests conclude that the application name was not the issue. Either way you name it, it will work.
Again, as #Darin already pointed out.. you certainly must include the solution name(space) as part of the 'add namespace' declaration in views/web.config

Related

EdgeNGram: Error instantiating class: 'org.apache.lucene.analysis.ngram.EdgeNGramFilterFactory'

I've set up Solr, so far everything's working just dandy, but now I wanted to add the EdgeNGram functionality to my searches. However, as soon as I throw it into my schema.xml, it starts throwing the error:
org.apache.solr.common.SolrException:org.apache.solr.common.SolrException:
Could not load conf for core collection1: Plugin init failure for [schema.xml]
fieldType "text_en_splitting": Plugin init failure for [schema.xml] analyzer/filter:
Error instantiating class: 'org.apache.lucene.analysis.ngram.EdgeNGramFilterFactory'.
Schema file is /opt/solr/server/solr/collection1/conf/schema.xml
The relevant schema part looks like this:
<filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="15" side="front"/>
The configuration of the rest of the fieldType is fine as I've tested it rather extensively. It's just adding this line that throws the error.
Now, I've done some looking around, and usually, these errors mean that there's a .jar that's missing (at least according to the other two questions posted on here, not relating to NGram specifically though). So I went ahead and digged up lucene-analyzers-common.jar and explicitly added it in my solrconfig.xml, like so:
<lib dir="${solr.install.dir:../../../..}/dist/" regex="lucene-analyzers-common-\d.*\.jar" />
No luck. I know the path is fine though, I included the mysql_connector just this way. Anyways, I grew pretty tired of this error, so I went ahead and included every single .jar that I could dig up:
<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-.*\.jar" />
Nope.jpg
Of course, all of this was accompanied by many bin/solr stop -all and starts, all of them still serving me that pretty, red banner in the Solr Admin. I'm on Solr 5.0.0
Help?
Well, this is awkward, but it's a typical thing to happen to me. Not 5 minutes after I posted this question, I made the error go away [note that I say "error go away", I didn't say "I solved it", because I haven't tested it completely yet].
Anyways, in my filter tag, you see that side="front" tag? Yeah, bad idea apparently. It's odd though, because I found that in the apache docs: https://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters#solr.EdgeNGramFilterFactory, I didn't make it up or anything.
Can anyone explain that?

INotifyPropertyChanged.PropertyChanged implemented and not implemented; Visual Studio build error

I'm seeing a strange build bug a lot. Sometimes after typing some code we receive the following build error.
Class 'clsX' must implement 'Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs)' for interface System.ComponentModel.INotifyPropertyChanged'.
And
'PropertyChanged' cannot implement 'PropertyChanged' because there is no matching event on interface 'System.ComponentModel.INotifyPropertyChanged'.
Those error should never go together! Usually we can just ignore the exception and build the solution but often enough this bug stops our build. (this happens a lot using Edit and Continue which is annoying)
We're using Vb.net and c# mixed in one big solution.
Removing the PropertyChanged event and retyping the same code! sometimes fixes this.
Question:
Has anyone else seen this problem and has some suggestions how to prevent his?
We're using a code generator that causes this error to surface but just editing some files manually triggers this exception too. This error occur's on multiple machines using various setups.
Someone had the same exact issue discussed here. It sounds like there is an issue with this build picking up an old version of a binary. I would try the following in order:
Verify all assembly references use project references where possible within the Visual Studio solution.
Disable build parallelization in case there is some weird file locking issue with concurrent project builds. Go to Tools -> Options, Projects and Solutions -> Build and Run, then set "maximum number of parrellel project builds" to 1. Not the best solution but it may help narrow down the problem.
Disable the Hosting Process in case it's locking some file causing an assembly to not get rebuilt correctly. For C# project go to Project Properties, Debug tab, and uncheck "Enable the Visual Studio hosting process". For VB.NET project you'll need to Unload Project, Edit the project file, and add <UseVSHostingProcess>false</UseVSHostingProcess> to the PropertyGroup of each configuration. Again, not the best solution but you probably won't notice a difference.
Lastly, try doing a Clean + Build to try and resolve the issue when it occurs (I know this is not a fix but it's easy enough to do), also Rebuild may be slightly different than Clean + Build so try the latter if the former doesn't work.
As I can not comment due to lack of appropriate points.
But I would like to share one of my experience:
In an aspx.cs page I was working, used to compile fine and some time gave mysterious error of a variable not defined or function not defined or sometime variable or the function defined two times. I changed possibly each and every variable and function name but there seemed no effect , but after entering a simple space or a new line at any place in the file used to solve the compile error. At one time I tried to save the file (in a different encoding as i am used to experiments) and found that the file was not saving in the correct encoding (i.e. the ansi encoding because the file had a unicode character ), I removed the unicode character and that compile error didn't bothered me again.
This unicode character problem could be (not a hard and fast rule) there so you could check it.
Nuke & restore using source control (TFS instructions here):
Make sure you have everything checked in
Exit Visual Studio
Rename the project directory to .Bak (effectively deleting it)
Reopen Visual Studio and in source control:
Get Specific Version
check 'Overwrite... not checked out' and 'Overwrite ... even if local version matches'
Re-open project
Another problem: Make sure some source files are not newer than the current date (or your date is set back). Often this happens in apps where you are doing logic that requires certain things to happen differently on certain dates. You change your clock to test it, make a revision to the source with the date advanced, set the date back, and viola, rebuild does not rebuild that file.
You say 'typing it in again' - can you try just saving? After 40 years since MULTIX the .net build still decides what has changed by checking the file timestamp.
good luck!
When you get the error, is it always on the VB calling C# side, or vice-versa, or does it work both ways?
If the answer is either of the first two situations, try building the "callee" project within the solution before building the "caller" project to see if it stops the situation.
Also, just in case it may jog something for you to think about, does this error crop up when you change a VB file or a C# file, or is there no correllation?
Oh, and sorry this looks like an answer instead of a comment, I cannot post comments yet (need 50 rep).

Custom Tool Warning: Cannot import wsdl:portType

I am not sure what this error is. Thought I would ask you guys on stack overflow what it could be. I had to change the contract on my service, on a test client that I used, I updated the service reference. Now I am getting this warning. How can I resolve this particular issue.
I found the answers in What does this WCF error mean: "Custom tool warning: Cannot import wsdl:portType" help. In my case, I chose unticking the 'Re-use types' box and that solved it.
Additional Thoughts: SOA, Distributed Objects, & Coupling
The ”Service Oriented” Vision implied by a WSDL and the WS-* standards is that the WSDL itself tells your client everything you need to know use the service. On this vision, unticking the 'Re-use types' box is the correct approach. You shouldn't be reusing types from anywhere except the wsdl.
Ticking the 're-use types' box is more a "Distributed Objects" approach: your client and service become coupled through the types in a shared dll. This is a strong distributed dependency. If the shared objects are updated, the service and all its clients must be updated, all in sync with each other. This is one reason why distributed objects fell very much out of favour and SOA took over.
Unless you company chose (possibly accidentally, by sharing libraries on a Nuget feed) a distributed objects architecture, and understand the costs, I would always untick the re-use types.
It reduces coupling.
This was the first answer I found when searching for a similar problem, but my issue was a [DataContract] attribute applied to an enum without any [DataMember] attributes, making an empty data contract.
I used this as a resource:
http://www.lukepuplett.com/2010/02/empty-datacontract-causes-misleading.html
It appears as though it is advisable to allow WCF to infer a datacontract for enums.
I find that this also can be solved by using the ?singleWsdl instead of just ?wsdl at the end. There are multiple WSDL files that are linked to, so something too naive to browse them can throw errors.
We had this problem today when trying to convert and old WSDL to a .cs fike with dotnet-svcutil.
We solved by replacing:
<xsd:complexContent>
<xsd:restriction base="xsd:anyType" />
</xsd:complexContent>
With
<xsd:complexContent>
<xsd:sequence>
<xsd:element name="deleteThisField" type="xsd:string" />
</xsd:sequence>
</xsd:complexContent>
And then manualy delete the deleteThisField string in the generated result code.

VB.NET 3.5; compiler complaining about nullable types (as in Double?) with "error BC30037: Character is not valid."

Visual studio just started throwing "error BC30037: Character is not valid." at me (while validating a web site) wherever I have use the nullable operator anywhere within one of my VB.NET 3.5 projects. This happened to a colleague some months ago but he doesn't remember how he fixed it.
For example, if I change every instance of Double? to Nullable(Of Double), it compiles just fine, and other usage of nullables in other projects in the same solution are continuing to work just fine.
I've tried cleaning the solution, closing Visual Studio, deleting the Temporary ASP.NET files, and restarting Visual Studio to no avail.
Anything else I could try?
ADDITION:
I thought I would mention that delegate syntax is also failing. I have this piece of code:
Dim hasSkips As Boolean = payments.Where(Function(p) p.Code = "SKIP").Count > 0
Dim isRegular As Boolean = Not hasSkips
payments is an IList(Of Payment). The Payment object has a string property called Code. There are no validation errors or anything visible in Visual Studio that makes it think it doesn't know what that line of code does. Intellisense works just fine, but the compiler has a fit with the syntax. The following two errors accompany the above two lines of code, respectively:
error BC30201: Expression expected.
error BC30451: Name 'hasSkips' is not declared.
It would almost seem as if Visual Studio has no knowledge of the VB.NET 3.5 features it's supposed to know about.
I figured it out. The web site project needs to be instructed which version of the compiler to use, even after you choose the version in the property pages. Added this to my web.config fixed the issue:
<system.codedom>
<compilers>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="OptionInfer" value="true"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
Well, good to know I guess.
I had this problem too - but it was caused by a corruption in the project file - some how the .aspx page was set to be compiled....
And compiling an Aspx page as VB means that the opening '<%#' characters are invalid...
Had to unload the Project- find the offending entry and delete it.
I second Andrew's answer. Double? is C# syntax, it doesn't exist in VB. VB uses Nullable(Of Double).

Castle-ActiveRecord Tutorial with .NET 3.5 broken?

Has anyone tried the ActiveRecord Intro Sample with C# 3.5?
I somehow have the feeling that the sample is completely wrong or just out of date. The XML configuration is just plain wrong:
<add key="connection.connection_string" value="xxx" />
should be :
<add key="hibernate.connection.connection_string" value="xxx" />
(if I understand the nhibernate config syntax right..)
I am wondering what I'm doing wrong. I get a "Could not perform ExecuteQuery for User" Exception when calling Count() on the User Model.
No idea what this can be. The tutorial source differs strongly from the source on the page (most notably in the XML configuration), and it's a VS2003 sample with different syntax on most things (no generics etc).
Any suggestions? ActiveRecord looks awesome..
(This was too long for a comment post)
[#Tigraine] From your comments on my previous answer it looks like the error lies not with the configuration, but with one of your entities. Removing the "hibernate" corrected the configuration so that it geve you the real error, which appears to be that the entity "Post" is not properly attributed for ActiveRecord to create its mapping.
If you further down in the error that it gives, it likely has some details as to what about "Post" failed.
Some common things include:
THe class does not have the [ActiveRecord] attribute.
There is no property with the [PrimaryKey] attribute.
There is no matching table called "Post" (or "Posts" if PluralizeTableNames is "true").
There is no matching column(s) for attributed properties.
Your attributed properties and public methods are not virtual (this one kills me all the time).
The 'hibernate' portion of the key was removed in NHibernate version 2.0.
This version is correct for NHibernate 2.0 onwards:
<add key="connection.connection_string" value="xxx" />
Edit:
I see that the quickstart doesn't come with the binaries for Castle and NHibernate. You must have downloaded the binaries from somewhere; it would be helpful if you could provide the version number of your NHibernate.dll file.
Confusingly, at least SOME of the quickstart has been updated to be current with NHibernate (NH) 2.0, but the latest 'proper' Castle release is still the 1.0 RC3 (almost a year old now), which does not include NH 2.0.
You can go two ways. You can continue using Castle RC3 and in this case you will indeed need to add the 'hibernate' prefix to your configuration entries. Or you can download a build of Castle from the trunk, which should be running against NH 2.0. The problem with the latter approach is that some of the other breaking changes introduced in NH 2.0 might not be fixed in the quick start.
Delete the "hibernate." part for all configuration entries. Your first example is the correct one.