如果您未在 .svc 文件中指定任何工厂,则所有端点都将来自 web.config 文件 - WCF 将尝试查找其 name 属性与 完全匹配的 <system.serviceModel / service> 元素- 服务类的限定名称。如果找不到,那么它将添加一个默认端点(使用basicHttpBinding,除非您更改了默认映射)。这似乎就是你所面临的。确认 <service> 元素的“名称”属性与 .svc 文件中 Service 属性的值匹配,并且您应该让两个端点正常工作。
您可以尝试做的另一件事是在服务中启用跟踪(级别 = 信息),以查看在服务上实际打开了哪些端点。下图:
这个例子的服务器没什么大不了的:
namespace MyNamespace
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string Echo(string text);
}
public class Service : ITest
{
public string Echo(string text)
{
return text;
}
}
}
Service.svc 没有指定工厂:
<% @ServiceHost Service="MyNamespace.Service" Language="C#" debug="true" %>
并且 web.config 定义了两个端点,在 traces 中显示:
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\temp\web_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
<trace autoflush="true"/>
</system.diagnostics>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="MyNamespace.Service">
<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration=""
name="basic" contract="MyNamespace.ITest" />
<endpoint address="web" behaviorConfiguration="Web" binding="webHttpBinding"
bindingConfiguration="" name="web" contract="MyNamespace.ITest" />
</service>
</services>
</system.serviceModel>
</configuration>
请注意,侦听器中显示了一个额外的侦听器,它是来自 WCF 的“帮助页面”(当您浏览到它时,它会告诉您该服务没有启用元数据)。
您可以尝试将此设置与您的设置进行比较,或者从这个简单的设置开始,然后开始从您的设置中添加组件,直到遇到问题。这将有助于隔离问题。
祝你好运!