Skip to content
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

Merged
merged 5 commits into from
Jan 5, 2022

Conversation

andersjonsson
Copy link
Collaborator

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?


if (httpcontext.Request.Path.Value.Contains("ServiceWithProcessor.svc"))
{
return Message.CreateMessage(MessageVersion.Soap11, "none");
Copy link

@moinessim moinessim Dec 31, 2021

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

Copy link
Collaborator Author

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.

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.

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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

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!

@andersjonsson
Copy link
Collaborator Author

Another thing i've been thinking about is to use a return type that contains more than just the Message.

Something like

public class SoapResponse
{
    int StatusCode {get;set;}
    Dictionary<string, string> Headers {get;set;}
    Message SoapMessage {get;set;}
}

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?

@andersjonsson
Copy link
Collaborator Author

@kotovaleksandr I would love to hear your thoughts on this.
I'm ready to convert this from draft and get it merged if you like the idea and the implementation.

@kotovaleksandr
Copy link
Member

Hi @andersjonsson ! Sorry for later response. Looks good, please prepare PR, i will merge them.

@andersjonsson andersjonsson marked this pull request as ready for review January 5, 2022 08:47
@andersjonsson andersjonsson changed the title WIP: Added ISoapMessageProcessor and accompanying unit tests Added ISoapMessageProcessor and accompanying unit tests Jan 5, 2022
@andersjonsson
Copy link
Collaborator Author

Done. Let me know if there is anything you would like me to fix before this is merged

@kotovaleksandr kotovaleksandr merged commit ab8f6ba into DigDes:develop Jan 5, 2022
@kotovaleksandr kotovaleksandr mentioned this pull request Jan 5, 2022
@andersjonsson andersjonsson deleted the isoapmessageprocessor branch January 11, 2022 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants