2011-12-03 83 views
0

我想在IIS 7.5中託管WCF 4.0服務,並且能夠使用basicHttpBinding綁定到WCF 4.0服務,還可以使用webHttpBinding以REST方式綁定它。WCF服務上的多個綁定

我需要能夠訪問它,像這樣:

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文件:

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

別t似乎能夠通過SOAP以REST方式和SOAP訪問它們。

謝謝! Sam

+1

爲什麼它必須是相同的基本URL?爲什麼不'http:// server/wcf/restservice/method/parameters'和'http:// server/wcf/soapservice/service.svc'? –

+0

@JohnSaunders - 我們將擁有20-25個WCF服務,所以我在考慮這個命名約定,以便於維護,但我想它可能有點不同。 – Sam

+2

我的建議將允許「soapservice」和「restservice」在IIS中成爲獨立的應用程序。 「restservice」將成爲映射到路由的平穩URL的開始。 –

回答

2

只需使用OperationContract和WebGet屬性裝飾您的方法即可。現在,添加以下system.serviceModel元素在你的服務器上的web.config

<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訪問相同的服務和網址情況如下:

SOAP - >http://localhost/virtualdirectoryname/CustomerContactService.svc

REST - >http://localhost/virtualdirectoryname/CustomerContactService/method/parameters

現在瀏覽到您在IE的服務,你應該看到的SOAP服務器,當你瀏覽到.svc文件,當你瀏覽到其他URL,你應該看到無論是XML即將在應該下載包含json格式響應的文件瀏覽器。