HttpResponseMessage unavailable in App_Code folder of VS 2012 project - c#-4.0

I am writing a Web API project (MVC architecture). I have a utility class with methods that returns an HttpResponseMessage. If this class is placed in a Utility folder everything works. If I attempt to place the class within the App_Code folder, I receive the message "The type or namespace name 'HttpResponseMessage' found not be found."
An example of a method that this occurs with is provided below. What needs to be done to be able to declare a variable or method as HttpResponseMessage within the App_Code folder?
public HttpResponseMessage GetResponseMessage<T>(T item, MediaTypeFormatter mtFormatter)
{
HttpResponseMessage response = new HttpResponseMessage()
{
Content = new ObjectContent<T>(item, mtFormatter)
};
return response;
}

Files outside of App_Code are compiled when Visual Studio calls the C# compiler and passes it the appropriate referenced assemblies (DLLs) based on the references in the csproj file.
Files inside App_Code are compiled when ASP.NET at runtime calls the C# compiler and passes it the appropriate referenced assemblies (DLLs) based on the reference in the web.config file.
To add a reference to System.Net.Http (the assembly where HttpResponseMessage is located), open your web.config file and merge in this setting:
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</assemblies>
</compilation>

Related

.NET Standard 2.0 logging NLOG gives System.TypeInitializationException

I did 2 projects to test out NLog on a .NET Framework 4.6.1 standard console app and on .NET Standard 2.0 Library. My intention is to port as much code as I can to .NET Standard 2.0 for future multiplatform compatibility.
Both share the same code but the .NET Standard version produces an exception.
Here's the code
Console.WriteLine("Writing log");
Logger _errorLog = LogManager.GetLogger("ErrorsLogger");
Logger _tradesLog = LogManager.GetLogger("TradesLogger");
_errorLog.Error("This is the log message!!!");
Console.WriteLine("End log");
Console.Read();
Here's the App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="ErrorsLogger" xsi:type="File" fileName="ErrorsLog.txt" />
<target name="TradesLogger" xsi:type="File" fileName="TradesLog.txt" />
</targets>
<rules>
<logger name="ErrorsLogger" minlevel="Info" writeTo="ErrorsLogger" />
<logger name="TradesLogger" minlevel="Info" writeTo="TradesLogger" />
</rules>
</nlog>
</configuration>
I get the log fine in the .NET 4.6.1 Console app and produces the expected log file with the log message in it.
If I run the .NET Standard 2.0 library through some Microsoft Unit Test project I get this exception when it tries to call _errorLog.GetLogger
UnitTestProject.UnitTest1.TestMethod1 threw exception:
System.TypeInitializationException: The type initializer for
'NLog.LogManager' threw an exception. --->
System.TypeInitializationException: The type initializer for
'NLog.LogFactory' threw an exception. --->
System.MissingMethodException: Method not found:
'System.AppDomainSetup System.AppDomain.get_SetupInformation()'.
EDIT:
Nuget NLOG Version: 4.4.12
Exception thrown in a non static method:
About the non static method to get a better exception:
The exception is thrown at the GetLogger("X") level, which is a non static constructor. The initialization routine crashes even before trying to log something.
In addition, I get a WARNING (yellow exclamation mark) in the error list saying:
Warning The 'configuration' element is not declared.
Adding internal logging doesn't produce any output. This is the configuration i have used, starting from their Internal logging guide:
InternalLogger.LogLevel = LogLevel.Trace;
InternalLogger.LogFile = #"C:\temp\int.txt";
InternalLogger.LogToConsole = true;
InternalLogger.LogToConsoleError = true;
InternalLogger.LogWriter = new StringWriter(new StringBuilder());
InternalLogger.LogToTrace = true;
LogManager.ThrowConfigExceptions = true;
LogManager.ThrowExceptions = true;
Logger logger = LogManager.GetLogger("foo");
I'm administrator and Visual Studio 2017 is started as administrator and I have permission to write in C:\temp ad .NET 4.6.1 console application is able to write in that folder and it's in the same project.
The internal log file is empty and the Unit test project runs the test successfully.
I have no clue of what is happening. No error is thrown now.
Any suggestion to debug the issue is welcome.
An ISSUE on GitHub is already opened.
HERE is a test solution that I made to show you (PASSWORD: logging123). Now that I've updated to Nlog 4.5 you will see that the .NET framework solution throws an error trying to get an old version of Nlog (that I've never referenced) and that .NET Core unit test solution works but doesn't produce any file.
I was experiencing the same issue though I was running .NET 4.7. I updated my NLog package from 4.4.12 to 4.5.0-rc04 and it worked. Knowing its pre-release you may want to be cautions about putting it on live environment though.
Your zipped solution is password protected, so now it is just me guessing, but it looks like you are using app.config to hold nlog.config.
Pretty sure app.config are not being used by NetCoreApps. Try to put your Nlog-config in a separate file called nlog.config and make sure it is Copy Always (In Visual Studio File Properties).

