开发者

WCF client inspector get name of web service it calls or calling method name or type or something

开发者 https://www.devze.com 2023-02-15 00:20 出处:网络
I have implemented a WCF inspector in my client application that consumes numerous web services. I am using this inspector as a logging mechanism to log calls sent from the application to those web s

I have implemented a WCF inspector in my client application that consumes numerous web services.

I am using this inspector as a logging mechanism to log calls sent from the application to those web services and the responses they give back.

public class WcfClientInterceptor : IClientMessageInspector
    {

        protected static readonly ILog log4net = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private static readonly IMessageLogger Logger = new Log4NetLogger();

        private MessageLogEntry LogEntry;// = new MessageLogEntry();

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

            if (Logger.IsLogEnabled)
            {
                LogEntry.ResponseBody = reply.ToString();
                Logger.Log(LogEntry);
            }
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            if (Logger.IsLogEnabled)
            {
                LogEntry = LogEntry ?? new MessageLogEntry();
                //instanceContext.GetServiceInstance().GetType().Name
                //LogEntry.WebServiceIdentity = request.Headers.Action;
                LogEntry.WebServiceIdentity = OperationContext.Current.IncomingMessageHeaders.Action;
                LogEntry.RequestBody = request.ToString();
            }
            return null;
        }
    }

My problem is that I don't know what web service is called. I want to get some kind of reference to them and log it.

This is the only method that works request.Headers.Action but it doesn't always work. Most of the time this is String.Empty. OperationContext.Current is null which I understand is normal on the client开发者_如何学编程 side.

Is there any other way of getting the name of the webservice that is called? or the name of the calling method? or something?

Thank you


it works with LogEntry.WebServiceIdentity = request.Headers.Action; I made a mistake

0

精彩评论

暂无评论...
验证码 换一张
取 消