如何:避免IIS托管的WCF应用程序的冷启动问题? [英] How to: Avoiding Cold-Start issues with IIS hosted WCF applications?

查看:153
本文介绍了如何:避免IIS托管的WCF应用程序的冷启动问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个系统,我们在IIS中托管几个WCF应用程序。其中一些应用程序有一段延长的启动时间需要几秒钟(超过用户会乐于等待的时间)。一旦启动并运行,一切都很快,所以它只是启动时间。



客户端还需要每晚回收应用程序池。 / p>

有没有办法唤醒所有这些服务,以便启动时间不是系统的第一个用户的问题?



初步想到在哪里编写一个Windows服务,每隔x(可配置)分钟就可以在每个服务上调用一个轻量级方法来保持应用程序活着,但是因为我们处于负载平衡环境中,并且应用程序需要传递特定的主机头,我们总是会遇到负载平衡地址,这意味着我们无法确保群集中所有盒子上的所有服务都已实际启动。



由于IIS中的单个wcf应用程序只能有一个主机头,因此唯一的另一种方法是设置指向同一应用程序的第二个iis网站。我只是不确定是否会这样做,因为它会在另一个主机环境中。



另一种选择可能是对IIS的扩展(如果不是确定这是否可能)当IIS或应用程序池实际再次启动时,可以调用我们的每个服务。 (当应用程序池已经回收但在第一个用户请求之前注意到这一点。



任何想法都将非常受欢迎。



谢谢



Gineer

解决方案

对于我们这些人在7.5之前的IIS版本上运行,我们正在测试以下解决方案......



正如原帖中所提到的,最初的想法是开火从每台机器上运行的服务到本地网站(托管WCF服务)的WebRequest,但这是不可能的,因为它们都使用主机头,并且它们都存在于网络负载平衡的服务器场中。 / p>

然后我们认为我们可以简单地将web请求中的自定义主机头提供给localhost。



转你无法在WebRequest中更新主机头名称。这是一个只读字段。



使用代理类进行混乱可以使它工作。
请参阅: http://social.msdn.microsoft。 com / forums / zh-CN / netfxnetcom / thread / 1b35c665-fe32-4433-8877-a62f2d400a8e /



以下是我的测试代码的一小部分在C#中。

  WebRequest req = WebRequest.Create(< Correct Host name>); 
req.Proxy = new WebProxy(127.0.0.1);

StreamReader stream = new StreamReader(
req.GetResponse()。GetResponseStream());

StringBuilder sb = new StringBuilder();

String LineString;

while((LineString = stream.ReadLine())!= null)
{
if(LineString.Length> 0)
sb.Append(LineString );
}
stream.Close();

String response = sb.ToString();

这可能不是代理类的用途,但似乎无论哪种方式都有效。



Gineer



Ps。不,您不需要在本地主机上安装任何实际的代理服务器。


We have a system where we host a couple of WCF applications in IIS. Some of these applications have a bit of an extended start-up time taking a couple of seconds (more than a users would be happy to wait for). Once it's up and running, everything is snappy though, so it really is only the startup time.

The client also has a requirement to recycle the application pools every night.

Is there some way to wake all these services up so that the start-up time is not an issue for the first user of the system?

Initial thoughts where to write a windows service that would simply call a lightweight method on each service every x (configurable) minutes to keep the apps alive, but since we are in a load balanced environment, and the applications need specific host headers to be passed, we would always hit the load balance address which means there is no way for us to make sure that all services on all boxes in the cluster are actually started.

Since a single wcf application in IIS can only have a single host header, the only other way would be to setup a second iis web site pointing at the same application. I'm just not sure if that would do the trick since it would be in another host context.

another option could be to wright something like an extension to IIS (not sure if this is possible yet) that could call each of our services when IIS or the app pool actually starts up again. (Something that notices when the app pool has recycled but before the first user request.

Any ideas would be much appreciated.

Thanks

Gineer

解决方案

For those of us running on a version of IIS before 7.5, we are in the process of testing the following solution...

As mentioned in the original post, the initial idea was to fire off a WebRequest from a service running on each machine to the local web sites (which host the WCF services), but this would be impossible, since they all make use of Host headers, and they all live in a Network load balanced farm.

We then thought that we could simply provide the custom host headers in the web request to the localhost.

Turns out you can not update the host header name in a WebRequest. It’s a Read only field.

Messing with a proxy class makes it work though. See: http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/1b35c665-fe32-4433-8877-a62f2d400a8e/

And a short piece of my test code below in C#.

        WebRequest req = WebRequest.Create("<Correct Host name>");
        req.Proxy = new WebProxy("127.0.0.1");

        StreamReader stream = new StreamReader(
            req.GetResponse().GetResponseStream());

        StringBuilder sb = new StringBuilder();

        String LineString;

        while ((LineString = stream.ReadLine()) != null)
        {
            if (LineString.Length > 0)
                sb.Append(LineString);
        }
        stream.Close();

        String response = sb.ToString();

This may not be what the proxy class was intended for, but it seems to work either way.

Gineer

Ps. No, you do not need to have any actual proxy server installed on the local host machine.

这篇关于如何:避免IIS托管的WCF应用程序的冷启动问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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