Format WCF Response - c#

I have a object return in a WCF and when I do an xml over http the response will look like:
<person>
<name>Hello</name>
</person
I need it to look like
<?xml version="1.0" encoding="UTF-8"?>
<pre:person>
<name>Hello</name>
</pre:person>
Is there an easy way to do it in C#?

Glance over this article it may help you Changing prefixes in XML responses for WCF services.
Or you can try to return XElement.

Related

How to read soap response in c#?

Soap Respose as:
<?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>
<LoginResponse xmlns="http://example.com/SystemIntegration">
<FirstName>#FirstName</FirstName>
<LastName>#LastName</LastName>
</LoginResponse>
</soap:Body>
</soap:Envelope>
I'm trying to read it as:
XDocument doc = XDocument.Parse(strReturnStatus);
List<XElement> result = doc.Elements("LoginResponse").ToList();
for (int intc = 0; intc <= result.Count - 1; intc++)
{
strResponseCode = result[intc].Element("FirstName").Value.ToString();
strResponseText = result[intc].Element("LastName").Value.ToString();
}
But it returning null result.
How to read above respose in asp.net c#??
The easiest thing is to create a proxy class for your service.
You can do that using the 'Add Service Reference' option in Visual Studio. Enter the URL of the service, and Visual Studio will generate the source code for you.
From that point you can access the service using C# code. No need to manually extract the payload of the SOAP message.
Note if you are the implementator of the service: ASMX webservices are deprecated for a long time already. If you can, use WCF.
Use Descendants() method in XDocumentobject to navigate through XML nodes to get elements, you can follow steps provided in this post Using C# to parse a SOAP Response

Initiating Outbound Calls with TWILIO

I have a C# Service using TWILIO API and I am having issues at the moment trying to use the InitiateOutboundCalls function.
private void SendCall(string cellNumber, string message) {
Call callResult;
callResult = _client.InitiateOutboundCall(twilioNumber, employeeNumber, message);
TwilioRestExceptionCheck(callResult);
}
This is the little function in the C# Service which is supposed to call the employee with this twimlbin code (the message variable as above):
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Cams Support. See text for Recording</Say>
<Sms>Sending Recording... </Sms>
<Hangup/>
</Response>
The issue is that even if the employee misses the call, ie: doesn't answer his/her phone, the call status is still set as "complete". Shouldn't the call be set as "no-answer"? How can I tell if the employee picks up or misses the call?
Thank you
Megan from Twilio here.
What is likely happening when an employee misses a call is that the call goes to an answering machine which will actually return the "complete" status that you are seeing.
One way that you might deal with this is to make sure that a call is actually answered by the human employee and not their voicemail using the <Gather> verb.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather action="/complete_call.xml" method="get">
<Say>Press any key to accept the call.</Say>
</Gather>
</Response>
Where /complete_call.xml might look like:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Connecting</Say>
</Response>
You can read more about using this method for Call Screening and see some C# examples there.
Hope you find this to be helpful.

How to retrieve a collection out of an SOAP message

I'm using a webservice for render company information based on a special company number.
But i can not get data out of the response SOAP message.
You can see an example of the response soap message. (I left out some company information the tags).
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<businessGetDossierV3Response xmlns="http://www.webservices.nl/soap/">
<out>
<paging>
<curpage>1</curpage>
<perpage>10</perpage>
<numpages>1</numpages>
<numresults>1</numresults>
<maxresults>100</maxresults>
</paging>
<results>
<item>
<DossierNo></DossierNo>
<SubDossierNo></SubDossierNo>
<ChamberNo></ChamberNo>
<Legalformcode></Legalformcode>
<LegalformcodeText></LegalformcodeText>
<Tradename45></Tradename45>
<EstablishmentPostcode></EstablishmentPostcode>
<EstablishmentCity></EstablishmentCity>
<EstablishmentStreetname></EstablishmentStreetname>
<EstablishmentHouseNo></EstablishmentHouseNo>
<CorrespondencePostcode></CorrespondencePostcode>
<CorrespondenceCity></CorrespondenceCity>
<CorrespondenceStreetname></CorrespondenceStreetname>
<CorrespondenceHouseNo></CorrespondenceHouseNo>
</item>
</results>
</out>
</businessGetDossierV3Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
! this is the result i see on the demo client !
But this is my code in C#
nl.webservices.ws1.BusinessDossierV3PagedResult result = null;
result = myserviceBusiness.businessGetDossierV3(KVKnr, "0000", 1);
I want to do something like this (but with the results instead of paging part)
int page = result.paging.numpages;
Then I should be like this I suppose
string city = result.results.item.CorrespondenceCity;
But this is giving a fault message
So in Visual studio 2010, i can only retrieve the data in the paging part of the xml and put it into a textbox, but not from results part. Is this because the result part is some kind of Collection?
So yes, how can i put the data from the tags EstablishmentPostcode and EstablishmentCity in a textbox on my default page?
thanks in advance
You could try adding your SOAP service as a web reference to the project. http://msdn.microsoft.com/en-us/library/tydxdyw9.aspx
If you don't want to do this and would rather work on the XML directly you could use xpath to get access to all the item elements in your results element.
http://www.stardeveloper.com/articles/display.html?article=2009031001&page=1
One thing to be careful of when using xpath is that you use the correct xml namespace for the node you're trying to select.
Ben

