Skip to content

Archive

Category: C#

Generally most of the codes of the following methods to create a path to use:

1
string path = somePath + "\\" + filename;

But if the program is run under Mono on Linux problem on the Linux route because this time there are as follows:

1
/somepath/filename

 

 

It is therefore recommended for paths in your program, Path.Combine method in the System.IO namespace to use.Because this method of preparation of the final values ​​Path.VolumeSeparatorChar Path.DirectorySeperatorChar and uses. The values ​​in Windows (\) and Linux (/) varies automatically at runtime by the framework will be used by management.
The other advantage of using Path.Combine, the input is valid, this means that if the unauthorized use of characters, an exception will be issued.
Just add another one might not be bad and it is Environment.NewLine philosophy. The usual practice is that a new row with \n at the end of a string, but this is not always true and in different platforms. Windows Environment.NewLine equal \r \n and Unix-based systems in equal \n will be. Therefore it is better instead of the \n use of Environment.NewLine for the construction of new lines.

The following console application and two copies conventional parallel processing version of a simple test for this measure provides:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace ObjectInitSpeedTest
{
    class Program
    {
        static void Main()
        {
            normalSpeedTest();
            parallelSpeedTest();
            Console.Title = "CLR Speed test";
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("http://www.ehsanenaloo.com");
            Console.WriteLine("Press any key ...");
            Console.ReadKey();
        }

        private static void parallelSpeedTest()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("ParallelSpeedTest");

            long totalObjectsCreated = 0;
            long totalElapsedTime = 0;

            var tasks = new List<Task>();
            var processorCount = Environment.ProcessorCount;

            Console.WriteLine("Running on {0} cores", processorCount);

            for (var t = 0; t < processorCount; t++)
            {
                tasks.Add(Task.Factory.StartNew(
                () =>
                {
                    const int reps = 1000000000;
                    var sp = Stopwatch.StartNew();
                    for (var j = 0; j < reps; ++j)
                    {
                        new object();
                    }
                    sp.Stop();

                    Interlocked.Add(ref totalObjectsCreated, reps);
                    Interlocked.Add(ref totalElapsedTime, sp.ElapsedMilliseconds);
                }
                ));
            }

            Task.WaitAll(tasks.ToArray());

            Console.WriteLine("Created {0:N} objects in 1 sec\n", (totalObjectsCreated / (totalElapsedTime / processorCount)) * 1000);
        }

        private static void normalSpeedTest()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("NormalSpeedTest");

            const int reps = 1000000000;
            var sp = Stopwatch.StartNew();
            sp.Start();
            for (var j = 0; j < reps; ++j)
            {
                new object();
            }
            sp.Stop();

            Console.WriteLine("Created {0:N} objects in 1 sec\n", (reps / sp.ElapsedMilliseconds) * 1000);
        }
    }
}

I understand where Web services used by the script actually calls the current site and not by an external program?

Here we can help the source of ASP.NET MVC was: (+). IsAjaxRequest same method in both ASP.NET Webforms can be used:

1
2
3
4
5
6
7
8
9
public static bool IsAjaxRequest(this HttpRequestBase request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
return (request["X-Requested-With"] == "XMLHttpRequest") ||
((request.Headers != null) &amp;&amp; (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
}

IsAjaxRequest result should be the beginning of all requests received. Of course, had to be careful that this study can be easily bypassed (because headers have been based), but still is better than no surveillance.

option-1: edit My Documents\IISExpress\config\applicationhost.config file and enable windowsAuthentication

option-2: Unlock windowsAuthentication section in My Documents\IISExpress\config\applicationhost.config and add following in web.config

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
    <security>
      <authentication>
        <windowsAuthentication enabled="true" />
      </authentication>
    </security>
    </system.webServer>
</configuration>
A Visual Studio 2010 Service Pack (which at the time of writing this beta version of its proposed), a new option to right click menu on project name in the name of VS.NET Use IIS Express, has added up could be easily This new option can be used (or in other words is integrated with IIS Express and requires no special regulation).
In other states (and copies, and that there will be no integration) can be acted as follows:
First method:
Following command on the command line Enter:
“C:\Program Files\IIS Express\iisexpress.exe” /path:D:\Projects\Test\MySiteTest\ /port:4986 /clr:v4.0

The case for providing accessible web server path mentioned site on port 4986(http://localhost:4986/) based on NET 4 will be established (for example for three and a half NET v3.5 Enter amount .)
Second method:
which in fact provide the first method behind the scenes it is temporary.
A) The first path My Documents \ IISExpress \ config file referred applicationhost.config open. Then tie the findings related to the site (about 153 rows) and select delete serverAutoStart:
1
2
3
4
5
6
7
8
<site name="WebSite1" id="1">
              <application path="/">
                  <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />
              </application>
              <bindings>
                  <binding protocol="http" bindingInformation=":8080:localhost" />
              </bindings>
</site>

B) setting the desired site to manually add the file. For example:

1
2
3
4
5
6
7
8
<site name="WebSite2" id="2">
              <application path="/" applicationPool="Clr4IntegratedAppPool">
                  <virtualDirectory path="/" physicalPath="D:\Projects\Test\MySiteTest\" />
              </application>
              <bindings>
                  <binding protocol="http" bindingInformation=":Test:localhost" />
              </bindings>
</site>

Slider by webdesign