SoapCore icon indicating copy to clipboard operation
SoapCore copied to clipboard

Fault message details

Open tomskarasha opened this issue 8 years ago • 16 comments

Hi!

Is there an example or possibility to fill Fault message details tag from service if there was exception or something similar? Thanks!

tomskarasha avatar Jul 18 '18 20:07 tomskarasha

Hi, please, explain your case? If you want make FaultException by your logic - use class ExceptionTransformer.

kotovaleksandr avatar Aug 01 '18 09:08 kotovaleksandr

Hi! What I meant was that if in my service was some kind of exception, f.ex.: <fault:MyServiceFault xmlns:fault="http://fault.org/soap/MyServiceFault/"> <fault:MyServiceFaultCode>12345</faultcode> <fault:MyServiceFaultString>Dude, something wrong.</faultstring> </fault:MyServiceFault> It's like already catching all exceptions, only more describing them, but originally I get this instead: <s:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode>s:Client</faultcode> <faultstring>Object reference not set to an instance of an object.</faultstring> <detail /> </s:Fault> </s:Body> </s:Envelope>

But what I want accomplish is: <s:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode>s:Client</faultcode> <faultstring>Object reference not set to an instance of an object.</faultstring> <detail> <fault:MyServiceFault xmlns:fault="http://fault.org/soap/MyServiceFault/"> <fault:MyServiceFaultCode>12345</faultcode> <fault:MyServiceFaultString>Dude, something wrong.</faultstring> </fault:MyServiceFault> </detail> </s:Fault> </s:Body> </s:Envelope>

If you say that I can use ExceptionTransformer class, then how can I use it if it's an internal class?

tomskarasha avatar Aug 02 '18 08:08 tomskarasha

See extension method AddSoapExceptionTransformer(this IServiceCollection serviceCollection, Func<Exception, string> transformer), you can set custom function to rewrite exception details.

kotovaleksandr avatar Aug 02 '18 08:08 kotovaleksandr

Well not quite solves the problem only horribly limitates things. Fault DTO Detail property as the tag in XML always will be empty because it's nowhere assigned, but I made changes a bit in the engine how he does that: In Fault DTO I added another property RawDetails with ignore in serialization attribute and changed Details property type to XmlElement then in Middleware/WriteErrorResponseMessage method I added that RawDetails property and assigned errorText to it then in FaultBodyWriter added: var doc = new XmlDocument(); doc.LoadXml(_fault.RawDetails); _fault.Details = doc.DocumentElement; this is needed because if I pass raw XML string it serializes it weird and looks ugly in the output. At the end in MyService class I can just call: throw new Exception(MyUtils.SerializeToXml(MyServiceFaultObject)); and I got the result what I needed!

I don't say that this is a best way to accomplish XML in details tag, a lot of testing and refactoring and additions will be needed!

tomskarasha avatar Aug 04 '18 14:08 tomskarasha

This is a real issue. I spent hours debugging both the server and the client when the client did not catch a typed Fault (FaultException<T>) thrown in the server code.

tompanchee avatar Aug 06 '18 12:08 tompanchee

@tompanchee Good job! Thanks, please check nuget with your PR https://www.nuget.org/packages/SoapCore/0.9.8.5

kotovaleksandr avatar Aug 08 '18 08:08 kotovaleksandr

you may try marking contract with proper attributes, so the server exception will be fully serialized and sent to client, as here

DmitryBryluk avatar Aug 09 '18 19:08 DmitryBryluk

Thanks for fixes n suggestions but this still doesn't work for me as it should! it transforms that passed XML string, but it shouldn't, current detail output:

<detail>
	<MyServiceFault xmlns="http://schemas.datacontract.org/2004/07/MyServiceModels.MyService">
		<MyServiceFaultCodeField>12345</MyServiceFaultCodeField>
		<MyServiceFaultStringField>Dude, something wrong.</MyServiceFaultStringField>
	</MyServiceFault>
</detail>

not like this:

