Referencing code in App_Code from web.config - asp.net

I have a type in my App_Code folder from a Web Site project that I want to refer to in Web.config. The type attribute is requiring me to put in an assembly name. The internets is failing me with what to put in for the assembly.
Specifically in,
<system.web>
<webServices>
<soapExtensionReflectorTypes>
<add type="MyType, $App_Code$" />
</soapExtensionReflectorTypes>
</webServices>
</system.web>
What do I put in $App_Code$ to make it compile? I've tried _ _ code, App _code, App _Code (Markdown is failing here: those type names don't have spaces in them)

OK, I found the answer on some obscure MSDN forum: you can't do that in a Web Site project for system.web/webservices/soapExtensionReflectorTypes. Only a Web Application Project will suffice.

I found a work around for a Web Site project. I moved the type out of the App_Code folder and put it in a new class library, compiled and added to the bin directory.
Now the web.config section finds the type through
<soapExtensionReflectorTypes>
<add type="MyNamespace.MyType, FileTitleOfLibrary"/>
</soapExtensionReflectorTypes>
The second argument is the filename of the library without the file extension.

Related

ASP.NET, two Web.Config

I have a .NET application with all the aspx pages and the main web.config in the root folder but I now have a situation where I need a portal like section for other users. I have created a new folder with it's own unique aspx pages and a second web.config.
I especially need a second web.config to have the <authentication> with a <forms loginUrl="" defaultUrl="> for this portal section and a <authentication> too.
I have implemeted this second web.config in the way I created the first and I am getting this error message:
It is an error to use a section registered as allowDefintion='MachineToApplication'
beyond application level. This error can be caused by a virtual directory not being
configured as an application in IIS.
Now I think the main web.config file is conflicting with the this second webconfig in it's folder. I'm not sure how to fix this, I have seen theories on how this works but no solid code.
Thank you for any suggestions and your time, it is much appreciated.
In IIS, create a virtual directory, under your main application. The local web.config will overide stuff in your main web.config, but still use you main web.config for everything else (i.e. ConnectionStrings, encoding, etc.).

Owin error with ASP.NET MVC application

I have an ASP.NET application that runs fine on my local machine. I just uploaded it to a server using web deploy. I'm getting the following error when I try to view the site:
The following errors occurred while attempting to load the app.
- The OwinStartup attribute discovered in assembly 'Gators3' referencing startup type 'Gators3.Startup' conflicts with the attribute in assembly 'MyFirstProject2' referencing startup type 'MyFirstProject2.Startup' because they have the same FriendlyName ''. Remove or rename one of the attributes, or reference the desired type directly.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.
I tried searching the entire solution for the string "MyFirstProject2" but did not come up with anything. The message gives a couple of suggestions, but none of them mean anything to me. I don't know how to "Remove or rename one of the attributes, or reference the desired type directly," and I don't see a place in the web.config to "add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config."
I found this, but am not quite sure how to implement it. I also did find [assembly: OwinStartupAttribute(typeof(Gators3.Startup))] in my Startup.cs, but not sure what the right thing to do there is either.
The problem is that the Gators3.dll and MyFirstProject2.dll are in the same folder (I guess it is bin folder on your server) and both are using Owin middleware. If you do not need MyFirstProject2.dll then the easiest way would be to delete that assembly. If you need it, but without Owin - add following line to Web.config/app.config in your MyFirstProject2 project:
<add key="owin:AutomaticAppStartup" value="False" />
If you need to use Owin for two projects configure friendly names for both of them.
Gators3 project:
Change Owin startup attribute to something like:
attribute [assembly: OwinStartupAttribute("GatorsConfig", typeof(Gators3.Startup))]
And add following line to Web.config within appSettings section:
<add key="owin:appStartup" value="GatorsConfig" />
MyFirstProject2 project:
Change Owin startup attribute to something
like:
attribute [assembly: OwinStartupAttribute("MyFirstProject2Config", typeof(MyFirstProject2.Startup))]
And add following line to Web.config within appSettings section:
<add key="owin:appStartup" value="MyFirstProject2Config" />
I had the same issue : removing everything in the /bin folder and rebuilding the solution alone worked for me. But it could be combined with renaming your assembly attribute at the top of the startupclass, giving it a Firendly name which will help to differentiate both the startup files.
[assembly: OwinStartup("MyFriendlyNameForProject1",typeof(MyProject.Startup))]
Clear your bin folder and obj folder.Rebuild the project again and run :)
Also, if you publish a solution to Azure:
1) right click and select Publish.
2) go to Settings and expand the "File Publish Options"
3) select "Remove additional files at destination"
4) Publish
Worked for me, after deleting the files from obj and bin Folder.
Delete anything that says 'MyFirstProject2' from your bin folder and rebuild the solution, It will work.
Happens when you reference (by mistake?) a project with owin startup inside another project with owin startup. Fix - delete the reference, bin, obj folders and rebuild.
I had the same problem and i added the following tag on web config:
<appSettings>
<add key="owin:AutomaticAppStartup" value="false" />
</appSettings>
This is what I have done:
Since I have 3 projects in one solution, I had to open all bin folder of each project and delete all files there.
Build each project one by one.
Build the whole solution.
Problem solves on my part.
Remove all files in your 'bin' folder and rebuild.
Delete files Bin, Build each project one by one.
Deleted everything in the bin folder, including the roslyn folder then published, and everything worked fine.
Remove old built data in temporary files, in the following path
C:\Users\[user]\AppData\Local\Temp\Temporary ASP.NET Files\vs
In my case, I was referencing one project in the other accidentally, which is why I was getting this error after removing the one accidentally added in the main project solved the problem.

