I am trying to import the WebRequestHandler class in a .vb file that is located in the App_Code folder in a Web Forms application using .net framework 4.6.
This does not work and shows a compiler warning when in the class file saying 'WebRequestHandler is not defined'.
Importing and using the WebRequestHandler class works fine in the same project outside of files contained in the App_Code folder.
I have made sure I am using the correct import statement i.e. System.Net.Http.
In the web.config I am targeting .net framework 4.6, WebRequestHandler is available from 4.5 onward.
I have tried installing Microsoft.CodeDom.Providers.DotNetCompilerPlatform version 3.6 and adding the following to the web.config:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
I know that the App_Code is compiled at run time and when trying to run the application it will throw a run time error, but I don't understand why I am able to use the WebRequestHandler in other files outside of the App_Code folder.
Is there a setting somewhere that I am missing to be able to use WebRequestHandler?
You should be able to create and use request handlers.
This code works fine for me:
Imports System.Net.Http
Imports System.Net.WebRequest
Public Class clsTest2
Dim cWebReq As HttpClient =
New HttpClient(New WebRequestHandler())
End Class
You will want to check from the project page, under references this:
Of course, I don't use app_code anymore, since code in that folder is compiled by IIS, and it OFTEN will cause issues - especially if you using Roslyn compiler.
I actually suggest you move all the code modules and routines from app_code to a folder called my code.
Hence like this:
Do keep in mind, that you have to right click, and select properties for each class and code module(s) you add to your "own" code folder like this:
So, set each module and class "build" option to compile.
This will allow far better compile time resolution for such code, and you thus not be relying on IIS to compile that code. All code behind is compiled by Visual Studio, but the one exception is the app_code folder, and source code files are to be included, and they will be compiled by IIS (and you don't want that to occur, since IIS has a far more difficult time resolving references, such as your case).
As my sample code shows, I am freely able to use the imports and use the WebRequestHandler() class.
And at publish time, the code files will not be published nor placed on the web server.
However, to be crystal clear? I don't know how to get those references to correctly work for app_code folder. However, due to this issue, and many others, I have long ago abandoned the app_code folder in favor of my own custom code folder.