0

I have one wcf post method, this method needs TokenId which was sent by client side. I am using the below code to get tokeid of incoming header.

IncomingWebRequestContext woc = WebOperationContext.Current.IncomingRequest;
string tokenId = (woc.Headers["TokenId"] == null ? string.Empty : woc.Headers["TokenId"].ToString());

for wcf get method, it works perfectly and it is able to get token id however for wcf post method, it generats below error message.

System.ServiceModel.Channels.MessageProperties get_Properties() ?Message?The message object has been disposed

Anyone knows how to resolve the issue and make sure the wcf post method is able to get header from incoming request? Thanks

Stefano Cavion
  • 641
  • 8
  • 16
Li Ling
  • 1
  • 2

1 Answers1

0

I see your error reason is "The message object has been disposed",I suggest you use IDispatchMessageInspector to intercept the message and get the request header.

public class ServerMessageLogger : IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            IncomingWebRequestContext woc = WebOperationContext.Current.IncomingRequest;

            string header = woc.Headers["Host"];

            return null;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {


        }
    }

We intercept its message after receiving the request to avoid the message being disposed.I use the get method and the post method to test respectively, and both of them can successfully obtain the request header.

For more information about IDispatchMessageInspector,Please refer to the following link:

https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.idispatchmessageinspector?view=netframework-4.8

Ding Peng
  • 3,702
  • 1
  • 5
  • 8
  • Hi Thanks Ding ding but may i know how to invoke this method? how can i get this three parameters?ref Message request, IClientChannel channel, InstanceContext instanceContext – Li Ling Jun 15 '20 at 09:32
  • You don't need to call this method, it will execute itself every time it receives a message. – Ding Peng Jun 15 '20 at 09:48
  • Hi Sir Thanks however it never execuate automatically when i debug my application. do you mean i add IDispatchMessageInspector interface and implement the mehtods in my current svc file. it will automatically execute? – Li Ling Jun 17 '20 at 08:00
  • You also need to add it to the service. For details, please refer to this link:https://stackoverflow.com/questions/62325865/how-to-enable-cross-origin-resource-sharing-in-net-console-application-wcf-serv/62336298?noredirect=1#comment110401181_62336298 – Ding Peng Jun 17 '20 at 09:12