<detail>
	<fault:MyServiceFault xmlns:fault="http://fault.org/soap/MyServiceFault/">
		<fault:MyServiceFaultCode>12345</MyServiceFaultCode>
		<fault:MyServiceFaultString>Dude, something wrong.</MyServiceFaultString>
	</fault:MyServiceFault>
</detail>

Why there is suddenly attached word Field?

The idea in code, it's not bad from my opinion, just something is missing, if I serialize DTO to XML string to keep it original and if that DTO MyServiceFault have my friends/clients/neighbors/dogs/fatCats has it (from XSD or whatever) then they should be able to deserialize it back to the same DTO at the moment it cant because it's changed starting with namespace. hmmm...

tomskarasha avatar Aug 13 '18 19:08 tomskarasha

OK, I might found a solution for this, but I can't pass UnitTests anymore :/ The thing is why he is doing that (in previous comment) is that in @tompanchee solution he uses DataContractSerializer to serialize Detail object, but when I changed it to XmlSerializer it works also and as expected with all it's namespaces and without mystical Field name attached, but when it comes to UnitTests somehow he suddenly thinks that the exception type is wrong, FaultException instead of FaultException<FaultDetail>, when change back to DataContractSerializer UnitTest passes. @kotovaleksandr @tompanchee @DmitryBryluk do you have any idea why is that so?

tomskarasha avatar Aug 19 '18 14:08 tomskarasha

perhaps lack of XmlSerializer attributes, see serialization tests if no idea, you may prepare the branch with the problem, so other will see

DmitryBryluk avatar Aug 20 '18 11:08 DmitryBryluk

OK @DmitryBryluk, branch created and changes are there. Failing test: IntegrationTests/ThrowsDetailedFault()

Tried suggestion and added attributes, but still no luck on this! This is really weird I must say!

tomskarasha avatar Aug 20 '18 18:08 tomskarasha

first, you have to create and use XmlSerializer endpoint: app.UseSoapEndpoint<TestService>("/Service.asmx", new CustomBinding(transportBinding, textEncodingBinding), SoapSerializer.XmlSerializer); second, things become worse, and now it becomes CommunicationException. so, problem is deeper, can't help now.

DmitryBryluk avatar Aug 21 '18 08:08 DmitryBryluk

hmm... changed everywhere to SoapSerializer and didn't get CommunicationException, but it looks like it's not impacted by that SoapSerializer at all.

tomskarasha avatar Aug 21 '18 09:08 tomskarasha

SoapCore\src\SoapCore.Tests\Startup.cs:42, insert: app.UseSoapEndpoint<TestService>("/Service.asmx", new CustomBinding(transportBinding, textEncodingBinding), SoapSerializer.XmlSerializer);

SoapCore\src\SoapCore.Tests\IntegrationTests.cs:CreateClient, change: var endpoint = new EndpointAddress(new Uri(string.Format("http://{0}:5050/{1}.asmx", "localhost", caseInsensitivePath ? "serviceci" : "Service")));

by default these tests using SoapSerializer.DataContractSerializer

DmitryBryluk avatar Aug 21 '18 09:08 DmitryBryluk

If switch in Fault.cs XmlSerializer to DataContractSerializer: new DataContractSerializer(detailObject.GetType()).WriteObject(ms, detailObject); test passed.

Problem on client side in wcf client. See code https://github.com/dotnet/wcf/blob/e3252b3f2a7a5b074d7143186dfe21dde4adc293/src/System.Private.ServiceModel/src/System/ServiceModel/Dispatcher/FaultFormatter.cs#L121 When fault details serialized with XmlSerializer IsStartObject return false and fault details skipped.

kotovaleksandr avatar Aug 21 '18 12:08 kotovaleksandr

See https://github.com/dotnet/wcf/blob/e3252b3f2a7a5b074d7143186dfe21dde4adc293/src/System.Private.ServiceModel/src/System/ServiceModel/Dispatcher/FaultContractInfo.cs#L54 Client always expect for Fault details serialized by DataContractSerializer. If fault details serialized with XmlSerializer wcf client parse only FaultException, without details.

kotovaleksandr avatar Aug 21 '18 13:08 kotovaleksandr