soh*_*970 26 wcf web-config wcf-binding
我花了很多时间搞清楚如何配置我的WCF服务,以便它们适用于生产环境中的https.
基本上,我需要这样做:
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<services>
<service name="MyNamespace.MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" bindingNamespace="https://secure.mydomain.com" binding="basicHttpBinding" bindingConfiguration="HttpsBinding" contract="MyNamespace.IMyService"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="HttpsBinding">
<security mode="Transport">
<transport clientCredentialType="None"></transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)
将bindingNamespace
属性添加到端点是使其工作的最终因素.
但是这个配置在我在常规http下工作的本地开发环境中不起作用.所以我的配置是:
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<services>
<service name="MyNamespace.MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="MyNamespace.IMyService"/>
</service>
</services>
Run Code Online (Sandbox Code Playgroud)
这里的区别是我将httpsGetEnabled
属性设置为false,并删除了bindingConfiguration和bindingNamespace.
问题是:如何创建一个处理BOTH的配置块?
我真的很讨厌每次发布时都要对配置进行大量的特殊修改.是的,我知道我可以有一个自动更改值的后期构建任务,但我想尽可能合并配置.
我试过这样的事情:
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<services>
<service name="MyNamespace.MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="MyNamespace.IMyService"/>
<endpoint address="" bindingNamespace="https://secure.mydomain.com" binding="basicHttpBinding" bindingConfiguration="HttpsBinding" contract="MyNamespace.IMyService"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="HttpsBinding">
<security mode="Transport">
<transport clientCredentialType="None"></transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)
我认为将两个端点放在一起会为激活服务时提供两个选项.但是,这不起作用.我收到此错误:
找不到与绑定BasicHttpBinding的端点的方案https匹配的基址.注册的基地址方案是[http].
从环顾SO和互联网的其他部分看,似乎其他人在杀死这条龙时遇到了问题.
mar*_*c_s 18
好吧,你的组合配置的一个问题是你的两个端点在同一个地址 - 这是行不通的.
如果您在IIS中托管,那么您的服务器,虚拟目录和所需的*.svc文件将确定您的基本地址 - 它将类似于:
http://yourservername/VirtualDirectory/YourService.svc
Run Code Online (Sandbox Code Playgroud)
如果要拥有两个端点,则至少需要其中一个端点定义相对地址:
<services>
<service name="MyNamespace.MyService"
behaviorConfiguration="MyServiceBehavior">
<endpoint
address="basic"
binding="basicHttpBinding"
contract="MyNamespace.IMyService"/>
<endpoint
address="secure"
binding="basicHttpBinding" bindingConfiguration="HttpsBinding"
contract="MyNamespace.IMyService"/>
</service>
</services>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您将拥有HTTP端点:
http://yourservername/VirtualDirectory/YourService.svc/basic
Run Code Online (Sandbox Code Playgroud)
和您的安全HTTPS端点:
https://yourservername/VirtualDirectory/YourService.svc/secure
Run Code Online (Sandbox Code Playgroud)
此外:您的安全端点使用HttpsBinding
配置 - 但您缺少这样的绑定配置 - 您只需:
<bindings>
<basicHttpBinding>
<binding name="HttpBinding">
<security mode="None">
<transport clientCredentialType="None"></transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)
你需要添加HttpsBinding
配置!!
<bindings>
<basicHttpBinding>
<binding name="HttpBinding">
<security mode="None">
<transport clientCredentialType="None"></transport>
</security>
</binding>
<binding name="HttpsBinding">
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)
我最近不得不webHttpBinding
在 Microsoft Azure 应用服务 (IIS) 中的 HTTP 和 HTTPS 上提供WCF 3.5 REST ( ) 服务。这是一次有趣的……而且是痛苦的冒险。这是我的发现和我web.config
的<system.serviceModel>
:
注:这些说明适用于使用WCF REST Web服务上运行*.svc
(@ServiceHost
)最小的ASP.NET 4.7应用程序中的文件(Global.asax
IIS 10中在Windows Server 2016上)这些说明不适用于自托管的WCF服务,非REST WCF 服务(即 SOAP)或早于 .NET Framework 4.7 的平台。这也不适用于 .NET Core。
<serviceHostingEnvironment>
元素是必不可少的,确保multipleSiteBindingsEnabled="true"
设置。
<baseAddressPrefixFilters>
不再需要的。<serviceMetadata>
元素web.config
。
<endpoint address=""
属性中使用绝对 URI - 只需将属性留空。
PATH_INFO
在 IIS 中托管 WCF 时将其保留为空白(或使用相对 URI 表示样式端点):
对于 IIS 托管的服务终结点,您必须始终使用相对终结点地址。如果端点地址未指向承载暴露端点的服务的 IIS 应用程序,则提供完全限定的端点地址(例如,http://localhost/MyService.svc)可能会导致服务部署错误. 使用托管服务的相对端点地址可以避免这些潜在的冲突。
web.config
文件告诉 WCF 在托管应用程序(即 IIS)在其父网站中未启用 HTTPS 时接受 HTTPS 连接,则会引发错误。
<services>
省略元素并在后台自动生成元素来简化 WCF 的配置,但是我没有得到与双 HTTP+HTTPS 绑定的 RESTful 服务一致的结果,所以我仍然<services>
手动指定我的。
这是<system.serviceModel>
我的web.config
文件中的元素:
<system.serviceModel>
<services>
<service name="WcfService1.MainService">
<endpoint address="" binding="webHttpBinding" contract="WcfService1.IMainService" behaviorConfiguration="myWebBehavior" bindingConfiguration="myWebHttpBindingInsecure" />
<endpoint address="" binding="webHttpBinding" contract="WcfService1.IMainService" behaviorConfiguration="myWebBehavior" bindingConfiguration="myWebHttpBindingSecure" />
</service>
<service name="WcfService1.AnotherService">
<endpoint address="" binding="webHttpBinding" contract="WcfService1.IAnotherService" behaviorConfiguration="myWebBehavior" bindingConfiguration="myWebHttpBindingInsecure" />
<endpoint address="" binding="webHttpBinding" contract="WcfService1.IAnotherService" behaviorConfiguration="myWebBehavior" bindingConfiguration="myWebHttpBindingSecure" />
</service>
<!-- etc... -->
</services>
<behaviors>
<endpointBehaviors>
<behavior name="myWebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<!-- By default (in machine.config), the 'http' scheme is mapped to 'basicHttpBinding' (SOAP), not 'webHttpBinding' (REST) and the 'https' scheme is not mapped. -->
<add binding="webHttpBinding" scheme="https" bindingConfiguration="myWebHttpBindingSecure" />
<add binding="webHttpBinding" scheme="http" bindingConfiguration="myWebHttpBindingInsecure" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<bindings>
<webHttpBinding>
<!-- maxReceivedMessageSize="104857600" is 100MiB -->
<binding name="myWebHttpBindingInsecure" maxReceivedMessageSize="104857600" transferMode="Streamed">
<security mode="None" />
</binding>
<binding name="myWebHttpBindingSecure" maxReceivedMessageSize="104857600" transferMode="Streamed">
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
29956 次 |
最近记录: |