web.config配置异常致WCF服务调用404,求正确配置方案
核心问题分析
URL路径错误
你使用的http://localhost:55331/wcfDateTest.DateTimeService.svc/ProcessDateTime存在明显错误:wcfDateTest是服务类的命名空间,并非.svc文件的名称,实际服务文件应为DateTimeService.svc,因此这个URL从根源上无法匹配服务地址。手动配置
后的Endpoint路径冲突
当你添加<services>配置段时,WCF会停用自动生成默认Endpoint的机制,转而使用你定义的Endpoint规则。你当前配置的Endpoint指定了address="rest",这意味着服务的REST接口会挂载在DateTimeService.svc/rest/路径下,而非原有的根路径。
修复方案
方案1:使用正确的URL访问
按照配置的Endpoint地址,使用以下URL访问:http://localhost:55331/DateTimeService.svc/rest/ProcessDateTime
方案2:修改配置保留原URL结构
如果希望继续使用http://localhost:55331/DateTimeService.svc/ProcessDateTime访问,只需将Endpoint的address设为空字符串,让它直接使用服务的基地址(.svc文件路径):
<services> <service name="wcfDateTest.DateTimeService"> <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="wcfDateTest.IDateTimeService" /> </service> </services>
方案3:添加元数据Endpoint(可选)
如果需要保留元数据访问能力,建议在<service>节点下添加一个元数据Endpoint,避免配置后无法通过.svc页面查看服务信息:
<services> <service name="wcfDateTest.DateTimeService"> <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="wcfDateTest.IDateTimeService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services>
为什么移除段后正常?
当你删除<services>配置时,WCF会启用默认配置映射,自动为REST服务生成匹配原有URL结构的Endpoint,因此无需额外配置即可正常访问。
内容的提问来源于stack exchange,提问作者Anders

