捕获全局WCF ServiceHost请求事件
问题描述:
我正在使用由WPF应用程序托管的非常简单的WCF项目。它基本上是作为REST服务器来运行我的个人电脑上的数据。没有IIS,它以SingleInstance运行。 我想知道哪些IP正在访问MyService,以及他们试图调用什么WebMethod。捕获全局WCF ServiceHost请求事件
好吧,我可以有一个事件作为我的服务的一部分,在服务类本身声明。下面是一些代码,得到它走,它所有的工作原理完全如预期(约m_
请无火焰;)):
MyService ds = new MyService(); // It's not really called this :)
ds.Request += new EventHandler(ds_Request); // I want to avoid this
ds.SomePropertySetFromMyRehostingClient = "something"; // SingleInstance now required
m_Service = new ServiceHost(ds, new Uri(GetServerHostUri()));
m_Service.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single;
m_Service.BeginOpen(new TimeSpan(0, 0, 5), new AsyncCallback(EndStartService), null);
然后在每个服务方法,我可以让我的应用程序知道引发此事件是有人试图使用它。辉煌,但让我们面对它,这是可怕的。
我要写的线沿线的东西:
var who = OperationContext.Current.IncomingMessageProperties.Via;
var what = OperationContext.Current.IncomingMessageProperties["UriTemplateMatchResults"];
为每个服务呼叫。
是否有更通用的全能事件可以检测到对我的服务的呼叫?可能有人被许多Behavior/ChannelDispatcher类型之一解雇,但我承认这些类型并不完全理解。
感谢您的帮助,汤姆
是的!而已!好东西,你显然知道所有这一切里面。我并没有真正使用IParameterInspector,而是IDispatchMessageInspector.AfterReceiveRequest()实现似乎有用。我只是在绑定一个返回适当处理的数据的事件之后作为一个行为添加。谢谢! – Tom 2011-03-17 14:46:01
很高兴它为你工作。我使用'IParameterInspector'来审计调用。 – Aliostad 2011-03-17 15:48:30