IIS上托管的WCF服务的入口方法是什么?

发布于 2024-12-09 21:42:30 字数 450 浏览 1 评论 0原文

一些背景信息 - 我正在尝试在 Azure 上托管 RESTful WCF 服务。据我了解,除非我在角色上托管 ASP.NET 类型,否则我实际上并不需要 global.asax 类(它具有 application_start 方法)。

从基本原型设计开始,我需要的只是 svc 文件及其背后的实现,它会在角色启动时自动初始化(我的意思是,托管在 IIS 上)。这很棒,因为除了 web.config 和我的文件之外,我不需要额外的代码服务已启动并正在运行。我不需要创建新的服务主机并开始侦听它等。我可以将角色和 POST 消息部署到我的服务。

问题 - 我实现了自定义日志记录和初始化类,我需要在服务启动时对其进行初始化。我将我的服务配置为单例,但我不确定应该将自定义初始化组件放在哪里。

如果没有显式的应用程序启动方法并且我的服务配置为单例,我是否可以假设当第一个请求传入时,我的服务构造函数被调用? (以及我所有的自定义初始化?)。

A little background info -
I'm trying to host a RESTful WCF service on Azure. As I understand, unless I have the ASP.NET type hosting on the role, I don't really need the global.asax class (which has the application_start method).

From basic prototyping, all I needed was the svc file and the implementation behind it and it automatically gets initialized on role startup (I mean, hosted up on IIS).This is great because I need no extra code other than web.config and my service is up and running. I don't need to create a new service host and start listening on it, etc. I'm able to deploy the role and POST messages to my service.

The problem -
I have custom logging and initialization classes implemented that I need to initialize when my service starts up. I configured my service to be a singleton and I'm not sure where I should put my custom initialization components.

Without an explicit application start method and my service configured as a singleton, can I assume that when the first request comes in, my service constructor gets called? (along with all my custom initialization?).

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

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

发布评论

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

评论(2

掩于岁月 2024-12-16 21:42:30

我可以假设当第一个请求到来时,我的服务构造函数被调用吗?

是的,但是您应该问自己是否真的希望您的服务作为单例运行。如果你对此感到满意,那么它就会工作得很好;如果您不希望它作为单例运行,那么您应该使用自定义工厂查看 Russell 的答案。

查看 WCF 服务通常应该是单例吗?关于 WCF 服务是否应该是单例的一些讨论。您需要根据自己的情况做出决定,但通常 WCF 服务不是单例,除非需要。

要实现自定义工厂,请参阅此 MSDN 链接 使用 ServiceHostFactory 扩展托管 。正如链接所描述的,像这样扩展服务主机工厂

public class DerivedFactory : ServiceHostFactory
{
   public override ServiceHost CreateServiceHost( Type t, Uri[] baseAddresses )
   {
      return new ServiceHost(t, baseAddresses )
   }
}

然后在 ServiceHost 指令中指定您的工厂

<% @ ServiceHost 
     Service="MyNamespace.MyService" 
     Factory="MyNamespace.DerivedFactory" %>

can I assume that when the first request comes in, my service constructor gets called?

Yes, but you should ask yourself whether you really want your service to run as a singleton. If you're happy with this then it will work fine; if you don't want it to run as a singleton then you should look into Russell's answer using a custom factory.

Look at Should WCF service typically be singleton or not? for some discussion about whether WCF services should be singletons. You need to decide for your situation, but generally WCF services are not singletons unless they need to be.

To implement a custom factory, see this MSDN link Extending Hosting Using ServiceHostFactory. As the link describes, extend the service host factory like so

public class DerivedFactory : ServiceHostFactory
{
   public override ServiceHost CreateServiceHost( Type t, Uri[] baseAddresses )
   {
      return new ServiceHost(t, baseAddresses )
   }
}

And then specify your factory in the ServiceHost directive

<% @ ServiceHost 
     Service="MyNamespace.MyService" 
     Factory="MyNamespace.DerivedFactory" %>
微凉 2024-12-16 21:42:30

您正在寻找 ServiceHostFactory。您可以将一部分添加到 SVC 文件以使用工厂,您可以在其中执行您可能需要的任何日志记录等。

我过去曾使用它来启动后台工作程序来启动单独的线程来进行某些后台工作。

http://msdn.microsoft.com/en-us /library/system.servicemodel.activation.servicehostfactory.aspx

希望这可以帮助您到达您需要的地方。 :)

You're looking for ServiceHostFactory. You can add a part to the SVC file to use a factory, where you can do any logging etc. you may need.

I have used this in the past to start a background worker to launch a separate thread for some background work.

http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.servicehostfactory.aspx

Hope this helps you get where you need to be. :)

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