Adding an Owin Startup Class in my project gives a SQL Server not found error

I have added the following startup code for Owin in my Startup.cs file -
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartupAttribute(typeof(Biosimilia.Startup))]
namespace Biosimilia
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
I have added a key in the Web.config file as -
<add key="owin:appStartup" value="Biosimilia.Startup" />
When I run the application with the startup file in place, I get the error -
Unable to connect to SQL Server Database.
If I remove the key and the startup file from the project, I get the following error -
The following errors occurred while attempting to load the app.
- No assembly found containing an OwinStartupAttribute.
- No assembly found containing a Startup or [AssemblyName].Startup class.
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.
If I add a key to my web.config to disable to startup discovery as following -
<add key="owin:AutomaticAppStartup" value="false"/>
I get the following error -
`No owin.Environment item was found in the context.`
I have looked at a lot of posts on the topic here already with no luck. Please help.
I had the following line in my web.config >
This was resulting in a SQL Server connection timed out error. Removed this line from the web.config. The OWIN startup.cs class now runs correctly and the issue is resolved.
It appears that when using Identity, we do not need to have the roleManager explicitly enabled in the web.config.

NLog with DNX Core 5.0

I am attempting to implement NLog logging using ASP.Net 5 and MVC 6. Be default, both DNX 451 and DNX Core 50 are included in the project template.
I am attempting to implement NLog Logging by following the example here.
However, in the sample app, there is the following line -
#if !DNXCORE50
factory.AddNLog(new global::NLog.LogFactory());
#endif
And if I run the app, this line never gets hit because the mvc application has dnx core 50 installed by default.
Is there any loggers that are available for DNX Core 50? If not, what purpose does dnx core serve in the default mvc app - is it actually needed?
Edit: If I remove the #if !DNXCORE50.... line above, I get a the following error -
DNX Core 5.0 error - The type or namespace name 'NLog' could not be found in the global namespace'
DNX Core 5.0 is only necessary if you want the cloud-optimized cross-platform version of the .Net framework; if you still plan on using the MVC app within only a Windows environment, you can remove your dnxcore50 framework reference from your project.json.
NLog for .NET Core (DNX environment) is currently available in version 4.4.0-alpha1.
Steps:
Create NLog.config
<?xml version="1.0" encoding="utf-8" ?>
<targets>
<target xsi:type="ColoredConsole" name="ToConsole" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="ToConsole" />
</rules>
Load and parse configuration
private static ILogger _logger;
public static void LoggerSetup()
{
var reader = XmlReader.Create("NLog.config");
var config = new XmlLoggingConfiguration(reader, null); //filename is not required.
LogManager.Configuration = config;
_logger = LogManager.GetCurrentClassLogger();
}
public static void Main(string[] args)
{
LoggerSetup();
// Log anything you want
}
When dealing with the MVC tooling in MVC6 (dnx stuff), the answer to this is very fluid.
In order to get NLog to work with my web app, I had to do a couple steps:
-> Big thanks to two NLog discussions(here and here)
I just needed to add the configuration setup in my Startup.cs's constructor:
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
// Set up logging configuration
// from: https://github.com/NLog/NLog/issues/641
// and: https://github.com/NLog/NLog/issues/1172
var reader = XmlTextReader.Create(File.OpenRead(Path.Combine(builder.GetBasePath(),"NLog.config"))); //stream preferred above byte[] / string.
LogManager.Configuration = new XmlLoggingConfiguration(reader, null); //filename is not required.
log.Info("NLogger starting");
Configuration = builder.Build();
}
I consider this a bit of a stop-gap as Microsoft is introducing a new Logging interface (that I hope will end up being like SLF4J.org is in Java). Unfortunately, documentation on that is a bit thin at the time I'm writing this. NLog is working diligently on getting themselves an implementation of the new dnx ILoggingProvider interface.
Additional information about my project setup
My NLog.config file is located in the project root folder, next to
the project.json and appsettings.json. I had to do a little digging
inside AddJsonFile() to see how they handled pathing.
I used yeoman.io and their aspnet generator to set up the web project.
Version of NLog, thanks to Lukasz Pyrzyk above:
"NLog": "4.4.0-alpha1"

