I have a static method that gets some data from Web Service ("GET"), if i gets HttpWebResponse with some status code (403,401), i have to reset, refresh or reload the Form1_load. Since my method is static, i cannot make instance of Form1. Is there solution for this ?
Related
I have created an authentication library in wp8 which requires me to provide a login screen UI and return the session id and other login details on authentication with server.. I have an API which returns the login data..
Now in the app im going to the login screen using NavigationService.navigate () which automatically instantiates my library class..
My Question is how do I get the reference to object of class that is instantiated by navigate method.. Also it seems that navigate() is asynchronous as it shows my login page and immediately moves to the line after the call.. My requirement is that I have to call the API that returns login data only after authentication has been performed but I have no reference of the instantiated object to call it
Is there any way to make the navigate() wait until the authentication is complete?
You are getting username and password from login screen and you are using this data for authentication.then,you can call your API in login screen's "say Login" button and after that you can use event delegate mechanism to receive web service response status, if its correct then you can navigate to next screen,till then you can use ProgressBar.
please check this code to get understanding of await:
Object response = await Authenticate(UsernameTextBox.Text, PasswdTextBox.Password);
if (response is success)//typecast your object as per your need to get status of result
{
Navigate
}
else
{
Show error dialog
}
Also dont forget to modify your button event handler's signature with "async".
I am in need of calling a non-static method in the active(current) asp page that the user is on by using a static WebMethod. How can I do this?
Both of these methods are within the ASP page's cs file.
public void NormalMethod()
{
txtFindingNum.Text = "Ajax is da bomb";
}
[WebMethod]
public static void MyWebMethod()
{
// This is the part I need help with...
DoIt();
}
You can't do it. It doesn't even make sense.
The instance methods of the page are about a specific instance of the page. When you're in the static web method (page method), there is no instance of the page.
If you could call the instance method from the web method, that would mean that the instance method should be a static method. Can you just add static to that method and have it still work? If not, then it depends on the particular instance of the page, and you simply can't call it when there is no instance.
Note that a page instance exists only during the HTTP request that it is serving. By the time your client-side code is calling the web service, that HTTP request is already over, and the page instance is gone.
You can't do it, but the txtFindingNum.Text field is input, and you can change it in client side (it also keep the change in the server after postback), with js or jq, like this:
$("#<%=txtFindingNum.ClientID%>").val("Ajax is da bomb");
I have placed the call to WebSecurity.InitializeDatabase in the _appstart.cshtml as noted in various google searches but periodically I keep receiving the below error when making subsequent calls to WebSecurity.IsAuthenticated for example, I would assume the initialisedatabase is called once for every user entering the website, is there a delay in this initialization before the _pagestart.cshtml is called?
You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.
at WebMatrix.WebData.SimpleMembershipProvider.VerifyInitialized()
at WebMatrix.WebData.WebSecurity.VerifyProvider()
at WebMatrix.WebData.WebSecurity.Logout()
at ASP._Page__PageStart_cshtml.Execute()
No, there should not be a delay.
To use WebSecurity.InitializeDatabaseConnection, you call once ever with the parameter autoCreateTables= true. After your tables are setup in your database, then from then on in _appstart, you'll call WebSecurity.InitializeDatabaseConnection with autoCreateTables = False.
Say that I have an ASP.NET page with a Label control and the following static class which executes a scheduled job:
public static class Job
{
// The Execute method is called by a scheduler and must therefore
// have this exact signature (i.e. it cannot take any paramters).
public static string Execute()
{
// Do work
}
}
When the job is done, the execute method should update the value of the Label control on the page.
I've done some research and the only way seems to be to use HttpContext.Current.CurrentHandler. However, this is undesirable for me since it can potentially return null.
Since the Execute method cannot take any parameters (see comment), passing the Page instance as an argument is not an option.
Is there any other way to update the control from the static class?
NOTE: the Execute method must be static because I'm creating an EPiServer scheduled job, which requires a static Execute method (that doesn't take any parameters).
If the job is not executed synchronously (or even if it is), I think that you may want to consider the order of control.
What I suggest in a case like this is a structure similar to the following:
1) The web page issues the request for the job
2) Somewhere, a unique reference to the job is created and stored (such as GUID or an identity column in a database table)
3) The job is executed asynchronously by code-behind and then the unique identifier is returned to the web page.
4) The web page, on startup, initiates a javascript method (using window.timeout, for example) that on a regular basis, issues an ajax query to the web server to check on the status of the job.
5) When the job is complete, it updates the global reference with the appropriate information.
6) When the javascript sees that the job is complete, it updates the label.
This process allows the user to carry on with other work if necessary and not have to worry about timeouts due to long postback times, etc.
For your specific scenario, you could add a GUID property to the Job class (which would be passed back to the client).
When Execute is complete, you could add this GUID to a static collection (i.e. Dictionary<Guid, string>) which the ajax request would check (the string value could store status or completion information).
When the ajax request fires, it would check this static collection and, when it finds its job, remove it and return the value to the caller.
You can create a static property that is updated by your Execute method and bind the Text property of the Label to the static property in the aspx's OnInit method, Label.Text = Job.StaticProperty, if you need a somewhat dynamic response you could use the Ajax Timer Control to call a method on the aspx page to return the same static value from the aspx Page.
public static class Job
{
public static string UpdatedValue { get; private set; } // Or whatever the property is you wish to expose.
// The Execute method is called by a scheduler and must therefore
// have this exact signature (i.e. it cannot take any paramters).
public static string Execute()
{
// Do work
Job.UpdatedValue = "Execute Completed";
}
}
protected override OnInit(EventArgs e)
{
base.OnInit(e);
this.TextLabel.Text = Job.UpdatedValue;
}
// Using MSDN basic sample
protected void Timer1_Tick(object sender, EventArgs e)
{
this.TextLabel.Text = Job.UpdatedValue;
}
Try with a global variable (static) or raise an event from Execute() method.
When making calls to a webservice from an asp.net web application, to avoid creating a new proxy for each webservice call, I store the proxy in a dictonary. So when a call to the webservice is being made, an instance of the webservice proxy is returned based on a key passed to a factory method. If a proxy for the corresponding key is already created, that proxy is returned. Otherwise, it creates a proxy for the key and returns the proxy.
This all works fine, unless the actual webservice for some reason "goes down" and don't return any response. If that happens, which it in my case does pretty frequently, a System.Net.HttpWebRequest is created, which never gets collected by the GC. The reason is, the HttpWebRequest is a local variable in HttpWebRequest.GetResponse() which is still being executed (since it is waiting for a response that will never come). Hence, there exist a reference to HttpWebRequest that will keep the object alive.
One way to get around this would be to put the call to the webservice within a Using block, thereby disposing the object after the call has been made. That way, the references to the HttpWebRequest would be cut off so that the HttpWebRequest can be collected by the GC. However, by disposing the proxy, the whole idea of storing the proxy in a dictonary to avoid creating a proxy for each call malfunctions.
Hence, my question is, do anyone have any idea how I could do to solve the issue having references to the HttpWebRequests left when there is no response from the webservice?
Thanx in advance!
Some psuedo-code to explain the concept of storing the proxies in a dictonary:
public Proxy GetProxy(String key) {
if(!proxies.ContainsKey(key)) {
proxies[key] = new Proxy();
}
return proxies[key];
}
public Object GetObject(String key) {
Object object =
this.GetProxy(key).GetObject();
return object;
}
If the target site goes down, you'll likely get a HttpException. Can you leverage that mechanism for managing the lifetime of your instances?