WCF服务上的多个绑定

时间:2011-12-03 00:39:59

标签: wcf wcf-binding wcf-rest

我想在IIS 7.5中托管WCF 4.0服务,并且能够使用basicHttpBinding和REST webHttpBinding绑定它。

我需要能够像这样访问它:

http://server/wcf/service/method/parameters(REST)

也是如此:

http://server/wcf/service.svc(基本HTTP)

到目前为止,我已将此文件用于我的Web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="json">
          <webHttp defaultOutgoingResponseFormat="Json" helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="SAIF.Services.WCF.Services.CustomerContactService">
        <endpoint address="CustomerContact" behaviorConfiguration="json" binding="webHttpBinding" contract="SAIF.Services.WCF.Contracts.ICustomerContactService" />
        <endpoint address="CustomerContact.svc" binding="basicHttpBinding" contract="SAIF.Services.WCF.Contracts.ICustomerContactService" />
      </service>
      <service name="SAIF.Services.WCF.Services.OnlineLoginService">
        <endpoint address="OnlineLogin" binding="basicHttpBinding" contract="SAIF.Services.WCF.Contracts.IOnlineLoginService" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
      <serviceActivations>
        <add relativeAddress="CustomerContact.svc" service="SAIF.Services.WCF.Services.CustomerContactService" />
      </serviceActivations>
    </serviceHostingEnvironment>
  </system.serviceModel>
</configuration>

我的global.asax文件中也有这个扩展名为less activation's:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
        Routing.RouteTable.Routes.Add(New ServiceRoute("CustomerContact", New ServiceHostFactory, GetType(SAIF.Services.WCF.Services.CustomerContactService)))
        Routing.RouteTable.Routes.Add(New ServiceRoute("OnlineLogin", New ServiceHostFactory, GetType(SAIF.Services.WCF.Services.OnlineLoginService)))
    End Sub

我用这个装饰了服务:     

我的服务界面与UriTemplates

似乎无法通过REST和SOAP来访问它们。

谢谢! 萨姆

1 个答案:

答案 0 :(得分:2)

只需使用OperationContract和WebGet属性修饰您的方法。现在将以下内容添加到服务器web.config中的system.serviceModel元素

    <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">
              <readerQuotas maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" />
            </standardEndpoint>
          </webHttpEndpoint>
        </standardEndpoints>

注意:哟 您可以从上面删除json端点(因为我们将使用新的REST Api实现清晰的URL概念),并且在您的global.asax中只需替换以下内容:

Routing.RouteTable.Routes.Add(New ServiceRoute("CustomerContactService", New WebServiceHostFactory, typeof(SAIF.Services.WCF.Services.CustomerContactService)));

现在,当您执行上述操作后,您应该能够通过SOAP和REST访问相同的服务,并且URL将如下所示:

SOAP - &gt; http://localhost/virtualdirectoryname/CustomerContactService.svc

REST - &gt; http://localhost/virtualdirectoryname/CustomerContactService/method/parameters

现在浏览到IE中的服务,当你浏览.svc文件时应该看到SOAP服务器,当你浏览到其余的URL时,你会看到应该下载包含在文件浏览器中的xml以json格式的响应。