Unwanted Prefix in SoapCore Response in .NET Core - c#

I am writing a SoapCore web service in .NET Core 5.0 and I have successfully been able to execute the request via SOAPUI and get back a valid response. The only thing I don't like is that it puts unwanted prefixes in the repose message. for example in this response:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<s:Body>
<getMeterIntervalDataByTimeSliceResponse xmlns="http://tempuri.org/">
<getMeterIntervalDataByTimeSliceResult xmlns:d4p1="http://schemas.datacontract.org/2004/07/MeterDataWebService.Models" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:AccountId>1234567890</d4p1:AccountId>
<d4p1:ID>0</d4p1:ID>
<d4p1:IntervalEndTime>2020-12-22T15:30:57.3705724-05:00</d4p1:IntervalEndTime>
<d4p1:IntervalLengthInSeconds>PT5M</d4p1:IntervalLengthInSeconds>
<d4p1:Load>2345.8010350836185</d4p1:Load>
<d4p1:MeterChannel>1</d4p1:MeterChannel>
<d4p1:TimeZone>UT</d4p1:TimeZone>
<d4p1:UnitOfMeasure>KM</d4p1:UnitOfMeasure>
<d4p1:UtcOffset>0</d4p1:UtcOffset>
</getMeterIntervalDataByTimeSliceResult>
</getMeterIntervalDataByTimeSliceResponse>
</s:Body>
</s:Envelope>
How do I get rid of the "d4p1" prefix?

I figured it out. It was in my Startup.cs class.
app.UseSoapEndpoint("/Service.asmx", new
BasicHttpBindingg(), SoapSerializer.XmlSerializer);
I forgot to add the SoapSerializer.XmlSerializer part

Related

SOAP Service call - Getting error Illegal Request format for element

