Environment.ProcessorCount in Windows Azure - c#

Let's say I have A3 and in my code which is executed at Windows Azure I write
int numberOfProcessors = Environment.ProcessorCount;
What will go into numberOfProcessors variable? I assume that numberOfProcessors will be equal to zero because msdn claims that Windows Azure doesn't belong to supported platforms for that function. If I'm correct, then what can I use as replacement Environment.ProcessorCount?

I think that you've read the wrong MSDN :) There is no such information in it about azure. Moreover, there is a lot of articles for ProcessorCountusage in Azure environment:
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder builder = new StringBuilder();
// Show the Environment Information
builder.AppendLine("<h2>Environment Information</h2>");
builder.Append("<b>Machine Name: </b>" + Environment.MachineName + "<br>");
builder.Append("<b>OS Version: </b>" + Environment.OSVersion + "<br>");
builder.Append("<b>Is 64Bit Operating System: </b>" + Environment.Is64BitOperatingSystem + "<br>");
builder.Append("<b>Processor Count: </b>" + Environment.ProcessorCount + "<br>");
builder.Append("<b>User Name: </b>" + Environment.UserName + "<br>");
builder.Append("<b>Is Debugger Attached: </b>" + Debugger.IsAttached + "<br>");
// Show the Process Information
builder.AppendLine("<h2>Processes Information</h2>");
foreach (Process process in Process.GetProcesses())
builder.AppendLine(process.ProcessName + "</br>");
// Show the RoleEnvironment Information
builder.AppendLine("<h2>Role Environment Information</h2>");
builder.Append("<b>Curent Role Instance Name: </b>" + RoleEnvironment.CurrentRoleInstance.Role.Name + "<br>");
builder.Append("<b>Deployment Id: </b>" + RoleEnvironment.DeploymentId + "<br>");
builder.Append("<b>Is Emulated: </b>" + RoleEnvironment.IsEmulated + "<br>");
// Display the Resutls
InfoLabel.Text = builder.ToString();
}
and result for it:
from here. So, it definitely work in Azure WebRole.
But if you are talking about the NumberOfCores, then you have to change it manually - it is administrative function:

Related

how to get active user from ip address in same network

I have network IP address can I know the active user based on the particular IP address
I have under details of the same network
"host"+"-" + new Program().GetHostName(ip[0].ToString()).ToString() + "<br>" +
"KeepAlive" + request.KeepAlive + "<br>" +
"Local end point: {0}" + "-" + request.LocalEndPoint.ToString() + "<br>" +
"Remote end point: {0}" + "-" + "--" + "<br>" +
"Is local? {0}" + "-" + request.IsLocal + "<br>" +
"HTTP method: {0}" + "-" + request.HttpMethod + "<br>" +
"Protocol version: {0}" + "-" + request.ProtocolVersion + "<br>" +
"Is authenticated: {0}" + "-" + request.IsAuthenticated + "<br>" +
"Is secure: {0}" + "<br>" + request.IsSecureConnection + "<br>" +
//"username " + "-"+ username + "<br>"+
I'm assuming you're using an HttpListener/HttpRequestListener here, and if so please see HttpListener: how to get http user and password? - the accepted answer there deals with basic auth but it links to an article that details how to request Windows auth, so the client should quote the logged in windows user name to your app.
Ps: Looks like you're creating a log? That string concat code is a bit of a mess and would benefit from being tidied up, maybe using interpolated strings:
$#"Host: {new Program().GetHostName(ip[0].ToString())}<br/>
KeepAlive: {request.KeepAlive}<br/>
Local end point: {request.LocalEndPoint}<br/>
... and so on ...

Update a OneNote page, using the developer API