Problem removing codeSubDirectories elements in sub-application

I have an ASP.NET application with a <codeSubDirectories> section in web.config. My main project is in VB.NET but I have a CSCode subdirectory under App_Code written in C#. Now for every new sub application I create I have to create a CSCode folder under App_Code. Microsoft's documentation says there is a <clear /> element but I get an error when I add it to the sub application's web.config file. What's going on?
Found it. Apparently Microsoft never got around the actually coding the <clear /> element for <codeSubDirectories>. The online MSDN Documentation mentions this in the Community Content section.
As a workaround I'm going to move the CSCode into a separate DLL.
If you didnt have the source code, you can create all subdirectories inside your App_Code subapplication, even with no content. Code works great, in the most cases.
References:
http://systemscenter.ru/avicodeenterprisedotnetmp2007manual.en/appendix-knowledgebase.htm

How to obtain ASP.NET debugging path using reflection?

I'm trying to load an assembly and instantiate a type contained on it using reflection.
The assembly is included in the references and is copied to the Bin folder after publishing, but when debugging, the assembly is not found.
I noticed that each assembly is deployed to a different temp folder for debug, something like:
C:\Users\Rafael\AppData\Local\Temp\Temporary ASP.NET Files\
root\a8b5e5d3\720e0f4c\assembly\dl3\7c6d5cae\d096ebc2_d1c5c901
This way, I cannot use the path of the running, the calling, or an assembly that contains a known type to build the path of the desired one.
Does anyone know how to make it work?
All those folders are added to the assembly search path, so you can say something like Assembly.Load("test.dll"), without supplying absolute path.
Add folowing to app.config:
<system.web>
<hostingEnvironment shadowCopyBinAssemblies="false" />
</system.web>
After that, DevServer will put everything in one folder and reflecting assemly would work.

How to compile a C# file outside of App_Code?

I'd like to stick a class down in my folder hierarchy. The scenario is too trivial to warrant it's own project or separate website. However, I hate to clutter my top-level App_Code with something that's used by a tiny corner of the site.
Is there a way in web.config to include another file or folder in the compilation process?
It sounds like you are using the "Web Site" project type. You might consider switching to the "Web Application" project type which works more like a traditional project, allowing you to have a much more flexible folder structure (code can go anywhere you like, and App_Code isn't a special folder).
This post has a brief discussion and links on the pros/cons of Web Site vs. Web Application projects:
http://forums.asp.net/p/1233004/2232697.aspx#2232697
<configuration>
<system.web>
<compilation>
<assemblies>
<add assembly="<AssemblyName>, Version=<Version>, Culture=<Culture>, PublicKeyToken=<PublicKeyToken>"/>
</assemblies>
</compilation>
</system.web>
</configuration>

Resources