[英]WCF REST and SOAP Service without WebServiceHostFactory
虽然阅读了很多帖子,例如( 这个看起来很受欢迎 )但我似乎无法将我的服务作为多个端点与SOAP和REST协议兼容 - 我的问题似乎与
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
后台服务代码中的元素。
如果我将其删除,我的SOAP端点工作正常,但找不到我的JSON端点。 如果我将该行放入,我的REST端点会像鸟一样唱歌,而SOAP端点会导致Service.svc页面上出现“Endpoint not found”。
我的操作似乎以标准方式设置,例如:
[OperationContract]
[WebGet(UriTemplate = "/GetData", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string GetData();
和配置文件
<endpoint address="rest" binding="webHttpBinding" contract=".IMeterService" behaviorConfiguration="REST" />
<endpoint address="soap" binding="wsHttpBinding" contract="IMeterService" bindingConfiguration="secureBasic" />
<behavior name="REST">
<webHttp />
</behavior>
我怎样才能做到这一点? 有没有办法在没有 System.ServiceModel.Activation.WebServiceHostFactory属性的情况下设置REST端点?
提前致谢。
如果未在.svc文件中指定任何工厂,则所有端点都将来自web.config文件 - WCF将尝试查找其name
属性与完全限定名称匹配的<system.serviceModel / service>
元素服务类。 如果找不到,那么它将添加一个默认端点(使用basicHttpBinding
,除非您更改了默认映射)。 这似乎是你所面临的。 确认<service>
元素的“name”属性与.svc文件中Service
属性的值匹配,并且您应该让两个端点正常工作。
您可以尝试做的另一件事是在服务中启用跟踪(level = Information)以查看在服务上实际打开了哪些端点。 下图:
这个例子的服务器并不重要:
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定义了两个端点,它们显示在跟踪中:
<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的“帮助页面”(当您浏览它时,它告诉该服务没有启用元数据)。
您可以尝试将此设置与您的设置进行比较,或者从这个简单的设置开始,然后开始添加您的组件,直到遇到问题为止。 这将有助于隔离问题。
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.