I tried to update a page in OneNote with the Microsoft reference :
http://msdn.microsoft.com/en-us/library/office/jj680118.aspx
Here's my problem. When i tried to update my page with the correct ID, it throwed me an error saying : Exception from HRESULT: 0x80042000.
Here is my code :
static void UpdatePageContent()
{
ApplicationClass onApplication = new ApplicationClass();
String strImportXML;
strImportXML = #"<?xml version="+"1.0"+" encoding="+"utf-16"+"?>" +
" <one:Page xmlns:one="+"http://schemas.microsoft.com/office/onenote/12/2004/onenote\""+"" +
"ID=\"{5BE09697-903A-45DD-88D4-8AD301A3D91F}{1}{B0}\">" +
" <one:PageSettings RTL=\"false\" color=\"automatic\">" +
" <one:PageSize>" +
" <one:Automatic/>" +
" </one:PageSize>" +
" <one:RuleLines visible=\"false\"/>" +
" </one:PageSettings>" +
" <one:Title style=\"font-family:Calibri;" +
" font-size:17.0pt\" lang=\"en-US\">" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[My Sample Page]]>" +
" </one:T>" +
" </one:OE>" +
" </one:Title>" +
" <one:Outline >" +
" <one:Position x=\"120\" y=\"160\"/>" +
" <one:Size width=\"120\" height=\"15\"/>" +
" <one:OEChildren>" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[Sample Text]]>" +
" </one:T>" +
" </one:OE>" +
" </one:OEChildren>" +
" </one:Outline>" +
" </one:Page>";
// Update page content
try
{
onApplication.UpdatePageContent(strImportXML, System.DateTime.MinValue);
}
catch (COMException e)
{
Console.WriteLine("Error Message : " + e.Message);
}
}
I really don't know how to solve this.
Your XML is not OneNote friendly.
Here's a list of error codes:
http://msdn.microsoft.com/en-us/library/office/jj680117.aspx
You can get rid of the first line, as #Sebastian has stated it's malformed anyway and my experience is that OneNote doesn't need it.
Also, remember that you don't need to send the entire page. You just need to send the page's objId and any updated objects. So one outline needs adding then this should also work:
"<one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/12/2004/onenote\" +
"ID=\"{5BE09697-903A-45DD-88D4-8AD301A3D91F}{1}{B0}\">" +
" <one:Outline >" +
" <one:Position x=\"120\" y=\"160\"/>" +
" <one:Size width=\"120\" height=\"15\"/>" +
" <one:OEChildren>" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[New Text]]>" +
" </one:T>" +
" </one:OE>" +
" </one:OEChildren>" +
" </one:Outline>";
Just this new outline will get added.
If you still get problems (it doesn't complain but the content doesn't update) then check the extra parameters for UpdatePageContent, certainly in the 2013 API one can send a last modified date to check and also there's a parameter to force a local over-write.
There are some issues in the strImportXML string, which causes the update to fail.
Adjust
#"<?xml version="+"1.0"+" encoding="+"utf-16"+"?>" to "<?xml version=\"" + "1.0" + "\" encoding=\"" + "utf-16" + "\"?>"
Add an empty space before the page ID attribute ( + " " + "ID...
instead of + "" + "ID)
Make sure that the page ID is found/ present in your onApplication
hierarchy
Make sure that you reference the COM library with the matching
namespace defined in the one:Page element (e.g. Office 2013/ 15.0
Object Library has another namespace)

Output from an Exception class in App_Code

I am trying to see what exception is happening on my dev clients devserver (our servers are fine) and I have an TestException.cs class that handles the most of my exceptions from my sql statement class.
I have a method like so:
StringBuilder errorMessages = new StringBuilder();
for (int i = 0; i < innerException.Errors.Count; i++)
{
errorMessages.Append("Index #" + i + "\n" +
"Message: " + innerException.Errors[i].Message + "\n" +
"LineNumber: " + innerException.Errors[i].LineNumber + "\n" +
"Source: " + innerException.Errors[i].Source + "\n" +
"Procedure: " + innerException.Errors[i].Procedure + "\n");
}
//Console.WriteLine(errorMessages.ToString());
Response.Clear();
Response.Write("FAILURE");
Response.End();
But the Response.XX won't compile and if I use console.writeline when it hits it on the client server, the browser gets a connection lost webpage.
Is there a better way to do this?
You could try using the Trace class. If you link with a tracewriter in web.config pointing to a text file you can generate logging with Trace.WriteLine. Then you would just need the file uploaded to your network for reviewing.

ASP.NET - how to detect a MAC user

I am trying to detect a MAC user using c#. I have used the following code but it always says unknown when a mac user navigates to my site. It works great for windows users but not for MAC or anything else. Does anyone have any ideas how to pick up on mac users?
Thanks
HttpBrowserCapabilities moo = HttpContext.Current.Request.Browser;
StringBuilder sb = new StringBuilder();
sb.Append("<p>Browser Capabilities:</p>");
sb.Append("Type = " + moo.Type + "<br>");
sb.Append("Name = " + moo.Browser + "<br>");
sb.Append("Version = " + moo.Version + "<br>");
sb.Append("Major Version = " + moo.MajorVersion + "<br>");
sb.Append("Minor Version = " + moo.MinorVersion + "<br>");
sb.Append("Platform = " + moo.Platform + "<br>");
sb.Append("Is Beta = " + moo.Beta + "<br>");
sb.Append("Is Crawler = " + moo.Crawler + "<br>");
sb.Append("Is AOL = " + moo.AOL + "<br>");
sb.Append("Is Win16 = " + moo.Win16 + "<br>");
sb.Append("Is Win32 = " + moo.Win32 + "<br>");
sb.Append("Supports Frames = " + moo.Frames + "<br>");
sb.Append("Supports Tables = " + moo.Tables + "<br>");
sb.Append("Supports Cookies = " + moo.Cookies + "<br>");
sb.Append("Supports VB Script = " + moo.VBScript + "<br>");
sb.Append("Supports ActiveX Controls = " + moo.ActiveXControls + "<br>");
sb.Append("CDF = " + moo.CDF + "<br>");
You can extract OS information from Request.UserAgent.
Macintosh user agent strings are in this form:
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us)
AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16"
"Mozilla/4.0 (compatible; MSIE 5.15; Mac_PowerPC)"
So you could do something like:
public bool IsMacOS(string userAgent)
{
var osInfo = userAgent.Split(new Char[] { '(', ')' })[1];
return osInfo.Contains("Mac_PowerPC") || osInfo.Contains("Macintosh");
}
you can use Request.UserAgent it will return something like this:
"Mozilla/5.0 (Windows; U; Windows NT 5.1; da; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13"..
then you will need to extract the OS
This might help you
http://www.javascripter.net/faq/operatin.htm
You should use native ASP.NET browser caps and just extend them.
What you do is just create App_Browsers/BrowserFile.browser file in your ASP.NET application.
And add this to the file:
<browsers>
<gateway id="MacOS" parentID="Safari">
<identification>
<userAgent match="Intel Mac OS X" />
</identification>
<capabilities>
<capability name="platform" value="MacOS" />
</capabilities>
</gateway>
</browsers>
Doing this will be enough for Browser.Platform to return "MacOS"