We are integrating a service from a third party. They have recently upgraded their service, and now with the new wsdl, I keep getting "Illegal Request format for Element".
From my investigations the problem seems to be with the xmlns that is added on the main element. If I use SOAPUI and remove the xmlns from the main element, it works, however visual studio adds it automatically in accordance with whats defined in the wsdl.
What is interesting is that with their previous wsdl, the service works with the xmlns included, it is only with the new wsdl that it throws an exception.
In terms of the wsdl, all I know is that they used JD 12 and manually created the wsdl, however upon comparing it, it looks similar to the old one with the name in the xmlns being the only difference.
This is the request visual studio creates:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<extractacccategElement xmlns="http://gna160ws/Management.wsdl/types/">
<xSecurity>
<timekey></timekey>
<authkey></authkey>
<publkey></publkey>
<version>1.x</version>
</xSecurity>
<xRequest1>
<supplierno></supplierno>
</xRequest1>
</extractacccategElement>
</s:Body>
</s:Envelope>
With the "xmlns="http://gna160ws/Management.wsdl/types/"" causing the problem.
Response:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header/>
<env:Body>
<srvc:extractacccategElementResponse xmlns="http://gna160ws/Management" xmlns:srvc="http://gna160ws/Management">
<srvc:result>
<response1Out>
<origin>gna160.extractacccategElement</origin>
<invsql>1200</invsql>
<message>Illegal Request format for extractacccategElement.</message>
</response1Out>
</srvc:result>
</srvc:extractacccategElementResponse>
</env:Body>
</env:Envelope>
When submitting the same request, but without the xmlns, I get a valid response. Example:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<extractacccategElement>
<xSecurity>
<timekey></timekey>
<authkey></authkey>
<publkey></publkey>
<version>1.x</version>
</xSecurity>
<xRequest1>
<supplierno></supplierno>
</xRequest1>
</extractacccategElement>
</s:Body>
</s:Envelope>
Response:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<ns0:extractacccategElementResponse xmlns="http://gna160ws/Management" xmlns:srvc="http://gna160ws/Management">
<ns0:result>
<ns0:acccategOut>
<ns0:invsql>0</ns0:invsql>
<ns0:message>Success</ns0:message>
<ns0:origin>gna160pkg.ExtractAccCateg</ns0:origin>
<ns0:stage>1</ns0:stage>
<ns0:acccategcode>A</ns0:acccategcode>
<ns0:acccategname>AAA</ns0:acccategname>
</ns0:acccategOut>
</ns0:result>
</ns0:extractacccategElementResponse>
</env:Body>
</env:Envelope>
Additionally, if I add a qualifer after the xmlns then it also works??
Example:
xmlns:hello="http://gna160ws/Management.wsdl/types/"
I have been working collaboratively with the developer on their side and we have not yet been able to identify the problem.
If anyone could help or point me in the right direction, it would be greatly appreciated.
Did you update your service reference with the new WSDL?
Alternatively you can try this answer from Rick Strahl: override the following function to add a custom namespace.
protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer)
{
writer.WriteStartElement("soapenv", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
writer.WriteAttributeString("xmlns", "oas", null, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
writer.WriteAttributeString("xmlns", "v2", null, "http://www.royalmailgroup.com/api/ship/V2");
writer.WriteAttributeString("xmlns", "v1", null, "http://www.royalmailgroup.com/integration/core/V1");
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
}
Link to his complete article

Web client Soap versionMismatch error c#

Hi I am new at WCF Soap services, I need to connect to a webserver use Xml-Rpc. First I tried to connect and got an error about "xml-rpc encoded" then I added Microsoft.Samples.XmlRpc to project. I can connect to the service but when I try to send Soap message I am getting :
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:hdr="urn:PASAHeader" xmlns:ns3="urn:PASAMnfGetFlowVehInfo_v2" xmlns:ns1="urn:PASAMnfGetFlowVehInfo" xmlns:ns2="urn:PASAMnfUpdatedFromSupplier" xmlns:ex="urn:PASAexploitationWS">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" id="_0">
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:VersionMismatch</faultcode>
<faultstring>SOAP version mismatch or invalid SOAP message</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I am using that code to convert to xml-rpc
ChannelFactory<ServiceReference2.MnfGetFlowVehInfoPortTypeChannel> typeChannel = new ChannelFactory<ServiceReference2.MnfGetFlowVehInfoPortTypeChannel>(new WebHttpBinding(WebHttpSecurityMode.None), new EndpointAddress(uriAdress));
typeChannel.Endpoint.EndpointBehaviors.Add(new XmlRpcEndpointBehavior());
I tried to change the version using messageVersion in library but I couldn't handle it.Thanks

How to set the "ServiceVersion" in WCF client

I have a .NET application that is consuming a Java WCF, and I need to change my Reference.cs to set the ServiceVersion, I don't know how I can do this.
Someone has some idea?
My SOAP XML should be:
<s:Header>
<ns:TocHeader>
<ns:Message>
<ns:X>
<ns1:ServiceVersion>12.05</ns1:ServiceVersion>
</ns:X>
</ns:Message>
</ns:TocHeader>
</s:Header>
The XML that .NET is generating:
<s:Header>
<ns:TocHeader xsi:nil="true"/>
</s:Header>

WCF/SOAP: Move XML namespace definition to parent element

I use WCF to implement a client for a SOAP web service. The code is generated by "Add Service Reference" based on the WSDL file.
The elements of the SOAP request are defined in different namespaces.
Now, a sample message looks like this:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<myMethod xmlns="http://example.org/xsd/requests">
<RequestData>
<Foo xmlns="http://example.org/xsd/elements">some</Foo>
<Bar xmlns="http://example.org/xsd/elements">thing</Bar>
<Baz xmlns="http://example.org/xsd/elements">content</Baz>
<!-- .... more elements .... -->
</RequestData>
</myMethod>
</s:Body>
</s:Envelope>
which contains the redundant definition of the elements namespace. In order to reduce message size, I would prefer to have the namespace definitions at the top of the message, eg. like this:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:a="http://example.org/xsd/requests"
xmlns:b="http://example.org/xsd/elements">
<a:myMethod>
<a:RequestData>
<b:Foo>some</b:Foo>
<b:Bar>thing</b:Bar>
<b:Baz>content</b:Baz>
<!-- .... more elements .... -->
</a:RequestData>
</a:myMethod>
</s:Body>
</s:Envelope>
(I don't really care which parent element defines the namespaces, so any way to move the namespace definitions to Envelope, Body or MyMethod would be okay for me.)
How can I achieve this?
EDIT: removed local xmlns definitions in second example (copy-paste mistake ;) )

Configure XmlSerializer used for WCF

We got a docs from our client specifying that our WCF service we developing should return responses like this one:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hsdq="http://site.gov/21398">
<soapenv:Header/>
<soapenv:Body>
<inf:getCarsResponse xmlns:inf="inf">
<hsdq:Message1/>
<hsdq:Message2>
<hsdq:ApplicationData>
...
</hsdq:ApplicationData>
<hsdq:ApplicationDocument/>
</hsdq:Message2>
</inf:getCarsResponse>
</soapenv:Body>
</soapenv:Envelope>
But response generated by our service is slightly different:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<getCarsResponse xmlns="inf">
<Message2 xmlns="http://site.gov/21398">
<ApplicationData>
...
</ApplicationData>
</Message2>
</getCarsResponse>
</s:Body>
</s:Envelope>
I know that I could fix missing of ApplicationDocument section and Header section. But how can I make my response be exact like provided?
I mean that I would like to configure xmlserializer to be possible:
1. Remove unnecessary xsi and xsd namespaces.
2. Rename envelope namespace s to soapenv.
3. hsdq namespace should be specified in root element and should be identified also by name not only by an url. (Compare in first response: xmlns:hsdq="http://site.gov/21398" and second response: xmlns="http://site.gov/21398"
I think I should use IDispatchMessageFormatter for that, but is seems too complicated for me that I couldn't use ut without any sample.
Thanks, guys!
I don't think you could do it. The namespace order of prefix should not matter to the server. You could write a custom encoder to swap namespaces but you should really check why the server rejects this, maybe it does not like something else.

Categories