I am working on an ASP.NET MVC3 project, i am using Ajax jquery to communicate with my controllers. i use asp.net caching to store the results before updating the database (SQL Azure).
function SaveCustomersList() {
$.ajax({
type: "POST",
url: '/Customers/SaveCustomersList/',
data: "",
cache: false,
dataType: "text",
success: function (data) {
return true;
}
});
}
The application work fine but not for all users !!
When some users try to update the database the content passed to my DAL objects is null.
this is just happening with the users working on a network.
i have no explanation for that.
thanks for your help.
Here is a list of questions you need to answer to get proper help:
How many instances of your application is running?
Where this application is running and local network or hosted outside?
When you say "users working on a network", what do u mean? Does it mean this application is accessible to users within your network or outside network?
If this application is running in your network, and the communication between Ajax calls and controller is passing through proxy, only then Proxy could be a factor.
As you are using InProc cache the problem It is critical to know if you have multiple instances of this application. You need to understand that the communication between your Ajax and controller is instance specific and if you have local cache to handle it that will work with only one instance. However if you have multiple instance of the same application and using local cache will not work properly because there is no guarantee that connection X served by instance #1 will always be server by instance #1.
If you decided to run application on multiple instance, using distribured cache (i.e. Outside your instance) is the only choice you have comparative to internal (i.e. Insides your instance ASP.NET inproc) Cache.
[Added]
Here is a .net sample application to use Windows Azure Cache:
https://www.windowsazure.com/en-us/develop/net/how-to-guides/cache/
If you wish to use MemCache you can use #smarx solution as described below:
http://blog.smarx.com/posts/memcached-in-windows-azure
What you need to do is set up a distributed cache that allows you to cache data between multiple instances. You can use Azure Caching for this (but it is expensive!).
Setting the caching up is fairly easy, just follow this guide: https://www.windowsazure.com/en-us/develop/net/how-to-guides/cache/
Related
I have two application, both deployed in same cluster.
Now from web, there is an ajax request to get data from api, but it always return 502 Connection refused.
here is my jquery code (web).
$.get("http://10.43.244.118/api/users", function (data) {
console.log(data);
$('#table').bootstrapTable({
data: data
});
});
Note: when I change the service type to LoadBalancer from ClusterIP then it works fine.
ClusterIP services (usually) only work within the cluster. You can technically make your CNI address space available externally, but that is rare and you probably shouldn't count on it. The correct way to expose something outside the cluster is either a NodePort service or a LoadBalancer (which is a NodePort plus a cloud load balancer).
1\Ajax is a front-end call to api.
2\The front-end call is to directly communicate with the user(Browser) and the called end and the api service.
3\ ClusterIP is internal to the cluster,User(Browser) cannot communicate with it.
4\ NodePort and LoadBalancer can expose the internal network, of course, it can be accessed normally.
I am making a Jquery Ajax web api call . That Api call requires me to pass the Api key. I have enabled CORS. Here is how my call looks currently
$.ajax({
type: "POST",
url: http://localhost:83/test/example1,
data: { name: JSON.stringify(myObject), userId: UserId },
dataType: "json",
headers: {
'apikey': 'asdfee'
});
My question is how do I securely pass this key? I don't want to directly expose the value.
Any directions please?
In short, you cannot secure the key on the client side. Anything on the client side is exposed and can be viewed by anyone.
This being said, there are ways you can attempt this.
Try to make it as hard as possible for anyone trying to get your key. This means store in something like local storage and minify your JavaScript code. This isn't 100% but it will make life harder for anyone trying to get it.
Introduce another layer in between. I have done something like this myself, this extra layer is another API. This is where you store the key and this is where you communicate with the other API. So basically you never expose the API key to the client side. Your front end call this new API and the new API calls the protected one. As I said, one extra layer in between but it does help keep the key secure.
In Visual Studio 2015 I have 2 projects in my solution: ASP.NET MVC app and an ASP.NET Web API app. The MVC app uses a different port in IIS express than the Web API app.
In the debugger I see that I end up in the ChangeName method Web API controller, but the parameter never gets set and then in the console I see errors. The errors have to do with cross domain problems.
Is it this complicated to make a jQuery AJAX request to a different domain? When I use fiddler everything works fine.
The domain for the app that the below code is in is: localhost:50675 and I am trying to make a request to another project in the same solution that is localhost:27081
Here is my AJAX request:
$("#btnChangeName").click(function() {
var name = $("#Name").val();
var url = 'http://localhost:27081/api/products/changename';
$.ajax({
url: url,
type: 'POST',
dataType: 'text',
data: JSON.stringify({name: name}),
success: successFuncApi,
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
Here is one of the errors I am seeing in the console:
XMLHttpRequest cannot load http://localhost:27081/api/products/changename.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:50675' is therefore not allowed access.
The response had HTTP status code 500.
I am not sure if the error is due to my controller erroring since the param is null or if it is the root of my problem.
The Error occurs because you send a request from origin (source) to another one.
all you need to do is to enable cross origin in your backend (or your ASP.NET Web API) to be added in the header.
this link will give you more information, and guide you to enable cross-origin requests.
This error might go away, when you move your apps to production (depending on the setup of webservers). As for now, I would try something, as explained here Enabling Cross-Origin Requests in ASP.NET. If this doesn't fit your situation, there are other ways to enable the same thing.
You could also deploy your apps on IIS. Create app for client, then add new app to that app for webapi.
I have been following the online tutorials provides on Azure and WP series. So far I have been able to understand flow of "TODO application" which is built.
I followed the guidelines provided and created my own WP 8.1 "User registration app". I have created the following in Azure.
Mobile Service
Database and required tables and required columns for this application
I have also written StoredProc which provides OUTPUT result upon success and failure.
When I tried to call this storedproc I was not able to access or to be more honest, I don't have any idea on how to call this or go about. I have been looking around places which were pointing to use "Mobile service API" then call the StoredProc function etc which made me more confused.
How to solve this?
What I need to achieve is, from the WP app I have written, the user key-in the required values, click on "register" button, the function should call Storedproc by passing all the values from WP app to StoredProc as parameter, upon successful execution of StoredProc, it returns a RESULT value which my application need to use and save a system generated value in WP isolated storage, so when next time application loads, it will check for this file, if available the application loads if not it will navigate the user to registration page.
I have been able to pull out sample on isolated storage and other stuff, but not able to achieve with respect to StoredProc and sending and receiving the data to and from Azure SQL Server.
Please help - Thanks in advance.
Depending on which sort of Mobile Services backend you have (Node or C#) you can invoke stored procedures using a pattern similar to the following:
function(request, response) {
var mssql = request.service.mssql;
mssql.query("EXEC <<your_sproc_name>> ?", request.query.<<your_query_prop_here>>, {
success: function(results) {
// console.log(results) <-- use for debugging
// return results (or pass to another function)
}
});
};
If this doesn't really answer the question fully it's worth looking at the Azure Mobile Services Custom APIs documentation which will cover this scenario in more detail.
I have web page index.aspx and corresponding server side code index.aspx.cs. This C# code has a method which cannot executed in parallel if multiple clients connect to my website. How can I restrict this?
Here is what the method does. It creates a folder, zip it and make it available for the user to download. My requirement is that when one user is executing this method, some other user should not do this because it will create the same folder again which leads to corruption of data.
I tried using Session objects. But I came to know that session objects are stored per client basis.
Can anyone suggest me some solution?
My immediate advice would be: create a random folder name per request, which would allow you to run them concurrently. However, if that isn't an option then you will need to synchronize using something like lock or Mutex. However, this would only work well if you are returning the result from the current request, rather than zipping it in one request, and letting them download it the next.
Frankly, though, I think that you should do the zip in the request for the zip. Indeed, unless the file will be huge you don't even need to touch the file-system - you can create a zip in-memory using MemoryStream and any of the zip encoders (System.IO.Packaging.ZipPackage for example) - then just hand the client the data from the MemoryStream.
If you are using MVC, this is just return File(contents, contentType). With vanilla ASP.NET you need a few more steps.
The Application context or a static class is application wide. So you can store a flag which indicates that the process is already started. After the procees ended, you can delete the flag.
http://msdn.microsoft.com/en-us/library/94xkskdf(v=vs.100).aspx
And always use Application.Lock when you write to the application state and lock(mutex) when you use a static class.
In your case a static class would be a better solution, because it seems that the application context exist only for compatible purposes to classic asp: Using static variables instead of Application state in ASP.NET
static object mutex= new object();
lock(mutex)
{
//Do the work
}
If you use the classic asp.net session you do not need to do anything because session all ready lock the run of the pages from multiple users.
If you not, then you can follow what Marc suggest, use Mutex.
About the session lock:
Web app blocked while processing another web app on sharing same session
jQuery Ajax calls to web service seem to be synchronous
ASP.NET Server does not process pages asynchronously
Replacing ASP.Net's session entirely