473,581 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating context

I have been trying to create a mock httpcontext for the
purpose of unit testing. I have a class, Customer, that
uses cookies. I have set it up to be able to take a
httpcontext as a parameter in an overloaded constructor.
I have seen a few references to people that have created
mock httpcontexts but I can not seem to get it to work.

This is what I have right now, it is the result of
working backwards from httpcontext and trial and error.
It compiles fine but the swrq is not set to an instance
of an object at runtime.

SimpleWorkerReq uest swrq;
TextWriter output = new StringWriter();
using(TextWrite r streamWriter = new StreamWriter
("InvalidPathCh ars.txt"))

swrq = new System.Web.Host ing.SimpleWorke rRequest
( "page", "query", output );

return new HttpContext( swrq );

Does anyone have any links to information about this?

Nov 18 '05 #1
3 1878
Seth,

I'm not sure that you can do this. A simple Worker Request by itself doesn't
make for an HTTP Context until it's fed back into the HTTPRuntime via
ProcessRequest, so I'm not surprised this isn't working in fact surprised
that hte cast is working. The sole purpose of HttpWorkerReque st is to be an
input to an instance of the ASP.Net runtime which in turn generates the
context.

Can't you host the ASP.net runtime and run the request through it to get
your Context and hook from inside there?
+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/webblog/
----------------------------------
Making waves on the Web
"Seth" <ss*****@hotmai l.com> wrote in message
news:00******** *************** *****@phx.gbl.. .
I have been trying to create a mock httpcontext for the
purpose of unit testing. I have a class, Customer, that
uses cookies. I have set it up to be able to take a
httpcontext as a parameter in an overloaded constructor.
I have seen a few references to people that have created
mock httpcontexts but I can not seem to get it to work.

This is what I have right now, it is the result of
working backwards from httpcontext and trial and error.
It compiles fine but the swrq is not set to an instance
of an object at runtime.

SimpleWorkerReq uest swrq;
TextWriter output = new StringWriter();
using(TextWrite r streamWriter = new StreamWriter
("InvalidPathCh ars.txt"))

swrq = new System.Web.Host ing.SimpleWorke rRequest
( "page", "query", output );

return new HttpContext( swrq );

Does anyone have any links to information about this?

Nov 18 '05 #2
Hi Seth,

Thank you for posting in Microsoft Newsgroup. Regarding on the issue, I am
finding proper resource to assist you and we will update as soon as posible.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
with no warranties, and confers no rights.)

Nov 18 '05 #3
Hi Seth,
Based on your description, you are wanting to manually create a HttpContext
to with a SimpleWorkReque st object, how ever when you run the app, you
found that the SimpleWorkReque st couldn't be created correctly. Is this the
problem you encountered?

If so, I've reviewed some references on the ASP.NET runtime. In ASP.NET ,
for a client request beccome from a httprequest to a html output stream, it
need to pass through many processes. At the highest level of abstraction,
the ASP.NET worker process accomplishes one main task¡ªhanding the request
over to a chain of managed objects dubbed the HTTP pipeline. The HTTP
pipeline is activated by creating a new instance of the HttpRuntime class
and then calling its ProcessRequest method. As mentioned, in ASP.NET you
have a single worker process running all the time (except that the Web
garden model is enabled) that manages all Web applications in distinct
AppDomains. Each AppDomain has its own instance of the HttpRuntime class¡
ªthe entry point in the pipeline. The HttpRuntime object initializes a
number of internal objects that will help carry the request out. Helper
objects include the cache manager (the Cache object) and the internal file
system monitor used to detect changes in the source files that form the
application. The HttpRuntime creates the context for the request and fills
it up with any HTTP information specific to the request. The context is
represented by an instance of the HttpContext class.

For more detailed information about the ASP.NET Runtime and the pipeline,
you can visit this tech article via the following weblink:
http://msdn.microsoft.com/library/en...PNETHTTPRuntim
e.asp?frame=tru e

So if you need to generate the required internals objects for the
HttpRuntime, otherwise, the HTTP pipline can't start working since the
working enviroment is not prepared well. Also, I've found some one else's
sample for using the HttpRuntime and SimpleWorkReque st outside the IIS(in a
winform application), you can visit the link below to get the sample and
its tutorial aritcle:

http://www.codeproject.com/dotnet/usingaspruntime.asp

This article has provided a complete example for how to Using ASP.NET
Runtime in Desktop Applications(vi sit a aspx page). I think it maybe
helpful to you.
If you have any new findings please also let me know.
Merry Christmas!!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1202
by: vande | last post by:
Hi, I am trying to create com objects in asp.net. The objects are getting created but the context in the object created is null. If I create the same object in asp the object is ok and the context variable is not null. The context is used in com objects to manage the transactions and other activities. Any help in creating the com objects in...
0
600
by: Matt Warner | last post by:
Hi guys, A couple of people have already posted questions about similar issues but haven't had any response. Occasionally, sometimes after running the app for a few hours, it bombs out saying that it could not create a windows handle. On one machine the stacktrace is this:
6
3238
by: DraguVaso | last post by:
Hi, In my application, on some given actions while debugging in Visual Studio, I suddenly get a "System.ComponentModel.Win32Exception was unhandled" Message="Error creating window handle." exception. The problem is that this exception isn't raised somewhere in a method, so it just shows up, and it causes the application to shut down. ...
6
3660
by: Eran Kampf | last post by:
I am trying to dynamically create directories in my ASP.NET application (I am using Server.MapPath("/")+"test" as the folder) and I am getting a DirectoryNotFoundException saying "Could not find a part of the path "D:\". My site is hosted on a public ISP that for obvious security reasons does not allow my read access above my wwwroot folder...
3
7451
by: Ivonne Riedel | last post by:
Hi everybody, I have got a serious problem building an HTTPHandler in codebehind style. I made the following steps: Create an ASP .net Website project Add a generic handler. Rewrite this "Hello World" item into codebehind style so that the .ashx file has the only line: <%@ WebHandler Language="C#" Class="handlerpur"...
4
2739
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. It should put a blue and yellow box on the page with "This is a test" as part of the picture. But what I get is a broken Gif. The other problem is...
1
6310
by: Frijoles | last post by:
Hello, I have a function that accepts an Image as the input. Currently, I load a bitmap in the calling class and pass that in. I'd like to create an overlay with another image and then pass the resulting bitmap in to the aforementioned function for it to finish everything. The problem I'm having is that after I combine the two images, I...
3
4010
by: Allan Ebdrup | last post by:
I get the error "Error creating control: ID property is not specified" when I view my custom web control in design view. I add the control that gives an error in OnInit like this: ----- foreach (WizardStep ws in WizardSteps) { CustomWizardStepHeader header = new CustomWizardStepHeader(); header.ID = "CustomWizardStepHeader";...
9
2969
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I read somewhere "using kernel stuff in thread is not good.." if ManualResetEvent object is created in thread but not actually used, will it affect performance? Bob
0
7876
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7804
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.