Differences between the AppDomain method for obtaining file paths from a cross-site access through WCF and HttpContext

Source: Internet
Author: User

First: Cross-origin. The method for obtaining the path of other website files is System. AppDomain. CurrentDomain. BaseDirectory.

Second, developers do not know which directory the website will be deployed, so they cannot write absolute paths. They only know the relative paths relative to the root directory of the website, to locate the file path, you can only call HttpContext. current. request. mapPath or HttpContext. current. server. mapPath to obtain the absolute path of the file. If HttpContext. Current returns null, how can I access the file?

Answer: System. AppDomain. CurrentDomain. BaseDirectory

Third: Use HttpContext for security

HttpContext context = HttpContext. Current; if (context! = Null) {// can run here, it must be processing ASP.. appendLine ("Url:" + context. request. rawUrl); // you can do it yourself. }
It is a judgment that solves some null problems, so please forget the following unsafe statements.

Conclusion: HttpContext: The current thread refers to the thread related to the current request.

1. Timer callback.
2. Cache removal notification.
3. Callback is completed asynchronously in APM mode.
4. Actively create a thread or submit the task to the thread pool for execution.

In Case 1, 2, 3, access HttpContext. Current will return null.

3. There are two solutions:

1. Add a field to the type to save the reference of HttpContext (before the Asynchronous Start ).
2.
Assign HttpContext to the last parameter (object state) of the BeginXXX Method)

We recommend that you first select the second method, because it can prevent accidental use of data members during maintenance by others in the future.

4. It is not the current thread, so you do not need to discuss it.

Ths: http://www.cnblogs.com/fish-li/archive/2013/04/06/3002940.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.