2014-01-24 42 views
1

我打算髮布我的Web服務,以便它可以在外部網絡上使用。問題的關鍵在於Web管理員不希望將此Web服務設爲外部,而是希望我圍繞它來包裝代理並使代理處於外部。我不喜歡代理部分,因爲它不必維護服務和代理上的版本。我想知道如果使用外部網絡(URL)訪問web服務以使用SSL並請求身份驗證,但是如果您在內部網絡(URL)中使用它以不請求身份驗證或SSL,是否有簡單的方法來執行此操作。我試過在webconfig中有兩個端點配置,一個安全,一個不安全,但問題是當你使用web服務時,綁定顯示和客戶端可以選擇一個或兩者。如果任何人做了這種不同的方式,或者以不同的方式讓我知道,通常我會採取一種方法,要麼是所有安全方法,要麼是所有非安全方法,但取決於網絡方面並不相同非常感謝:)面向內部和外部客戶端的WCF安全性

回答

1

你會很高興知道,WCF已經內置支持你的'代理'。它被稱爲路由服務並在WCF 4.0和更高版本中可用。

您可以對其進行配置,以將特定服務合同的Internet呼叫路由到在Intranet中運行的WCF服務。它甚至可以轉換綁定,以便使用TCP綁定的內部服務可以由外部客戶使用可以穿過防火牆的HTTP綁定來調用。

它只需要知道哪些合約路由到哪裏。因此,當您的合同更改時,無需更新...

請參閱here以瞭解更多信息。

EDITED:以下示例system.serviceModel節點允許客戶端向路由服務添加服務引用。訣竅是讓路由服務將其本身作爲其路由到的服務。請參閱serviceActivations節點。請注意,這樣就不需要有.svc文件。

接下來我們定義兩個端點過濾器,將服務的請求重定向到路由服務,並將mex端點的請求(顯示元數據)重定向到路由服務的mex端點。請參閱filters節點。

最後,我們明確地禁止通過http暴露路由服務本身的元數據,強制客戶端使用mex(我們正在路由)進行發現。請參閱serviceBehaviors節點。

再次編輯:爲MEX大小限制添加解決方案

<system.serviceModel> 
    <serviceHostingEnvironment> 
    <serviceActivations> 
     <!--Lets the routing service impose himself as Service.svc (No .SVC file is needed!!!) --> 
     <add service="System.ServiceModel.Routing.RoutingService" relativeAddress="Service.svc" /> 
    </serviceActivations> 
    </serviceHostingEnvironment> 
    <bindings> 
    <wsHttpBinding> 
     <!-- a mexHttpBinding is in fact a wsHttpBinding with security turned off --> 
     <binding name="mexBinding" maxReceivedMessageSize="5000000"> 
     <security mode="None"/> 
     </binding> 
    </wsHttpBinding> 
    </bindings> 
    <behaviors> 
    <serviceBehaviors> 
     <behavior > 
     <!-- Use the filter table with the name 'Filters' defined below --> 
     <routing routeOnHeadersOnly="false" filterTableName="Filters"/> 
     <serviceDebug includeExceptionDetailInFaults="true"/> 
     <!-- Disable exposing metadata for this routing service to force discovery using mex --> 
     <serviceMetadata httpGetEnabled="false"/> 
     </behavior> 
    </serviceBehaviors> 
    </behaviors> 
    <routing> 
    <filters> 
     <!-- Declare a routing filter that filters on endpoint Service.svc --> 
     <filter name="WcfServiceFilter" filterType="EndpointAddress" filterData="http://localhost/ServiceRouter/Service.svc" /> 
     <!-- Declare a routing filter that filters on mex endpoint of Service.svc --> 
     <filter name="WcfServiceFilter.mex" filterType="EndpointAddress" filterData="http://localhost/ServiceRouter/Service.svc/mex"/> 
    </filters> 
    <filterTables> 
     <!-- Declare the routing table to use --> 
     <filterTable name="Filters"> 
     <!-- requests that match the WcfServiceFilter (declared above) should be routed to the client endpoint WcfService --> 
     <add filterName="WcfServiceFilter" endpointName="WcfService"/> 
     <!-- requests that match the WcfServiceFilter.mex (declared above) should be routed to the client endpoint WcfService.mex --> 
     <add filterName="WcfServiceFilter.mex" endpointName="WcfService.mex"/> 
     </filterTable> 
    </filterTables> 
    </routing> 
    <services> 
    <!-- Declare our service instance and the endpoints it listens on --> 
    <service name="System.ServiceModel.Routing.RoutingService"> 
     <!-- Declare the endpoints we listen on --> 
     <endpoint name="WcfService" contract="System.ServiceModel.Routing.IRequestReplyRouter" binding="wsHttpBinding" /> 
     <endpoint name="WcfServiceFilter.mex" address="mex" contract="System.ServiceModel.Routing.IRequestReplyRouter" binding="mexHttpBinding" /> 
    </service> 
    </services> 
    <client> 
    <!-- Define the client endpoint(s) to route messages to --> 
    <endpoint name="WcfService" address="http://localhost/WcfService/Service.svc" binding="wsHttpBinding" contract="*" /> 
    <endpoint name="WcfService.mex" address="http://localhost/WcfService/Service.svc/mex" binding="wsHttpBinding" bindingConfiguration="mexBinding" contract="*" /> 
    </client> 
</system.serviceModel> 
+0

我看了一下路由服務,並嘗試了一些例子,但它看起來像它使複雜的客戶端使用該服務,因爲你真的不能引用服務,但一個虛擬代理不清除的方法,除非你分別指定wsdl ...我的理解是否正確? –

+0

路由服務是一個透明的代理,所以你的客戶端不需要知道它在那裏。有關如何配置路由服務以公開其服務元數據的詳細信息,請參閱[本主題]中的答案(http://stackoverflow.com/questions/17476156/wcf-how-to-combine-several-services -into-single-wsdl/19890944#19890944) –

+0

真棒......我感謝您的幫助,因爲這是我第一次創建路由服務......我創建了一個示例服務,並能夠通過'代理'。現在,當我嘗試將「代理」鏈接到我們的實際服務時,它給了我一個錯誤。在單步執行代碼的過程中,我發現它對於某些DataContracts存在問題,這些問題沒有任何意義,因爲它們沒有什麼不同尋常的地方。如果我刪除了DataContract,它會起作用,如果我將它帶回來失敗......您之前如何體驗過這一點?有任何想法嗎? –