How to return 504 (gatewayTimeout) in WebApi - c#

In my company API (we use WebAPI 2) we want to return 504 (gateway timeout) when controller action takes to much time. Sometimes inside our WebAPI actions we cooperate with external company APIs, which not always work correctly, so it could be convinient for user.
I added entry in web.config, but it doesn't work.
<system.transactions>
<defaultSettings timeout="00:00:10" />
</system.transactions>
How can I resolve this problem?

timeout is set in number of seconds, and I Think you should use httpruntime executiontimeout instead of transaction timeout wich only affects the transactions within the request
<system.web>
<httpRuntime executionTimeout="10" />
</system.web>

Related

ASP.NET Web API request does not reach controller action

I have trouble posting 'large' objects to my web api using the HttpClient's PostAsJsonAsync method. The objects contain a base64 encoded image file.
After a period which seems like the client's timeout setting, the exception
'A task was canceled.'
is thrown by the HttpClient. The timeout is already set very high (10 minutes). This only happens with files larger than ~2 MB. Anything smaller works fine. I know that the request does not even hit the first line of code in the controller method because the first line is a logging line and the log is empty. There are no exceptions in the server event viewer.
It is hard to pin down the issue because the controller works fine when I deploy the web api on my local IIS. But when I deploy it on my Azure VM, it only work for small files. The web.config files are identical.
maxRequestLength is already set high enough.
<httpRuntime targetFramework="4.5.2" maxRequestLength="1048576" />
Instead of IIS hosting in an Azure VM, i've just tried to deploy the API as an app service. The exact same thing happens there.
You can also try setting this limit:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
Note that contrary to the maxRequestLength parameter, this value is in bytes.

IIS ServicePoint ConnectionLimit always set to Int32.MaxValue

I have a local web application running on IIS which contacts a service deployed on AWS.
While I was testing the throughput against the AWS service I realised that my local web application didn't respect the ConnectionLimit value set in the web.config through the following code:
<system.net>
<connectionManagement>
<add address="*" maxconnection="48" />
</connectionManagement>
</system.net>
Instead the following code always return int.MaxValue
#(ServicePointManager.FindServicePoint(new Uri("https://myawsservice/")).ConnectionLimit)
This seems to contradict what I have read here: http://msdn.microsoft.com/en-us/library/ms998583.aspx#scalenetchapt17_topic14
Increasing maxconnection enables more calls to be executed concurrently to a remote Web service. This attribute does not affect local Web service calls. An increase in the number of concurrent calls causes an increase in the utilization of threads that are used to make the remote calls.
I would expect Int32.MaxValue to only be applicable to local web services. I don't consider my AWS service to be local.
Did I miss something? Did I misunderstood the definition of local web service?
Thanks in advance for your help.
Nico.
Found the issue.
If the processModel autoConfig attribute is set to "true" inside your machine.config then you can't override the ConnectionLimit using the web.config
<system.web>
<processModel autoConfig="true" />
...
</system.web>
Set it to "false" if you want to control the ConnectionLimit but in that case you'll probably want to review the other values set when autoConfig is on.

MVC 'Find' method taking a long time (even in release)? (Using miniprofile)

