ASMX not throwing SOAP faults - asp.net

My ASP.NET 3.5 web service (asmx) is not throwing exceptions as SOAP faults. Everything I've seen says this is the default behavior but mine is sending all exception information as text/plain. This is a new web application project with one service added. No other changes from out of the box behavior. How do I get SOAP faults?
Code:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
int test1 = 0;
int test2 = 5 / test1;
return "Hello World";
}
}
Result:
HTTP/1.1 500 Internal Server Error
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 25 Oct 2011 21:39:23 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Content-Length: 201
Connection: Close
System.DivideByZeroException: Attempted to divide by zero.
at WebApplication.WebService.HelloWorld() in C:\....cs:line 23

ASMX web services will only respond with SOAP if the request was SOAP. The testing page generated for you by the service doesn't include the SOAP request envelope:
POST http://server/service.asmx/HelloWorld HTTP/1.1
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
Host: server
A proper SOAP request looks like this:
POST http://server/service.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://server/HelloWorld"
Host: server
Content-Length: 283
Expect: 100-continue
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<HelloWorld xmlns="http://server/" />
</soap:Body>
</soap:Envelope>
Notice the SOAPAction header field and the SOAP envelope content. If the request is a valid SOAP request the service will respond with a SOAP response. For the initial example, the following SOAP fault is the response:
HTTP/1.1 500 Internal Server Error
Date: Sat, 05 Nov 2011 16:55:17 GMT
Content-Type: text/xml; charset=utf-8
Content-Length: 695
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>
System.Web.Services.Protocols.SoapException: Server was unable to process request. --- > System.DivideByZeroException: Attempted to divide by zero.
at Service.HelloWorld() in C:\Test\Service.asmx.cs:line 35
--- End of inner exception stack trace ---
</faultstring>
<detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>

Related

ASP.NET Web API with Windows Authentication lacks header WWW-Authenticate in response

I understand that, for a secured request, a client does an anonymous request first, gets a 401 response and a WWW-Authenticate header and then retries using the received Authentication system.
Now, I have the following code: (if something is missing, let me know)
web.config:
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<authentication mode="Windows"/>
</system.web>
Controller.cs:
[Authorize]
public class FunctionalLocationsController : ApiController
{
// ..
}
WebApiConfig:
config.EnableCors(new EnableCorsAttribute("*", "*", "*")
{
SupportsCredentials = true
});
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });
Request (fiddler):
GET http://localhost/MADI.Backend.WebApi/api/functionallocations HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: en-GB,en;q=0.5
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost
Connection: Keep-Alive
Response headers:
HTTP/1.1 401 Unauthorized
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Fri, 04 Jan 2019 13:44:17 GMT
Content-Length: 6084
The response also contains the human message in Html about the 401.
Does anybody know why it is NOT returning the WWW-Authenticate header?
thanks
Sigh... I found it myself 10 minutes after posting here...
Windows Integrated Security is something you need to explicitly enable in your Windows Features.
I figured <authentication mode="Windows"/> enabled it, but that's only the case when it is also installed. Else it silently does nothing (to my knowledge).
it works now.

restlet get not sending close response

I have the following get in my restlet app:
#Get
public String represent(Variant variant) throws ResourceException
{
String text = "returntext";
text+="\r\n";
return text;
}
When checking the response from invoking this service, I get the following:
CFG - HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Type: text/plain;charset=UTF-8
Date: Mon, 29 Jul 2013 19:59:37 GMT
Server: Restlet-Framework/2.0.9
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Length: 118
Connection: keep-alive
Shouldn't the Connection be: closed as opposed to keep-alive?
I think is causing the connection to get tied up because I can only invoke the service once.
#Ab is correct. http 1.1 has a keep alive. This was the problem.

Error when initializing a RavenDB documentstore that contains a machinename in the URL

I get an error when initializing a RavenDB documentstore that contains a machinename in the URL, combined with specifying a non default database i.e Multitenant.
I can only get store.Initialize() to work if I do not specify a default database.
My ConnectionString is http://somemachinename:8080;ResourceManagerId=d5723e19-92ad-4531-adad-8611e6e05c8b
Following code fails
var store = new DocumentStore
{
ConnectionStringName="RavenDB,
DefaultDatabase="TNW_Grc_CLient",
EnlistInDistributedTransactions=false
}
store.Initialize(); // fails with the following exception
with this message
Message = "Value cannot be null.\r\nParameter name: g"
Scenario:
a) The first time (if the database does not yet exist) this is called,
the call succeeds and I note that the raw http response is:
HTTP/1.1 201 Created
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
Location: /docs/Raven/Databases/TNW_Grc_Client
Server: Microsoft-HTTPAPI/2.0
Raven-Server-Build: 427
Date: Fri, 26 Aug 2011 19:45:23 GMT
X-Charles-Received-Continue: HTTP/1.1 100 Continue
Expires: 0
Cache-Control: no-cache
{"Key":"Raven/Databases/TNW_Grc_Client","ETag":"00000000-0000-0300-0000-000000000001"}
b) The second time the call fails. Debugging some, it appears the etag header is not sent from
the server but is somehow expected by the client code:
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
Server: Microsoft-HTTPAPI/2.0
Raven-Server-Build: 427
Date: Fri, 26 Aug 2011 19:48:10 GMT
Expires: 0
Cache-Control: no-cache
{"Id":null,"Settings":{"Raven/DataDir":"~\\Tenants\\TNW_Grc_Client"}}
You can specify values on the connection string or on the document store properties, not both at the same time.

IIS7 overwriting defined HTTP header values

I am attempting to set the content-type of an asp.net .ashx file to text/plain.
When I run this through the ASP.NET Development Server, the content-type is properly set. When I serve it through IIS7, however, the content-type (and any other header values I set) don't come through (it came through as text/html).
The only value set in the HTTP Response Headers section of IIS Manager is the X-Powered-By attribute. I tried setting the content-type here, but that didn't work. But if I removed the X-Powered-By attribute, it was removed from the header.
Any ideas?
Code in .ashx file
public class Queries1 : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("hello");
}
public bool IsReusable
{
get { return false; }
}
}
HTTP Header from IIS7 (pulled through python script):
[('content-length', '58'),
('x-powered-by', 'ASP.NET'),
('server', 'Microsoft-IIS/7.0'),
('date', 'Thu, 21 Oct 2010 15:51:28 GMT'),
('content-type', 'text/html'),
('www-authenticate', 'Negotiate, NTLM')]
To add HTTP Headers you need to use:
context.Response.Headers.Add("MyHeader", "Hello World!");
Based on Coding Gorilla's clarification, are you sure you're browsing to the correct url? If I try the exact same code as you've written I see the following in Fiddler:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Thu, 21 Oct 2010 20:11:44 GMT
Content-Length: 5
hello

ASP.NET WebService, I want to add some custom HTTP headers onto a ASP.Net web service

I want to add some custom HTTP headers onto a ASP.Net web service to allow cross site access.
the result I need is something like this.
i.e,
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: X-Requested-With
Access-Control-Max-Age: 86400
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope >
<soap:Body>
.....
</soap:Body>
</soap:Envelope>
I tried this but it doesn't work
<WebMethod()> _
Public Function SomeMethod(blabla...) As Something
...
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*")
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "X-Requested-With")
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "86400")
...
End Function
I'm using Don.Net 2.0
any idea?
thanks in advance.

Resources