Log4Net and GAC - How to reference Configuration Files?

I am using log4net during my development, as as part of a project constraint, I now need to add it to the Global Assembly Cache.
The logging definitions are in a file Log4Net.xml. That file is referenced in my assemblyinfo as: [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.xml", Watch = true)]. So long as the xml file was in the same directory as the log4net.dll, everything has been working fine.
However now that I've added log4net to the GAC, it is no longer picking up the xml file.
Does anyone know what I need to change in order to have it pick up the XML file again? Is hardcoding the patch in the assembly reference the only way?
Many thanks
log4net expects the config file to be in the path returned by:
System.AppDomain.CurrentDomain.BaseDirectory
Let your application print this information to some file and then you know where you need to place the config file.
Of course there are other solutions, but then you cannot use the attribute anymore. Calling the ConfigureAndWatch() method directly allows you to figure out yourself where the config file is; you can even decide on a location (does not have to be a hard-coded path).
You might ensure that your log4net.xml file is set to "Copy Always" (Right Click on log4net.xml -> Properties -> Copy to Output Directory = Copy always). To ensure that your config file is being copied, you should check your bin\debug or bin\release directory and verify that the log4net.xml file exists in the same directory that your application executes.
If that doesn't workthen you can try enabling internal debugging in log4net. To enable internal debugging, add the following key to your app.config file. This will send internal log4net debug messages to your Visual Studio Output window (View -> Output).
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
For more information on log4net's internal debugging, you might check Phil Haack's blog post here.
If all else fails, you can turn on internal debugging and explicitly load the configuration by calling log4net's XmlConfigurator.ConfigureAndWatch method.
var fi = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\log4net.xml");
XmlConfigurator.ConfigureAndWatch(fi);

SecurityException using SPWebConfigModification

I'm getting a SecurityException, "Access Denied" when trying to make a web.config modification programmatically.
Code closely follows this example:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// create modification
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebConfigModification m = new SPWebConfigModification();
m.Path = "configuration/SharePoint/SafeControls";
m.Name = string.Format(CultureInfo.InvariantCulture, "SafeControl[#Assembly='{0}'][#Namespace='{1}'][#TypeName='*'][#Safe='True']", ADSWebPart.GetAssemblyFullName(), ADSWebPart.GetNamespace());
m.Sequence = 0;
m.Owner = SPContext.Current.Web.CurrentUser.Name;
m.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
m.Value = string.Format(CultureInfo.InvariantCulture, "<SafeControl Assembly='{0}' Namespace='{1}' TypeName='*' Safe='True' />", ADSWebPart.GetAssemblyFullName(), ADSWebPart.GetNamespace());
// apply modification
SPWebService service = SPWebService.ContentService;
service.WebConfigModifications.Add(m);
service.Update();
service.ApplyWebConfigModifications();
});
}
(I started with no call to RunWithElevatedPrivileges(), got same exception, then continued enclosing more and more code up to enclosing all body of FeatureActivated().)
Ideas welcome, thanks.
I'm not answering your question as asked, but why are you using a Feature receiver to add SafeControl Entries?
The "Right" way to add SafeControl Entries is to embed these inside <SafeControls> inside the corresponding <Assembly> in manifest.xml of your Solution like this:
<Assembly Location="MyLib.dll" DeploymentTarget="WebApplication">
<SafeControls>
<SafeControl Assembly="MyLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4489c7aa5341e32c" Namespace="MyNs" TypeName="*" Safe="True" />
</SafeControls>
</Assembly>
Out of sheer curiosity, is the web.config set to read only? Have you checked the permissions on the filesystem for the web directory in inetpub?
Also, is this being activated on the Web Application level or further down?
Instead of using SPContext.Current.Web.CurrentUser.Name;
Try following the instructions here to impersonate the SystemAccount:
http://blackninjasoftware.com/2009/04/09/how-to-programmatically-impersonate-users-in-sharepoint/
Another question, are you activating the feature from a Forms Based Authentication site? You can have issues because the site collection administrator is from the authentication provider path, and not AD. The Web.Config file security is based on the AD account, so you have no ability to update the web.config without doing something bad, like giving Everyone access to web.config.
Here is an earlier question I posted on the topic.
Modifying SharePoint app Web.config file with Forms Based Authentication

Resources