是否有相当于 Java 初始化 Servlet 的 .net 版本?

发布于 2024-10-01 13:26:30 字数 119 浏览 6 评论 0 原文

是否有相当于 Java 初始化 Servlet 的 .net 版本?

当我们这样做时,相当于过滤器吗?

最后,如果所说的东西存在(过滤器),它们可以在向 WCF 服务发出请求之前/之后运行吗?

Is there a .net equivalent of a Java initialization Servlet?

While we're at it, the equivalent of a filter?

And finally if said thing exists (filters) can these run before/after a request is made to a WCF service?

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

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

发布评论

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

评论(2

听闻余生 2024-10-08 13:26:30

虽然您可以使用 OnStart 事件HttpApplication 在服务启动时进行拦截,以及 IHttpModule 用于拦截请求,这不是在 WCF 中执行这些拦截的正确方法。

该建议有效的唯一原因是您托管在 IIS 中并使用特定于 IIS 的挂钩。但是,WCF 服务可以托管在任何地方,您可能会发现将服务移至服务进程,并更改绑定(例如,您可能使用 net-tcp 而不是 http,在这种情况下,您将如何读取通过 IHttpModule 实现传入的内容?您不能)这会导致这些挂钩中断。

也就是说,理想情况下,您应该在创建 ServiceHost 实例。但是,由于您无权访问 IIS 中 WCF 中的 ServiceHost 实例,因此您必须实现自定义 ServiceHostFactory在 svc 文件中指定该工厂(请参阅标题为“在 IIS 或 WAS 中使用自定义 ServiceHost”的部分)。 采用这种方法将使其可移植。

至于拦截调用,可以在客户端和服务端进行。在客户端,您可以实现 IClientMessageInspector 并且在服务器端您需要 IDispatchMessageInspector(我假设你想要后者)。

为了在服务端“注入”此检查器,您必须使用端点行为(这将应用于服务上的特定端点,IEndpointBehavior)或服务行为(通过实现 < a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.iservicebehavior.aspx" rel="nofollow noreferrer">IServiceBehavior 接口)。

通过这些实现,您可以查看 ServiceEndpoint (对于终结点行为)或 ServiceDescription< /a> 类来应用检查器的实现,或 WCF 中可用的任何其他挂钩。

您可以阅读Paolo Pialorsi 的“编写 WCF 消息检查器”,了解如何将所有这些整合在一起并让您深入了解 WCF 中的自定义行为。

While you can use the OnStart event of HttpApplication to intercept when the service starts as well as IHttpModule for intercepting requests, it is not the correct way to perform these interceptions in WCF.

The only reason that recommendation works is because you are being hosted in IIS and using IIS-specific hooks. However, WCF services can be hosted anywhere, and you might find that you move your service to a service process, as well as change the bindings (you might use net-tcp instead of http, for example, in which case, how would you read the content coming in through an IHttpModule implementation? You couldn't) which would cause these hooks to break.

That said, ideally you would perform any kind of initialization before you create your ServiceHost instance. However, since you don't have access to the ServiceHost instance in WCF in IIS, you will have to implement a custom ServiceHostFactory and specify that factory in your svc file (see the section titled Using a Custom ServiceHost in IIS or WAS). Taking this approach will make it portable.

As for intercepting calls, you can do that on the client and on the service side. On the client side, you would implement IClientMessageInspector and on the server side you want IDispatchMessageInspector (I assume you want the latter).

In order to "inject" this inspector on the service side, you would have to use an endpoint behavior (which would apply to a specific endpoint on the service, an implementation of IEndpointBehavior) or a service behavior (which applies to all endpoints on the service, through an implementation of the IServiceBehavior interface).

It is through those implementations that you would look at the ServiceEndpoint (for endpoint behaviors) or ServiceDescription class to apply the implementation of your inspector, or any one of the other hooks available in WCF.

You can read Paolo Pialorsi's "Writing a WCF Message Inspector" for a full walkthrough of how to pull this all together and give you insight into custom behaviors in WCF.

浅暮の光 2024-10-08 13:26:30

好吧,我不知道 java 框架,但从它的声音来看,您可能正在寻找 Global.asax HttpApplication.Application_OnStart 事件用于初始化。

还有过滤器?尝试 IHttpModule

Well, I don't know the java frameworks, but from the sounds of it you might be looking for the Global.asax HttpApplication.Application_OnStart event for initialization.

And filters? Try IHttpModule.

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