服务启动前初始化WCF服务? [英] Initialize WCF service before service starts?

查看:664
本文介绍了服务启动前初始化WCF服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含WCF服务的程序集,它们在程序集的app.config文件中声明,并在我开始调试客户端应用程序时自动托管。这一切都有效。

I've created an assembly containing WCF Services, which are declared in the app.config file of the assembly and are automatically hosted when I start to debug my client application. This all works.

问题是,我需要在服务托管之前运行一些初始化,特别是我需要收集已知的要与[ServiceKnownType(RegisterKnownTypes,typeof(ServiceKnownTypesHelper))]属性一起使用的类型(我使用MEF从我的程序集中收集知识,所以我不能把它们放在配置文件中,因为它们在运行时,而不是之前)。

Problem is, I need to run some initializations before the services are hosted, especially I need to collect a list of known types to be used with the [ServiceKnownType("RegisterKnownTypes", typeof(ServiceKnownTypesHelper))] attribute (I use MEF to collect the knowntypes from my assemblies, so I can't just put them in the config file, since they are known at runtime, not before).

有没有办法在WCF服务托管之前初始化 之前的某些东西?我可以在服务类的构造函数中初始化东西,但是现在已经不能注册已知的类型了。

Is there a way to initialize something before the WCF services are hosted? I can initialize stuff in the constructor of the service classes, but this is too late to register the known types.

推荐答案

通常封装在派生的 ServiceHost 中,您可以通过覆盖 OnOpening 方法来主持服务之前初始化任何内容。您还可以通过处理默认上的开始事件来执行一些初始化,而不实施新的 ServiceHost ServiceHost

This is usually encapsulated in derived ServiceHost where you can initialize anything before you host the service by overriding OnOpening method. You can also do some initialization without implementing a new ServiceHost by handling the Opening event on default ServiceHost.

编辑:

正如您已经找到.config文件为您的服务库生成仅由测试工具WcfSvcHost.exe使用。应用程序本身必须在主app.config / web.config中具有自己的配置,因此您可以根据需要更改库配置以进行调试。没有其他方法如何影响WcfSvcHost.exe中的 ServiceHost 初始化。

As you have already found the .config file generated for your service library is only used by test tool WcfSvcHost.exe. The application itself must have its own configuration in the main app.config / web.config so you can change the library config for debugging purposes as you need. There is no other way how to affect ServiceHost initialization in WcfSvcHost.exe.

这篇关于服务启动前初始化WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