2014-01-24 102 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,它会起作用,如果我将它带回来失败......您之前如何体验过这一点?有任何想法吗? –