为何IIS对WCF服务返回405方法不允许错误?求排查
我在Windows 10上搭建了IIS 10开发环境部署.NET WCF服务,服务基本可用,但通过POST动词调用.svc文件时返回405方法不允许错误。目前有建议称是因为先安装VS再装IIS导致,需要卸载所有组件后重装且先安装IIS。
事件日志内容
<EventData> <Data>System.ServiceModel.ServiceHostingEnvironment+HostingManager/30405926</Data> <Data>System.ServiceModel.ServiceActivationException: The service '/blah.svc' cannot be activated due to an exception during compilation. The exception message is: Service 'com.blah.webservice.zzz.blah' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.. ---> System.InvalidOperationException: Service 'com.blah.webservice.zzz.blah' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element. at System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreApplicationEndpoints(ServiceDescription description) at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) at System.ServiceModel.ServiceHostBase.InitializeRuntime() at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at System.ServiceModel.ServiceHostingEnvironment.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity) at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) --- End of inner exception stack trace --- at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) at System.ServiceModel.ServiceHostingEnvironment.EnsureServiceAvailableFast(String relativeVirtualPath, EventTraceActivity eventTraceActivity)</Data> <Data>w3wp</Data> <Data>13140</Data> </EventData>
已知情况:相同代码在另外三个Windows 10开发环境中可正常运行,说明代码和web.config无问题。IIS的处理程序映射看似正常,但错误提示为编译异常。
提问:请问该Win10主机上还有哪些可能导致编译失败的原因?
WCF组件未正确注册:无需直接卸载重装组件,可手动注册WCF相关模块。以管理员身份打开命令提示符,运行以下命令(64位系统需同时执行Framework64下的对应命令):
%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i %windir%\Microsoft.NET\Framework\v4.0.30319\ServiceModelReg.exe -r应用程序池配置异常:确认应用程序池的.NET CLR版本与WCF服务的.NET版本匹配,托管管道模式设置为集成模式(经典模式易引发端点识别问题)。同时检查应用程序池身份的权限,确保其能读取web.config和bin目录下的程序集。
文件权限不足:IIS应用程序池身份用户对网站根目录、bin文件夹没有读取/执行权限,导致无法加载服务配置和程序集,触发编译异常。
web.config读取受阻:当前环境下web.config可能存在权限问题、被上级配置锁定,或本地配置转换错误。可尝试替换为最简配置测试,或检查配置文件的读取权限。
程序集加载失败:bin文件夹中依赖程序集缺失、版本不匹配,或强名称签名验证失败,导致服务编译时无法加载依赖。可查看事件查看器的.NET运行时日志,获取程序集加载失败的详细信息。
IIS模块冲突:第三方IIS模块(如URL重写、安全防护模块)可能干扰WCF服务激活流程,导致端点识别失败。可临时禁用非必要模块测试服务是否恢复正常。
临时编译目录权限问题:ASP.NET临时编译目录(默认路径
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files)权限不足,无法生成服务临时编译文件。需确保应用程序池身份对该目录有读写权限。
内容的提问来源于stack exchange,提问作者Tom McDonald

