没有WebServiceHostFactory的WCF REST和SOAP服务

时间:2011-09-23 15:52:28

标签: wcf rest soap

尽管阅读了一些帖子,例如(This one seems popular)我似乎无法将我的服务公开为多个兼容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>

我怎样才能做到这一点?有没有办法设置REST端点没有 System.ServiceModel.Activation.WebServiceHostFactory属性?

提前致谢。

1 个答案:

答案 0 :(得分:5)

如果未在.svc文件中指定任何工厂,则所有端点都将来自web.config文件 - WCF将尝试查找<system.serviceModel / service>属性与name属性匹配的basicHttpBinding元素服务类的完全限定名称。如果找不到,则会添加默认端点(使用<service>,除非您更改了默认映射)。这似乎是你所面临的。确认Service元素的“name”属性与.svc文件中namespace MyNamespace { [ServiceContract] public interface ITest { [OperationContract] string Echo(string text); } public class Service : ITest { public string Echo(string text) { return text; } } } 属性的值匹配,并且您应该让两个端点正常工作。

您可以尝试做的另一件事是在服务中启用跟踪(level = Information)以查看在服务上实际打开了哪些端点。下图:

SvcTraceViewer image

此示例的服务器并不重要:

<% @ServiceHost Service="MyNamespace.Service" Language="C#" debug="true" %>

Service.svc没有指定工厂:

<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>

web.config定义了两个端点,它们显示在跟踪中:

{{1}}

请注意,侦听器中显示了一个额外的侦听器,它是来自WCF的“帮助页面”(当您浏览它时,它告诉该服务没有启用元数据)。

您可以尝试将此设置与您的设置进行比较,或者从这个简单的设置开始,然后开始添加您的组件,直到遇到问题为止。这将有助于隔离问题。

祝你好运!

相关问题