如何使用WCF Starter Kit公开SOAP端点?

时间:2011-01-16 10:25:29

标签: .net wcf web-services rest soap

我是WCF的新手,所以请耐心等待。

使用最新版本的WCF REST Starter工具包,我创建了一个由Android应用程序调用的Web服务。 RESTful端点工作正常,但我想创建一个SOAP端点,以便.NET客户端能够使用它并生成所有必需的类。

我仍在使用默认配置文件,我对我需要做些什么感到困惑。

这是

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

</configuration>

我想我需要在配置文件中添加以下内容,但我不确定它属于哪里,或者我是否在正确的路径上。

<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="Service1"/>
<endpoint name="soap" address="soap" binding="basicHttpBinding" contract="Service1"/>

我只有一个类,那就是Service1.cs。我试图做一些改变,但我没有成功。

我想知道我要添加什么,并解释为什么需要它会很精彩。

- 更新 -

插入服务标签后,我无法在Visual Studio中使用“添加服务引用”功能。我发现'HttpGetEnabled'必须是真的,所以它会将服务元数据发布到http。

我添加了这个,它似乎正在发挥作用。

<behaviors>
          <serviceBehaviors>
              <behavior name="Service1Behavior">
                  <serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>
              </behavior>
          </serviceBehaviors>
      </behaviors>

如果我添加更多服务,是否还必须为该服务再创建两个端点?

感谢。

1 个答案:

答案 0 :(得分:0)

如果您要在服务上公开其他端点,则需要将这些端点添加到配置中的<service name="....">标记中:

<system.serviceModel>
  <services>
    <service name="NameSpace.ServiceClassName>
       <endpoint name="rest" 
                 address=""
                 binding="webHttpBinding"
                 contract="IService1" />
       <endpoint name="soap" 
                 address="soap"
                 binding="basicHttpBinding"
                 contract="IService1" />
       <endpoint name="mex" 
                 address="mex"
                 binding="mexHttpBinding"
                 contract="IMetadataExchange" />
    </service>
  </services>
</system.serviceModel>

您显然正在使用WCF 4标准端点 - 如果您想更改它,则需要明确说明您需要的所有配置。

现在,您的服务(在ServiceClassName命名空间中的NameSpace中实现)将有三个端点:

  • * .svc文件的基地址将是使用webHttpBinding
  • 的REST端点
  • (service.svc)/soap地址将是相同的合同(相同的服务方法),但使用基于SOAP的basicHttpBinding
  • (service.svc)/mex地址将具有SOAP客户端的服务元数据