I'm using MiniProfile to try and figure out why my MVC site is taking a long time. It looks like the 'Find' method is where the time is being spent. I have read a couple things on this and most people seemed to suggest that it was due to having <compilation debug="true" .. > set in the web.conf. I can confirm that in the following example the debug is not set (my compilation looks like the following):
<compilation targetFramework="4.0">
...
</compilation>
No where is debug mentioned. But yet, my find calls are taking upwards of three seconds :S
Any suggestions on how I can figure this out? (Note: I am running on an Azure production server. I have RDC'd into the system to confirm that the pushed web.config does not contain the debug="true").
If this is the first request coming to your website after publishing or after the AppDomain has been brought down by IIS it is normal that the Find method takes a long time. It will then cache the location for this view and on subsequent requests it should be much faster. Try Ctrl+F5ing in your browser on the same url.
Also try explicitly setting debug="false" in your web.config.

asp.net Session timeout issue

got a problem with my asp.net mvc project timing out after only a couple of minutes. It's especially annoying because I've got quite a complex upload and import procedure which falls over when i get logged out. I currently use asp.net membership provider for authentication.
I've tried a few things that I've seen on this site and others but to no avail. Here is what I have so far in the web config:
<location path="Admin/Upload">
<system.web>
<httpRuntime executionTimeout="1200"/>
</system.web>
</location>
<system.web>
<sessionState mode="InProc" timeout="20" cookieless="false" />
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="20" />
</authentication>
<add name="ConnectionString" providerName="System.Data.SqlClient" connectionString="Server=SERVERNAME;Database=DBNAME;User Id=USER;Password=PASSWORD;timeout=30;MultipleActiveResultSets=True" />
I was sure it was only a minute before being timed out before I added the sessionstate timeout, then it was upped to 2 mins, but can't be 100%.
I thought about using this:
<lifetime leaseTime="15M"/>
...but not 100% sure how to implement it - anyone had any success with it, or know something else I could try?
Thanks
EDIT: I'm on a cloud hosting solution, but only have a control panel - no access to IIS
UPDATE: I've now tried adding lifetime leasetime and it's not made any difference:
<system.runtime.remoting>
<application>
<lifetime leaseTime="20M" />
</application>
</system.runtime.remoting>
</configuration>
UPDATE 2:
Ok, I've edited the title and the web config values to reflect my latest effort, but I'm still struggling. I spoke to the hosting company who set the connection timeout to 20 minutes. However, it the session ends after 10 mins. Is there anything else I can try?
I'm getting there, but I would like 20 minutes!
The lifetime leasetime tag goes in the application tag.
<application>
<lifetime leasetime = "15M"/>
I've never actually used it but if it is like other timeouts idk if you will need the M at the end. That is pure speculation though. You should also be able to set it to "0" so that its lifetime is "forever"
As for the session timeout that looks like it should be set for 100 minutes. However, it should be inside of your <configuration> tags
EDIT
Completely unrelated to the question kind of. But i like how your runtime execution timeout is "over 9000"
Right, I got a solution to this, but it wasn't what I was expecting.
It turned out I was using the aspnet Membership Provider wrongly. I was using like a previous membership system I worked on and logging in then setting the UserId as a session variable and using that throughout the site. I then discovered that is the incorrect way to use it, and changed it to a combination of User.Identity.IsAuthenticated and Membership.GetUser().ProviderUserKey.
It no longer times out and all is well. Thanks anyway.

Raise the time-out for a single webservice

My web configuration looks as follows:
<system.web>
<compilation debug="false"/>
<httpRuntime executionTimeout="90"/>
</system.web>
This is fine for most webservice functions but one function has a query that is running a very long (5 minutes) time and will be stopped before it has finished. Is it possible to set the runtime to 5 minutes for this webservice alone?
E.g.:
MyWebServices.asmx?op=WS_LongMethod --> Timeout of 5 minutes
I've thought about running the database query async (fire and forget) but it doesn't seem possible with sybase/oracle through ODBC.
Yes, you can do that. In the web.config, you'll need to add a <location/> element:
<location path="Path_To_Your_Service.asmx">
<system.web>
<httpRuntime executionTimeout="90"/>
</system.web>
</location>
The <location/> element gives you a mechanism whereby you can apply web.config attributes to specific paths within your site.
You can use a delegate within your webservice to call another method asynchronously. Within this method you can do your database IO. This means you will return to the caller before the operation is complete, however if you generate a unique ID and store any info related to that ID, then you can retrieve it at a later date.
Did you check the .Timeout property of web service?
Indicates the time an XML Web service client waits for a synchronous XML Web service request to complete (in milliseconds).

Categories

Resources