WCF consuming multiple possible REST responses

I've recently started reading about the WebHttpBinding usage in WCF and being able to consume REST services, however, I've been stumped on this one for a bit now.
I'm writing a service that makes a single request to a given api, however, can return one of many responses.
For example, the generic response:
<ActualResponse>
<ResponseItem>
<Name />
<Area />
</ResponseItem>
</ActualResponse>
However, if something were invalid in the outgoing request or the responding service itself was experiencing any sort of issue a returning response would be:
<ErrorResponse>
<Message />
</ErrorResponse>
Pedram Rezaei had a great post on consuming REST services, which is where I borrow most of my information from. From what I can tell, we can create an object as long as the object has serializable attributes defined. The issue being, there's no condition to which class to be created(ErrorResponse/ActualResponse).
I'm unsure if I should be looking at some sort of TryParse functionality that sends the initial request and catches the error if no deserialization can occur or if there is a more elegant approach.
I'm fairly new to the WCF world, so the possibility exists I may be entirely overlooking something!
I think you can borrow some practice from SOAP, which has a hierarchy like so:
<soap:Envelope>
<soap:Body>
... message contents
</soap:Body>
</soap:Envelope>
I'm not suggesting that you use SOAP, I'm suggesting that you learn from the design employed by SOAP. What SOAP does is embed the successful (or in your words "actual") response within the Body, or return a soap:Fault within the Body.
a success in SOAP might look like this:
<soap:Envelope>
<soap:Body>
<ActualResponse>... </ActualResponse>
</soap:Body>
</soap:Envelope>
while a fault might look like this:
<soap:Envelope>
<soap:Body>
<soap:Fault>... </soap:Fault>
</soap:Body>
</soap:Envelope>
In your case, you might have this:
<ServiceResponse>
<ActualResponse> ... </ActualResponse>
</ServiceResponse>
or
<ServiceResponse>
<Fault> ... </Fault>
</ServiceResponse>
And XML Serialization is really good at that. . .
But it sounds like you have no control over the envelope. The fact is that you can get multiple different responses. To handle this, you could wrap the actual response received in a contrived XML envelope, and deserialize the result of that.
If you get <ActualResponse>...</ActualResponse> , wrap it in a deserializable envelope to get something like <ServiceResponse><ActualResponse>...</ActualResponse></ServiceResponse>, then deserialize.

Consuming in Java a .NET Web Service that requires a custom SOAP Header

So I need to consume a Web Service that uses a custom SoapHeader, as described below. What is the simplest way to pass the correct values through this header using Java. I'm using Netbeans.
<?xml version="1.0" encoding="utf-8"?\>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<CustomSoapHeader xmlns="http://snip">
<UserName>"string"</UserName>
<Password>"string"</Password>
</CustomSoapHeader>
</soap:Header>
<soap:Body>
<SomeWebMethod xmlns="http://snip" />
</soap:Body>
</soap:Envelope>
EDIT: What's the best way to display XML on Stack Overflow?
It might help to add that the Web Service is implemented in .NET and I cannot change the server side code.
Here are the basic steps, assuming you're doing this on the client side:
Install a HandlerResolver on your service interface (service.setHandlerResolver())
Override HandlerResolver.getHandlerChain() to insert your own implementation of SOAPHandler
Implement SOAPHandler.handleMessage() to modify the SOAP header before it's sent out
You can pass parameters to your handler through the request context:
Map<String, Object> context = ((BindingProvider) port).getRequestContext();
context.put("userName', "foo");
...
in handleMessage() you can get at the header like this:
public boolean handleMessage(SOAPMessageContext context) {
...
SOAPMessage msg = context.getMessage();
msg.getSoapHeader();
...
}
Hope that helps. I'm guessing there's also a way to do this stuff with annotations as well.

Categories