C# - Creating Instances Of A Class Inside A Static PageMethod Thread Safe?
Dec 14, 2010
I am using jQuery to call PageMethods. For certain operations, the current user credentials must be validated and for other operations, I need to call other static methods. Here is some sample code:
Sample #1
[WebMethod]
public static void PostComment(string comment)
{
UserAuth auth = new UserAuth();
if (auth.isAuthenticated)
{
//Post comment here...
}
}
Sample #2
[WebMethod]
public static string GetComment(int commentId)
{
commentDto comment = //get comment data from the database...
string friendlyDate = ConvertFriendlyDate(comment.commentDate);
return friendlyDate + " " + comment.text;
}
public static string ConvertFriendlyDate(DateTime commentDate)
{
string friendlyDate = //call static utility method to convert date to friendly format
return friendlyDate;
}
Will I be safe using these kinds of operations? Am I better to drop page methods and just call a separate ASPX page for my AJAX requests?
View 4 Replies
Similar Messages:
Mar 2, 2010
I have the following code in my ASP.NET project
public sealed class IoC
{
private static readonly IDependencyResolver resolver =
Service.Get("IDependencyResolver") as IDependencyResolver;
static IoC()
{
}
private IoC()
[Code]....
View 2 Replies
May 10, 2010
Is it safe to access asp.net session variables through static properties of a static object?Here is what I mean:
public static class SessionHelper
{
public static int Age
{
get
{
[code]...
Is it possible that userA could access userB's session data this way?
View 2 Replies
Jul 28, 2011
I understand the problem of the static variable inside a class in the web application, now I try to create a simple class to deal with SQL Server for example, so I created Static Method inside this class, this static method used in insert, update and delete from database, I wonder if this is safe, and there is no problems will appear ?
View 10 Replies
Mar 30, 2011
Probably these are two questions in one, I am using one EF context per request, but I want to use one per thread, because I am going to make some complex task in another thread during the request. So, is it safe? If the answer is yes, how to do it? how to store objects in thread and get them back?
View 2 Replies
Feb 2, 2010
I just created a custom membership provider I would like to know if I can make calls to my data access layer and not put my data access code inside the membership methods will that prevent my custom membership provider from being thread safe, for example:
public override [Code]....
CreateUser(string username, string password, string email, out MembershipCreateStatus status){ // DB calls to my data layer}v.s.public override [Code]....
CreateUser(string username, string password, string email, out MembershipCreateStatus status){ // data access }
View 2 Replies
Oct 17, 2010
I want to know , how good / bad is it to save configuration values in a static properties?I am creating an application which reads configuration from an XML file and a DataContext file, instead of sending the CustomConfig class into the configuration.xml each time I tought about using an ReadXml() method on application_start at the global.ascx
the ReadXml() method would save the data in static properties of CustomConfig and whereever I would want to read that particler data I would ask CustomConfig.PROERTY_NAME
My question is ? how smart it is ? how long does IIS would save the data in a static member? (if as long as the application exists then I'm ok as long as I add the ReadXml() in the application_start)
View 2 Replies
Feb 17, 2010
is there any way to stop share the static variable at multiple users....I need to create a new insatnce of static variables or not accessing the same static variables across multiple who are using the same site.....while googling i found like hisSystem.Threading.Interlocked.Increment(ref MyClass.InstanceCounter); by using can I do....or is there any other way to stop accessing the static variables accross multiple instances of my site......and in my scenario i cannot use rely on session variable also
View 2 Replies
Oct 21, 2010
I have a pagemethod that saves data to my database. Initially I worked on editing an existing record. So I'd load a page by adding the FileNumber to the url:
Code:
[URL]
for example. Then on the click of my save button I send all the textbox ojects etc to a javascript function which then gets the values to save, and passes them to my pagemethod. I did this so I won't have to have refresh the page each time a user updates the file. ajxaified it in other word. Now whan creating a new record, I pass -1 as the fileNumber in my querystring. I do this so in my code, I will know it is a new record and I will write the required logic. However once the new file is created, I get the its primary key value by using SCOPE_IDENTITY(). then unless I refresh the page from my javascript function the url will remain
Code:
[URL]
View 16 Replies
Mar 26, 2011
I have a private static field in my Controller class in an MVC web application.
I have a static method in that controller that assigns some value to that static field, I want to apply lock on that static field until some other instance method in the controller uses the value stored in the static field and then releases it.
DETAILS:
I have a controller named BaseController having a static ClientId field as follows and two methods as follows:-
public static string ClientId = "";
static void OnClientConnected(string clientId, ref Dictionary<string, object> list)
{
list.Add("a", "b");
// I want the ClientId to be locked here, so that it can not be accessed by other requests coming to the server and wait for ClientId to be released:-
BaseController.clientId = clientId;
}
public ActionResult Handler()
{
if (something)
{
// use the static ClientId here
}
// Release the ClientId here, so it can now be used by other web requests coming to the server.
return View();
}
View 1 Replies
Jan 10, 2011
Assuming a static method like below is called from ASP.NET page,can a different thread(b) overwrite the value of s1 after the first line is executed by thread(a)?If so, can assigning parameters to local variables before manipulation solve this?
public static string TestMethod(string s1, string s2, string s3)
{
s1 = s2 + s3;
....
...
return s1;
}
Is there are a simple way to recreate such thread safety related issues?
View 3 Replies
Nov 1, 2010
It seems when I try to dynamically create multiple instances of a User Control, I get an Object Reference Not Set To An Instance Of An Object error on any ASP control that I'm using in that User Control
Here's the class where I'm creating them.
[Code]....
Am I able to successfully refer to a panel on the page that I'm calling this routine from simply by ByVal ... As Panel, or should I use HttpContext?
View 4 Replies
Dec 23, 2010
I have a class that maintains a static dictionary of cached lookup results from my domain controller - users' given names and e-mails.My code looks something like:
private static Dictionary<string, string> emailCache = new Dictionary<string, string>();
protected string GetUserEmail(string accountName)
{
if (emailCache.ContainsKey(accountName))
{
return(emailCache[accountName]);
}
lock(/* something */)
{
if (emailCache.ContainsKey(accountName))
[code]...
View 6 Replies
Feb 8, 2011
I have a class which implements IHttpHandler that is designed to handle image resize requests. It handles Urls like so [URL] Currently the handler looks for myimg.jpg on disk, cuts a 100x100 thumbnail (if it isn't already present) and redirects the client to the thumbnail like so Response.RedirectPermanent("/some/virtualPath/to/thumbnail.jpg");
This has been working great, but I would like to avoid forcing the client to issue a second HTTP request. Is it safe to do the following? Server.Transfer("/some/virtualPath/to/thumbnail.jpg") All the MSDN documentation talks about using Server.Transfer() to redirect to an aspx page, so I'm not sure if this is the right thing to do or not.
View 1 Replies
Jan 27, 2011
I have a question about C Sharp ASP.NET:Is there a difference (in code speed, resources) between:public static variable declared in public static class MyGlobals in a 'Code File' template;and the variable declared in a normal 'Class File' template;I use this variable in 2 different Class Files and also in _Default Page codebehind cs file.In fact in my case I need about 20 global variables of type List<string>.
View 9 Replies
Apr 2, 2010
I want to remove checked items from checklistbox (winform control) in class file method which i am calling asynchronously using deletegate. but it showing me this error message:-
Cross-thread operation not valid: Control 'checkedListBox1' accessed from a thread other than the thread it was created on.
i have tried invoke required but again got the same error. Sample code is below:
[code]....
View 1 Replies
Jan 20, 2010
Im creating a messaging system which is a bit like the inbox feature of this forum. So far I have built the page with a datalist (as this is the advice i was given). I just want to know how I should create the tables within the database so that the thread of messages is seen by the right people when they login and view messages. Which is basically the 2 people messaging each other. The system is gonna have user sign in functionality as well.
View 12 Replies
May 21, 2010
The following code is part of a WCF service. Will eventWatcher take up a thread in the ASP .NET thread pool, even if it is set IsBackground = true?
[code].....
View 1 Replies
Jan 13, 2010
Im creating a messaging system abit like the inbox feature of facebook and even this website (its gonna be created as a asp.net website). Now the messages ive been told should go to a database and then from there be displayed on a page. Im gonna be creating this system because its a project for university. Its not going to be published online or anything, just a simple demo in front of suoervisor n examiner. Im gonna simply create a database within the application to hold all the data.Now i need to have user login feature capability so the right messages are displayed to the right users. I wants the messages to be as a "thread". Any idea how this can be done in asp.net? im using visual studio 2008 .net framework 3.5.
View 1 Replies
Feb 3, 2010
With asp.net MVC 2, I have been trying to get Matt Hawley's Localization helper to work in my web application, but I am getting stuck feeding a null into the Language string variable. I can't figure out why I am doing this.
namespace MvcLocalization
{
public abstract class LocalizedControllerBase : Controller
{
public String LanguageCode { get; private set; }
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
LanguageCode = requestContext.RouteData.Values["languageCode"].ToString();
if ( !AppConfig.SupportedLanguages.Contains(LanguageCode) )
LanguageCode = AppConfig.DefaultLanguageCode;
System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture(LanguageCode);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
base.Execute(requestContext);
}
}
}
View 1 Replies
May 5, 2010
I want to use session in thread class but it return null in parent that is the page class.
View 4 Replies
Oct 27, 2010
when we declared as static then we will not create the object to call the static method. then my doubt is how is call the static constructor after creating the object?can you explain cleary?
View 1 Replies
Jan 4, 2010
I have created a data list, which displays the details of 3 students in a horizontal manner. I have retrieved the data's for the data-list dynamically.And it displays as shown below
Guru Ram Ragu
12 13 12
Male Male Male
Chennai Trichy Ooty
...Like this. But I want to make a header for every row as shown below.
Name Guru Ram Ragu
Age 12 13 12
Gender Male Male Male
Location Chennai Trichy Ooty
....How to do this.
View 2 Replies
Oct 1, 2010
Yeah some people would say "Are you crazy using winforms controls inside asp forms"... and I think they are right. But I would say.. "I'm not the only one!!, take a look" [URL] So. Doing some kind of stuff like the previous link. I did the following:
[Code]....
Now. the DocumentCompleted event is not fired (neither ProgressChanged) and I've tried the next: Add the library MsHtml.dll to my project and place the file into my lib folder... I did it... No changes.Try to handle the state of the WebBrowser.ReadyState... I did it... No changes (Actually after receive the ebBrowserReadyState.Complete I tried to print the document with
[Code]....
View 1 Replies
Dec 20, 2010
Is there any limit with amount of time that can be delayed inside a thread that was created inside XMLBased webService's WebMethod?
View 3 Replies