Get XML response when calling webservice method - c#

Is it possible to get XML response parallel with calling webservice method;
For example:
var ws = new WebReference.WService();
String result = ws.HelloWorld();
I need to get XML response from webservice for HelloWorld() function like
<string xmlns="http://tempuri.org/">Hello World</string>
and parallel string result in variable result = "Hello World"

In order to avoid calling the same method twice, you'll need to move to a WCF approach, implementing an IEndpointBehavior behavior to read the raw communication with your webservice endpoint.
An example for such behavior can be found at Generic WCF Host.

This is answer https://stackoverflow.com/a/13779722/1179895. But it is necessary to add service reference to project as a service, NOT webservice.

Related

How to send JSon array to remote asmx webservice using c#?

In my application i've to send request to a remote asmx web service which is asking for a number of parameters. There is a parameter which is asking for json array. But i can't figure it out that how can i input my json string in the asmx url.(There is no issues with other string values for other parameters).
Thanks in advance.
It's my understanding that unless the webservice has been heavily customised, you are going to need to pass that parameter in the request body rather than as a GET parameter. For example (with jQuery):
var data = {
paramName: ['this', 'is', 'the', 'array']
};
$.post('service.asmx/Method?getParam1=herp&getParam2=derp', data)
.done(function(d)
{
console.log('Response: ' + JSON.stringify(d));
});
It's been a while since I used an ASMX service, and I'm not 100% positive that you can mix parameters like that. I have a feeling that if you put anything in the body, you have to put everything in it.

REST Webservice for HTML5 Server Side Events

At the moment I have an C# console application that exposes Web Services through WebServiceHost those web services are being used by an website but now I'm trying to add SSE to the site.
The code at the client is:
var source = new EventSource(server+'eventSource');
source.onmessage = function (event) {
alert(event.data);
};
But on the server side, when I try to define the contract:
[OperationContract]
[WebGet]
String EventSource();
What the service is returning service is a xml with a String.
What should I do on the server side to create a document available for SSE?
Thanks in advace
If you have an OperationContract, the return Type is always serialized as XML or optionaly as JSON. If you do not want the return value to be serialized define it as Stream.
[OperationContract]
[WebGet]
Stream EventSource();
// Implementation Example for returning an unserialized string.
Stream EventSource()
{
// These 4 lines are optional but can spare you a lot of trouble ;)
OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse;
context.Headers.Clear();
context.Headers.Add("cache-control", "no-cache");
context.ContentType = "text/event-stream"; // change to whatever content type you want to serve.
return new System.IO.MemoryStream(Encoding.ASCII.GetBytes("Some String you want to return without the WCF serializer interfering."));
}
If you build the stream yourself remember to exectute .Seek(0, SeekOrigin.Begin); before you return it.
EDIT:
Changed the command order to set the ContentType AFTER the Header gets cleard. Otherwise you would clear the freshly set ContentType too ;)

RESTful web service returning XML not JSON

I have this simple web service, right now it just looks to see if the part number is A123456789 and then it returns a model number. This will be replaced by logic that will be connecting into a database to check the partno against and then return the actual model number. But at this point I just need it to return some dummy JSON data. However when I use Fiddler and look at the call in the web broswer of http://localhost:PORT/Scan/Model/A123456789 it returns this
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Model: CVS-1679</string>
But when I do a GET in fiddler of the same URI I get
"Model: CVS-1679"
Only under the textview tab.
Why is it being returned in XML (in the browser and text in Fiddler) and not JSON, when I have setup my ResponseFormat to be JSON?
My Code:
[WebGet(UriTemplate = "Model/{partno}", ResponseFormat = WebMessageFormat.Json)]
public string Model(string partno)
{
if (partno == "A123456789")
{
string modelno = "CVS-1679";
return "Model: " + modelno;
}
else
{
string modelno = "CVS-1601";
return "Model: " + modelno;
}
}
ASP.NET webservice return XML / SOAP message by default. In case you want to return Json string, you would need to decorate the Webservice with [ScriptService] attribute. This inform the IIS that this service would be used by ASP.NET AJAX calls. These attribute are part of System.Web.Extensions.
You can define the web method response format by decorating the webmethod with ScriptMethod attribute.
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
However even after decorating the webservice and webmethod by these attribute, the response can still be in XML format. This behaviour happen when the client which makes the request does not set the Request Header content type as “application/json”.
Before return the method call from webmethod serialize to Json string using JavaScriptSerializer
Debugging WebService using Fiddler
It is quite easy to use fiddler to test webservice. Following figure is an example of how to call a Webservice which returns a json string. Note that the request content type is set to application/json. The parameters expected by webserivce is mentioed in the Request Body section.
Note that the request content type is set to application/json.
It is being returned in Json if you look at the format of the data you get...
key: value
or in your case
string Model = "CVS-1679"
When you view it in fiddler your seeing the raw serialization transport from one MS endpoint to the other. The serialisation & De-serialisation elements in the .NET framework take care of transporting it across the wire, so that when you get the object back into your .NET app at the calling end, you get a variable called Model with the value you expect.
If you try to send an entire class you'll see a lot of nested XML tags, but when you get the object in your code, you'll see a first class citizen in the object hierarchy.
The reason it appears in your browser is because, the browser doesn't know how to de-serialise it, and so just displays the text

ASMX Returning a pure string

I have an ASP.NET web service (.asmx). My service is defined like the following:
[System.Web.Services.WebService(Namespace = "http://tempuri.org/")]
[System.Web.Services.WebServiceBinding(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1)]
public class MyService : System.Web.Services.WebService
{
[System.Web.Services.WebMethod]
public string GetResult()
{
string result = "";
int day = System.DateTime.UtcNow.Day;
if ((day % 1) == 1)
result = "odd";
else
result = "even";
return result;
}
}
Currently, if I call this service method, I get the following result:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">even</string>
My issue is, I need to return just the string part. I do NOT want to return the wrapping XML. How do I do this with an .asmx?
Thanks!
Does it need to be an .asmx web service for this? I mean, by excluding the SOAP envelope you're essentially saying "this is not a SOAP web service" as it is, so why not take it a step further and make it a regular .aspx page instead of an .asmx web service.
As a page, what you're trying to do would be trivial. Remove all mark-up from the page, use Response.Headers to edit the response headers accordingly, Response.Write() to output your raw text, and Response.End() to close the response.
Use json
add the required attribute to your web service and your web method and you get what you want.
Web Service Attribute:[ScriptService]
Web Method Attribute:[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
Read a sample Here
Why do you want to get rid of the XML part? The code which is generated by the proxy needs a common format so it can understand and read the data that is being returned. Stripping the XML essentially makes your return data unreadable by the client proxy.

How to get XMLResponse from Webservice in c# Soap or other soap

I want to integrate with webservice writes in php (PEAR SOAP).
Wsdl file is without types definition.
When i was connect to webservice i getting a null response.
In WebServiceStudio i see xmlrequest and xmlresponse, my I get xmlresponse in c# default soap or other soap.
I know what is causing the null response. PEAR SOAP returnx STRUCT[X] as a response type, where X is number of list element.
When I my get XMLresponse and replace this section then it would be cool
Regards
Sorry for my english
The method you need is getLastResponse(), but if it's anything like other, similar HTTP-stream classes, you'll probably need to set a flag to enable capturing the raw requests/responses. Then again, maybe not, since you apparently have to use parseResposne to get the data out of the XML.
Try:
$soapClient->call(some,parameters,here);
$response = $soapClient->getLastResponse();
echo $response;

Categories