为了从头开始测试IIS/WCF实现的许多令人头疼的问题,我构建了HelloWorld服务,客户机(非常好地)遍历了here。我为net.tcp添加了端点,对于IIS 7.5
(在Windows7上)下的两种绑定,该服务都可以在它自己的ApplicationPool
HW中正常工作。
我正在尝试的是宣布的AutoStart和预加载(或“预热缓存”)功能。我非常仔细地遵循了here和here的说明(彼此非常相似,但总能有不同的意见)。这意味着我
1)设置应用程序池startMode
...
<applicationPools>
<!-- ... -->
<add name="HW" managedRuntimeVersion="v4.0" startMode="AlwaysRunning" />
</applicationPools>
2) ...enabled serviceAutoStart
并设置指向我的serviceAutoStartProvider
的指针
<site name="HW" id="2">
<application path="/" applicationPool="HW" serviceAutoStartEnabled="true" serviceAutoStartProvider="PreWarmMyCache" />
<!-- ... -->
</site>
3) ...and指定了所述提供程序,类的GetType().AssemblyQualifiedName
在下面完整列出
<serviceAutoStartProviders>
<add name="PreWarmMyCache" type="MyWCFServices.Preloader, HelloWorldServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</serviceAutoStartProviders>
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。
发布于 2012-05-15 19:42:48
对于您的问题,这是一种略有不同的方法:
答案1:Appfabric AutoStart feature应该开箱即用(如果你没有使用MVC的ServiceRoute来注册你的服务,它们必须在Web.config的serviceActivations
部分或者使用物理的*.svc
文件来指定。
Re 2:要将自定义启动代码注入到WCF管道中,您可以使用如下属性:
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可以像这样使用:
public static class ServiceActivation
{
public static void OnServiceActivated()
{
// Your startup code here
}
}
[ServiceActivator(typeof(ServiceActivation), "OnServiceActivated")]
public class YourService : IYourServiceContract
{
}
这正是我们已经在大量服务上使用了相当长一段时间的方法。将WCF用于自定义启动代码(而不是依赖于ServiceBehavior
基础结构)的额外好处是它可以在任何宿主环境中工作(包括。自托管),可以更容易地进行测试。
发布于 2013-08-15 01:45:37
我知道这听起来很荒谬,但我也遇到了同样的问题(在更改配置后w3wp.exe不会自动触发),这是因为我在编辑applicationHost.config文件时没有在管理模式下运行文本编辑器。我犯了个愚蠢的错误。
在我的辩解中,我使用的是Notepad++,它告诉我它在保存,而实际上它不是。
发布于 2012-05-16 20:42:56
我也做过同样的事。它起作用了..。
在预加载方法中,我从一份不错的白皮书中复制了一些代码,可用here!
预加载方法看起来像...
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;
}
}
}
}
https://stackoverflow.com/questions/10468167
复制相似问题