捕获全局 WCF ServiceHost 请求事件

发布于 2024-10-23 13:42:36 字数 1217 浏览 4 评论 0原文

我正在使用一个非常简单的 WCF 项目,该项目由 WPF 应用程序托管。它基本上作为 REST 服务器运行,用于在我的 PC 上提供数据。没有 IIS,它作为 SingleInstance 运行。 我想查看哪些 IP 正在访问 MyService,以及它们尝试调用哪些 WebMethod。

好的,我可以将一个事件作为我的服务的一部分,在服务类本身中声明。下面是一些让它运行的代码,一切都按照预期工作(请不要对 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"];

是否有一个更通用的包罗万象的事件可以检测对我的服务的调用?可能有一个事件是由众多 Behaviour/ChannelDispatcher 类型之一触发的,我承认这些类型我并不完全理解。

谢谢你的帮助,汤姆

I'm using a really simple WCF project which is being hosted by a WPF application. It basically operates as a REST server for dishing up data on my PC. No IIS, and it runs as SingleInstance. I want to see what IP's are accessing MyService, and what WebMethod they're attempting to invoke.

Ok so I can have an event as part of my Service, declared in the service class itself. Here's some code that gets it going, it all works exactly as expected (no flames about m_ please ;)):

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);

Then in each service method, I can raise this event so my app knows that someone has tried to use it. Brilliant, but let's face it, this is awful.

I have to write something along the lines of:

var who =  OperationContext.Current.IncomingMessageProperties.Via;
var what = OperationContext.Current.IncomingMessageProperties["UriTemplateMatchResults"];

for each service call.

Is there a more generic catch-all-event that can detect a call to my service? There's probably one fired by one of the many Behaviour/ChannelDispatcher types which I admittedly don't fully understand.

Thanks for your help, Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来日方长 2024-10-30 13:42:36

使用IParameterInspector,您可以挂钩任何方法调用并检查方法和/或参数。

除了您使用的方法之外,没有其他方法可以获取传入消息的额外信息(IP 地址等)。恕我直言,这只是微软的一个糟糕的设计(请参阅我的咆哮此处)。

您可以在此处找到示例。

Using IParameterInspector you can hook to any method calls and inspect the method and/or parameters.

There is no other way to get the extra information (IP address, etc) of the incoming message other than the one you have used. This is just a bad design by Microsoft IMHO (see my rant here).

You can find an example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文