没有 WebServiceHostFactory 的 WCF REST 和 SOAP 服务 [英] WCF REST and SOAP Service without WebServiceHostFactory

查看:41
本文介绍了没有 WebServiceHostFactory 的 WCF REST 和 SOAP 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管阅读了许多帖子,例如(这个似乎很受欢迎) 我似乎无法将我的服务公开为与 SOAP 和 REST 协议兼容的多个端点 - 我的问题似乎出在

 Factory="System.ServiceModel.Activation.WebServiceHostFactory"

服务代码隐藏页面中的元素.

如果我忽略它,我的 SOAP 端点可以正常工作,但找不到我的 JSON 端点.如果我把这条线放进去,我的 REST 端点会像小鸟一样唱歌,而 SOAP 端点会在 Service.svc 页面上显示找不到端点".

我的操作似乎以标准方式设置,例如:

 [操作合约][WebGet(UriTemplate = "/GetData", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]字符串 GetData();

和配置文件

 <endpoint address="soap" binding="wsHttpBinding" contract="IMeterService" bindingConfiguration="secureBasic"/><行为名称=REST"><webHttp/></行为>

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

提前致谢.

解决方案

如果您没有在 .svc 文件中指定任何工厂,则所有端点都将来自 web.config 文件 - WCF 将尝试找到一个 元素,其 name 属性与服务类的完全限定名称相匹配.如果它没有找到,那么它将添加一个默认端点(使用 basicHttpBinding,除非您更改了默认映射).这似乎就是你所面临的.确认 <service> 元素的name"属性与 .svc 文件中 Service 属性的值匹配,并且您应该有两个端点很好.

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

这个例子的服务器没什么大不了的:

namespace MyNamespace{[服务合同]公共接口 ITest{【经营合同】字符串回声(字符串文本);}公共类服务:ITest{公共字符串回声(字符串文本){返回文本;}}}

Service.svc 没有指定工厂:

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

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

<预><代码><配置><系统诊断><来源><source name="System.ServiceModel" switchValue="信息,ActivityTracing"传播活动=真"><听众><add type="System.Diagnostics.DefaultTraceListener" name="默认"><过滤器类型=""/></添加><add name="ServiceModelTraceListener"><过滤器类型=""/></添加></听众></来源></来源><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"><过滤器类型=""/></添加></sharedListeners><trace autoflush="true"/></system.diagnostics><system.serviceModel><行为><端点行为><行为名称="Web"><webHttp/></行为></endpointBehaviors></行为><服务><服务名称="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"/></服务></服务></system.serviceModel></配置>

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

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

祝你好运!

Despite reading a number of posts eg (This one seems popular) I can't seem to expose my service as multiple endpoints that are compatible with both the SOAP and REST protocol - my problem seems to be with the

  Factory="System.ServiceModel.Activation.WebServiceHostFactory"

element in the Service code behind page.

If I leave it out, my SOAP endpoint works grand, but my JSON endpoint is not found. If I put the line in, my REST endpoint sings like bird and the the SOAP endpoint is results in "Endpoint not found" on the Service.svc page.

My operations appear to set up in the standard way eg:

    [OperationContract]
    [WebGet(UriTemplate = "/GetData", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
    string GetData();

And the configuration file

 <endpoint address="rest" binding="webHttpBinding" contract=".IMeterService" behaviorConfiguration="REST" />

 <endpoint address="soap" binding="wsHttpBinding" contract="IMeterService" bindingConfiguration="secureBasic" />

 <behavior name="REST">
      <webHttp />
 </behavior>

How can I achieve this? Is there a way to set up the REST endpoint without the System.ServiceModel.Activation.WebServiceHostFactory attribute?

Thanks in advance.

解决方案

If you don't specify any factory in the .svc file, all the endpoints will come from the web.config file - WCF will try to find a <system.serviceModel / service> element whose name attribute matches the fully-qualified name of the service class. If it doesn't find one, then it will add a default endpoint (using basicHttpBinding, unless you changed the default mapping). That appears to be what you're facing. Confirm that the "name" attribute of the <service> element matches the value of the Service attribute in the .svc file, and you should have the two endpoints working out fine.

Another thing you can try to do is to enable tracing in the service (level = Information) to see which endpoints were actually opened on the service. The image below:

The server for this example is nothing major:

namespace MyNamespace
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text;
        }
    }
}

The Service.svc doesn't have factory specified:

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

And the web.config defines two endpoints, which are shown in the 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>

Notice that there's an extra listener shown in the listener, it's the "help page" from WCF (the one which tells, when you browse to it, that the service doesn't have metadata enabled).

You can try either to compare this setup with yours, or start with this simple one, then start adding component from yours until you hit the issue. That will help isolate the problem.

Good luck!

这篇关于没有 WebServiceHostFactory 的 WCF REST 和 SOAP 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