Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6541329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:05:30+00:00 2026-05-25T11:05:30+00:00

I’m trying to set up a Routing Service that will sit in the DMZ

  • 0

I’m trying to set up a Routing Service that will sit in the DMZ of my network, and allow external people to access some internally hosted WCF services. I’ve got everything set up and working, but when I forward on the MEX services, it points our external clients to our internal address, which obviously they can not access.

Microsoft seems to recommend making a copy of the wsdl, which would probably work, but would require me to make a new copy of the wsdl every time the service definitions change, which they do quite often, and seems like overkill. The only thing that needs to be changed is a address in the mex message.

       <endpoint address="http://appsrv1:8781/ProcessManagementService/"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IProcessManagementService"
            contract="IProcessManagementService" name="WSHttpBinding_IProcessManagementService" />

It seems as though using an IDispatchMessageInspector, I should be able to intercept the mex message and replace the internal server name with the external server name, and then I would only have to touch the Routing service when I need to add or remove a service, rather than every time I make any change.

I don’t have much experience with XML readers, writers, and so on, so I’m looking for some guidance on how to proceed. If I could just read the xml content into a string, I could perform a replace operation to substitute the external address for the internal one, then replace the message content of the reply with my modified version. How do I go about doing that, or is there a better way to modify the content of a WCF message?

Edit: So this is what I’ve cobbled together so far.

public class EndpointReplacementInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        return null;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {           
        var ms = new MemoryStream();
        var w = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true, IndentChars = "  ", OmitXmlDeclaration = true });
        var bodyReader = reply.GetReaderAtBodyContents();
        w.WriteStartElement("s", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
        while (bodyReader.NodeType != XmlNodeType.EndElement && bodyReader.LocalName != "Body" && bodyReader.NamespaceURI != "http://schemas.xmlsoap.org/soap/envelope/")
        {
            if (bodyReader.NodeType != XmlNodeType.Whitespace)
            {
                w.WriteNode(bodyReader, true);
            }
            else
            {
                bodyReader.Read(); // ignore whitespace; maintain if you want
            }
        }
        w.WriteEndElement();
        w.Flush();
        var body = Encoding.UTF8.GetString(ms.ToArray());
        body = body.Replace("internalserver", "externalserver");            
        var replacedMessage = Message.CreateMessage(XmlReader.Create(new StringReader(body)), int.MaxValue, reply.Version);
        replacedMessage.Headers.CopyHeadersFrom(reply.Headers);
        replacedMessage.Properties.CopyProperties(reply.Properties);
        reply = replacedMessage;    
    }
}

And it seems to mostly work. However, the XMLReader is throwing an exception “Data at the root level is invalid. Line 1, position 1.” when I attempt to create the message. I don’t know where to start on that one.

Edit 2:

Ok, Now I’ve got a method that extracts the message into an xdocument, then sends that to a string, then edits it, then pulls that back into an xdocument, and I get a “Connection Forcibly Closed” when I send back a message containing that edited message. Terrible.

Edit 3:

After testing, simply extracting the message from the reply to an xdoc and then loading it into a new message is enough to cause the “Connection Forcibly closed” issue. This must not be the right way to edit messages. I’m looking for examples or experience on how to best approach this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:05:31+00:00Added an answer on May 25, 2026 at 11:05 am

    I got an answer from the MSDN Forums. my problem was that I was changing the length of the string, but not resetting the length of the MemoryStream, which meant bytes at the end were not being reported.

    Here is a working replacement function.

     public Message ChangeString(Message oldMessage, string from, string to)
            {
                MemoryStream ms = new MemoryStream();
                XmlWriter xw = XmlWriter.Create(ms);
                oldMessage.WriteMessage(xw);
                xw.Flush();
                string body = Encoding.UTF8.GetString(ms.ToArray());
                xw.Close();
    
                body = body.Replace(from, to);
    
                ms = new MemoryStream(Encoding.UTF8.GetBytes(body));
                XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(ms, new XmlDictionaryReaderQuotas());
                Message newMessage = Message.CreateMessage(xdr, int.MaxValue, oldMessage.Version);
                newMessage.Properties.CopyProperties(oldMessage.Properties);
                return newMessage; 
           }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.