-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added ISoapMessageProcessor and accompanying unit tests #788
Added ISoapMessageProcessor and accompanying unit tests #788
Conversation
|
||
if (httpcontext.Request.Path.Value.Contains("ServiceWithProcessor.svc")) | ||
{ | ||
return Message.CreateMessage(MessageVersion.Soap11, "none"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting this error message when I try to message.WriteBodyContents( <some-writer> )
.
System.InvalidOperationException: This message cannot support the operation because it has been written.
at System.ServiceModel.Channels.Message.EnsureWriteMessageState(XmlDictionaryWriter writer)
at System.ServiceModel.Channels.Message.WriteMessage(XmlDictionaryWriter writer)
at SoapCore.MessageEncoder.SoapMessageEncoder.WriteMessageAsync(Message message, PipeWriter pipeWriter) in ..../src/SoapCore/MessageEncoder/SoapMessageEncoder.cs:line 173
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is when you are trying to read the message and act differently depending on the contents?
Messages are weird and can only be written (read) once. You need to do something like this
var bufferedMessage = message.CreateBufferedCopy(int.MaxValue);
var msg = bufferedMessage.CreateMessage();
var reader = msg.GetReaderAtBodyContents();
var content = reader.ReadInnerXml();
return bufferedMessage.CreateMessage();
If everything is in order, return bufferedMessage.CreateMessage();
. Otherwise create and return your own message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is when I want to return a new Message, but I want it to have some content, like a serialized DTO, or some other XML.
Apparently, even if I return a new Message and not call "next", something down the pipeline will try to write into the new message.
And, because I already wrote it, the exception is raised.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
message.WriteBodyContents( <some-writer> )
writes the content of the message to the supplied writer. Not the other way around.
To create a message with content you have to use one of the overloads of Message.CreateMessage()
I just tried changing my test to:
if (httpcontext.Request.Path.Value.Contains("ServiceWithProcessor.svc"))
{
return Message.CreateMessage(MessageVersion.Soap11, "none", "This is the body");
}
else
{
return await next(message);
}
That worked and gave me
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">none</Action>
</s:Header>
<s:Body>
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">This is the body</string>
</s:Body>
</s:Envelope>
in return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm missing something, please add a code sample of what you're trying to do and I'll see if I can figure it out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, thanks!
I actually forgot that overload existed.
Anyway, because my DTO has attributes for the XmlSerializer, I used CreateMessage(MessageVersion, String, XmlReader) , following this guide and it worked fine!
Another thing i've been thinking about is to use a return type that contains more than just the Message. Something like
Requiring you to set headers and statuscode directly on the HttpContext could get messy, if multiple messageprocessors add different headers to the response. What do you think? |
@kotovaleksandr I would love to hear your thoughts on this. |
Hi @andersjonsson ! Sorry for later response. Looks good, please prepare PR, i will merge them. |
Done. Let me know if there is anything you would like me to fix before this is merged |
Trying out a concept I've been thinking about.
The idea is to create something like messageinspector, but with the added possibility to "short circuit" the processing and return another message.
Any thoughts?