How to search Complex Arraylist & Insert at given point

I am trying to create a simple appplication that can read from a text file and write to the text file. I have a main form which is shown below, also i have a secondary form also shown below; there are also getter and setter classes for Customers, accounts and Transactions. What i would like to do is search for a Customer based on any data held, but mostly there account number. How would i get it to return the correct customer with ALL of there information. Linked to this it would be good to use the search as a point for inserting say a new account.
Below is the main form for pritning out the customer and getting the information from the file.
//create account if all ok
if (allInputOK)
{
//create Account
Account temp = new Account(tempAccSortCode, tempAccNumber, tempAccNickName, tempAccDate, tempAccCurBal, tempAccOverDraft, tempNumTrans);
//add to array
Form1.accDetails.Add(temp);
//finish up
MessageBox.Show("Success Account added ");
resetForm();
}
}
foreach (Customer c in bankDetails)
{
lstOutput.Items.Add(" ");
lstOutput.Items.Add(c.getCustomerNumber() + " " + c.getCustomerTitle() + " " + c.getFirstName()
+ " " + c.getInitials() + " " + c.getSurname() + " " + c.getDateOfBirth()
+ " " + c.getHouseNameNumber() + " " + c.getStreetName() + " " + c.getArea()
+ " " + c.getCityTown() + " " + c.getCounty() + " " + c.getPostcode()
+ " " + c.getPassword() + " " + c.getNumberAccounts());
foreach (Account a in c.Accounts)
{
lstOutput.Items.Add("\t" + a.getAccSort() + " " + a.getAccNumber() + " " + a.getAccNick() + " " + a.getAccDate()
+ " " + a.getAccCurBal() + " " + a.getAccOverDraft() + " " + a.getAccNumTrans());
foreach (Transaction t in a.Transactions)
{
lstOutput.Items.Add("\t \t" + t.getDate() + " " + t.getType() + " " + t.getDescription() + " " + t.getAmount()
+ " " + t.getBalAfter());
}
}
}
In the above code for adding an account, where is adds it to the class Account and the arraylist i think is incorrect with the rest of the program on the main form which uses an underlying list to store the customer information and there accounts/transactions.
EDIT: the above code snippet shows the adding of a new account, however it doesnt seem to be working as i need to find the correct customer by searching the customer array, and then inserting it into the correct place. The second snippet of code shows the arraylist with the underlying lists connected to each customer.
Where to begin?
Generally, for querying objects you can use Linq to Objects.
Also, I would recommend against using text files to keep track of things. They are fine at first, but quickly grow unwieldy and slow. On top of that, what if your app crashes before you saved? Look into a "light database" like SQLite (yes, that's one 'L').

Categories