gpt4 book ai didi

web-config - 如何将 http 和 https 的 WCF 服务配置合并到一个 web.config 中?

转载 作者:行者123 更新时间:2023-12-03 10:29:00 25 4
gpt4 key购买 nike

我花了很多时间弄清楚如何配置我的 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>

添加 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>

这里的区别是我设置了 httpsGetEnabled属性为 false,我删除了 bindingConfiguration 和 bindingNamespace。

问题是: 如何创建一个同时处理两者的配置 block ?

我真的很讨厌每次发布时都必须对配置进行大量特殊修改。是的,我知道我可以有一个自动更改值的构建后任务,但如果可能的话,我想合并配置。

我试过这样的事情:
<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>

我认为放置两个端点会给它在激活服务时寻找两个选项。但是,这不起作用。我收到此错误:

找不到与绑定(bind) BasicHttpBinding 的终结点的方案 https 匹配的基地址。注册的基地址方案是 [http]。

从 SO 和互联网的其他地方看,似乎其他人在杀死这条龙时遇到了问题。

最佳答案

我最近不得不在 Microsoft Azure 应用服务 (IIS) 中的 HTTP 和 HTTPS 上提供 WCF 3.5 REST (webHttpBinding) 服务。这是一个有趣的......和痛苦的冒险。这是我的发现和我的web.config<system.serviceModel> :

注:这些注释适用于使用 *.svc 运行的 WCF REST Web 服务。 ( @ServiceHost ) 在 Windows Server 2016 上的 IIS 10 中的最小 ASP.NET 4.7 应用程序(带有 Global.asax )中的文件。这些注释 不要适用于自承载 WCF 服务、非 REST WCF 服务(即 SOAP)或早于 .NET Framework 4.7 的平台。这也不适用于 .NET Core。

  • 升级到 .NET Framework 4.7.2 或更高版本(这只是常识)
  • 最重要的部分正在使用 <serviceHostingEnvironment>元素是必不可少的,请确保 multipleSiteBindingsEnabled="true"已设置。
  • 这记录在这里:
  • https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/supporting-multiple-iis-site-bindings
  • https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/servicehostingenvironment
  • 看起来这是在 .NET Framework 4.0 ( https://blogs.msdn.microsoft.com/saurabs/2012/11/21/wcf-handling-multiple-iis-bindings/ ) 中添加的,在 .NET 3.5 中您需要使用 <baseAddressPrefixFilters>不再需要这些了。
  • 不要需要<serviceMetadata>您的 web.config 中的元素文件。
  • 显然这个元素是针对 WSDL 元数据的——而 RESTful 服务不支持 WSDL。
  • 我指出这一点是因为至少有一些关于文章和 StackOverflow 帖子的热门 Google 搜索结果,人们说这是必需的。那些人是不正确的。
  • 不要在您的 <endpoint address="" 中使用绝对 URI属性 - 只需将属性留空。
  • 这仅在 WCF 3.5 中是必需的。
  • Microsoft 表示在 IIS 中托管 WCF 时将其留空(或为 PATH_INFO 样式的端点使用相对 URI):
  • 阅读这篇文章:https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/deploying-an-internet-information-services-hosted-wcf-service

    You must always use relative endpoint addresses for IIS-hosted service endpoints. Supplying a fully-qualified endpoint address (for example, http://localhost/MyService.svc) can lead to errors in the deployment of the service if the endpoint address does not point to the IIS-application that hosts the service exposing the endpoint. Using relative endpoint addresses for hosted services avoids these potential conflicts.

  • WCF 将检测是否可以使用 HTTP 和 HTTPS 访问它,如果 web.config 将引发错误。当宿主应用程序(即 IIS)没有在其父网站中启用 HTTPS 时,文件告诉 WCF 接受 HTTPS 连接。
  • 这出乎我的意料,因为我使用过的所有其他 Web 应用程序平台都不会提示它们是否配置为 HTTPS,但父 Web 服务器没有。
  • 特别是考虑到在这种情况下,WCF 在 .NET 请求管道中/在其内部运行,并且 ASP.NET 当然不关心 IIS 的网站绑定(bind)(因为 ASP.NET 设计为与父网站绑定(bind)无关)
  • 注意:“网站绑定(bind)”是指 IIS 网站绑定(bind),不是 “WCF 绑定(bind)”是一个单独的概念。
  • 要使其在 Visual Studio 和 IIS Express 中本地工作,请确保父 Web 应用程序项目在“属性”窗口中具有“SSL Enabled = True”(与“项目属性”窗口不同(是的,我也尖叫了) ):
  • enter image description here
  • 在 .NET 4.6.1 中,Microsoft 通过允许 <services> 简化了 WCF 的配置。元素被省略并在幕后自动生成,但是我没有与具有双 HTTP+HTTPS 绑定(bind)的 RESTful 服务的结果一致,所以我仍然指定我的 <services>手动。
  • 这记录在这里:https://docs.microsoft.com/en-us/dotnet/framework/wcf/simplified-configuration

  • TL;博士:

    这是我的 <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>

    关于web-config - 如何将 http 和 https 的 WCF 服务配置合并到一个 web.config 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4346926/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com