首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >AutoStart/预热功能在IIS 7.5 / WCF服务中不起作用

AutoStart/预热功能在IIS 7.5 / WCF服务中不起作用
EN

Stack Overflow用户
提问于 2012-05-06 12:43:32
回答 4查看 9.2K关注 0票数 16

为了从头开始测试IIS/WCF实现的许多令人头疼的问题,我构建了HelloWorld服务,客户机(非常好地)遍历了here。我为net.tcp添加了端点,对于IIS 7.5 (在Windows7上)下的两种绑定,该服务都可以在它自己的ApplicationPool HW中正常工作。

我正在尝试的是宣布的AutoStart和预加载(或“预热缓存”)功能。我非常仔细地遵循了herehere的说明(彼此非常相似,但总能有不同的意见)。这意味着我

1)设置应用程序池startMode...

代码语言:javascript
复制
<applicationPools> 
     <!-- ... -->
     <add name="HW" managedRuntimeVersion="v4.0" startMode="AlwaysRunning" /> 
</applicationPools>

2) ...enabled serviceAutoStart并设置指向我的serviceAutoStartProvider的指针

代码语言:javascript
复制
<site name="HW" id="2">
    <application path="/" applicationPool="HW" serviceAutoStartEnabled="true" serviceAutoStartProvider="PreWarmMyCache" />
    <!-- ... -->
</site>

3) ...and指定了所述提供程序,类的GetType().AssemblyQualifiedName在下面完整列出

代码语言:javascript
复制
<serviceAutoStartProviders> 
    <add name="PreWarmMyCache" type="MyWCFServices.Preloader, HelloWorldServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
</serviceAutoStartProviders>

代码语言:javascript
复制
using System;

namespace MyWCFServices
{
    public class Preloader : System.Web.Hosting.IProcessHostPreloadClient
    {
        public void Preload(string[] parameters)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\temp\PreloadTest.txt");
            sw.WriteLine("Preload executed {0:G}", DateTime.Now);
            sw.Close();
        }
    }
}

遗憾的是,所有这些手动配置,加上几个iisreset调用,我什么也得不到。在任务管理器中没有启动w3wp.exe进程(尽管如果我启动HelloWorldClient就会启动),没有文本文件,最重要的是,没有令人满意的结果。

令人沮丧的是,无论是在SO上还是在更广泛的网络上,关于这一特性的讨论都少得令人沮丧,这里的几个类似问题也没有引起多少关注,所有这些都敲响了警钟。也许没有必要--有没有专家曾经在这条路上走过一两次,愿意附和一下?(如果你能建议一个好的地方托管它,我很乐意提供整个解决方案。)

编辑:我尝试将Preload方法中路径重置为相对的App_Data文件夹(另一个SO回答建议),但这并不重要。此外,我还了解到,w3wp.exe进程在简单地浏览本地主机时触发。该进程消耗了令人印象深刻的17MB内存来提供其单个微型OperationContract,而价格提供的预加载价值为零。17MB的ColdDeadCache。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-05-15 19:42:48

对于您的问题,这是一种略有不同的方法:

  1. 使用Windows Server AppFabric进行服务自动启动
  2. 使用WCF基础结构执行自定义启动代码

答案1:Appfabric AutoStart feature应该开箱即用(如果你没有使用MVC的ServiceRoute来注册你的服务,它们必须在Web.config的serviceActivations部分或者使用物理的*.svc文件来指定。

Re 2:要将自定义启动代码注入到WCF管道中,您可以使用如下属性:

代码语言:javascript
复制
using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCF.Extensions
{
    /// <summary>
    /// Allows to specify a static activation method to be called one the ServiceHost for this service has been opened.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
    public class ServiceActivatorAttribute : Attribute, IServiceBehavior
    {
        /// <summary>
        /// Initializes a new instance of the ServiceActivatorAttribute class.
        /// </summary>
        public ServiceActivatorAttribute(Type activatorType, string methodToCall)
        {
            if (activatorType == null) throw new ArgumentNullException("activatorType");
            if (String.IsNullOrEmpty(methodToCall)) throw new ArgumentNullException("methodToCall");

            ActivatorType = activatorType;
            MethodToCall = methodToCall;
        }

        /// <summary>
        /// The class containing the activation method.
        /// </summary>
        public Type ActivatorType { get; private set; }

        /// <summary>
        /// The name of the activation method. Must be 'public static void' and with no parameters.
        /// </summary>
        public string MethodToCall { get; private set; }


        private System.Reflection.MethodInfo activationMethod;

        #region IServiceBehavior
        void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            serviceHostBase.Opened += (sender, e) =>
                {
                    this.activationMethod.Invoke(null, null);
                };
        }

        void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            // Validation: can get method
            var method = ActivatorType.GetMethod(name: MethodToCall,
                             bindingAttr: System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public,
                             callConvention: System.Reflection.CallingConventions.Standard,
                             types: Type.EmptyTypes,
                             binder: null,
                             modifiers: null);
            if (method == null)
                throw new ServiceActivationException("The specified activation method does not exist or does not have a valid signature (must be public static).");

            this.activationMethod = method;
        }
        #endregion
    }
}

..which可以像这样使用:

代码语言:javascript
复制
public static class ServiceActivation
{
    public static void OnServiceActivated()
    {
        // Your startup code here
    }
}

[ServiceActivator(typeof(ServiceActivation), "OnServiceActivated")]
public class YourService : IYourServiceContract
{

}

这正是我们已经在大量服务上使用了相当长一段时间的方法。将WCF用于自定义启动代码(而不是依赖于ServiceBehavior基础结构)的额外好处是它可以在任何宿主环境中工作(包括。自托管),可以更容易地进行测试。

票数 4
EN

Stack Overflow用户

发布于 2013-08-15 01:45:37

我知道这听起来很荒谬,但我也遇到了同样的问题(在更改配置后w3wp.exe不会自动触发),这是因为我在编辑applicationHost.config文件时没有在管理模式下运行文本编辑器。我犯了个愚蠢的错误。

在我的辩解中,我使用的是Notepad++,它告诉我它在保存,而实际上它不是。

票数 3
EN

Stack Overflow用户

发布于 2012-05-16 20:42:56

我也做过同样的事。它起作用了..。

在预加载方法中,我从一份不错的白皮书中复制了一些代码,可用here

预加载方法看起来像...

代码语言:javascript
复制
 public void Preload(string[] parameters) 
 {     
        bool isServceActivated = false; 
        int attempts = 0; 
        while (!isServceActivated && (attempts <10)) 
        {
            Thread.Sleep(1 * 1000);
            try
            {
                string virtualPath = "/Test1/Service1.svc";
                ServiceHostingEnvironment.EnsureServiceAvailable(virtualPath);
                isServceActivated = true;
            }
            catch (Exception exception)
            {
                attempts++;
                //continue on these exceptions, otherwise fail fast 
                if (exception is EndpointNotFoundException ||
                    exception is ServiceActivationException || 
                    exception is ArgumentException) 
                {
                    //log 
                }
                else
                {
                    throw;
                }
            }
        }
   }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10468167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档