gpt4 book ai didi

IIS 托管和 SSL 的 WCF web.config 文件设置

转载 作者:行者123 更新时间:2023-12-04 20:22:51 25 4
gpt4 key购买 nike

经过几个小时的示例搜索后,其中大部分只包含方法片段而不是“全貌”,我正在寻求指导。从开箱即用的 web.config Visual Studio 使用新的 WCF 服务创建开始,我编写了我的基本 Web 服务。当您在调试中运行时,WCF 测试客户端会显示您可以测试的函数。这很棒。现在,想要将代码移动到 IIS(首先在我的本地计算机上,然后在使用 SSL 的 Web 服务器旁边),我添加了一些我在 Web 上找到的代码。我确实让我的配置在某一时刻工作,但设法对其进行了太多更改,以至于我丢失了原始配置。所以,我有这个:

    <system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>

<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>

<services>
<service name="TaskTrackerAppService.Service1" behaviorConfiguration="">
<endpoint address=""
binding="webHttpBinding"
contract="TaskTrackerAppService.IAppWebService"
behaviorConfiguration="WebBehavior"></endpoint>

<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" bindingConfiguration=""></endpoint>
</service>
</services>

<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>

<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>

<bindings>
<basicHttpBinding>
<binding name="TaskTrackerAppService.IAppWebService"></binding>
</basicHttpBinding>
</bindings>

<client>
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="TaskTrackerAppService.IAppWebService"
contract="TaskTrackerAppService.IAppWebService"></endpoint>
</client>


<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>

<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>

我将客户端桌面应用程序服务引用配置为指向本地 IP http:192.168.0.100:90/AppWebService.svc。然后,当我运行我的客户端应用程序时,出现错误:

Could not find default endpoint element that references contract 'ServiceReference.IAppWebService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

所以我想更正 web.config 设置。然后部署到 SSL 已准备就绪的托管 IIS 服务。作为奖励,有没有办法配置端点,这样我仍然可以运行调试器并获取 WCF 测试客户端。在曾经工作的配置中,WCF 测试停止工作。它可以同时支持简单配置和托管配置吗?

谢谢。

最佳答案

<client> <system.serviceModel> 部分由客户端应用程序用来指定 "ABC" properties服务端点的(地址、绑定(bind)和契约(Contract))。您的桌面应用程序中应该有该部分,以便您可以简单地将其从服务器配置中删除。

<client>但是,桌面应用程序的 app.config 部分应该具有与服务端点相同的“ABC”属性。由于您的服务绑定(bind)是 webHttpBinding客户端还应该有 webHttpBinding作为绑定(bind),但我可以看到它指的是 bindingConfiguration,TaskTrackerAppService.IAppWebService实际上是一个 basicHttpBinding所以这是一个错误配置。

此外,由于您的生产环境正在使用 SSL,因此您的生产 web.config 应该具有类似于以下内容的 SSL 绑定(bind)配置:

 <bindings>
<webHttpBinding>
<binding name="webBindingHTTPS">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>

具有以下端点配置:

<endpoint address="" 
binding="webHttpBinding"
contract="TaskTrackerAppService.IAppWebService"
behaviorConfiguration="webBindingHTTPS"></endpoint>

实现此目的的最佳方法是使用 web.config transformation syntax .在这种情况下,您的 Release web.config 可能包含以下元素:

<bindings>
<webHttpBinding>
<binding name="webBindingHTTPS" xdt:Transform="Insert">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>

<endpoint address="" xdt:Transform="Replace" xdt:Locator="Match(name)"
binding="webHttpBinding"
contract="TaskTrackerAppService.IAppWebService"
behaviorConfiguration="webBindingHTTPS">
</endpoint>

这样,只要您的项目是在 Debug 模式下构建的,它将被配置为不使用 SSL,而无论何时在 Release 模式下构建,它将使用 SSL。

关于IIS 托管和 SSL 的 WCF web.config 文件设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49665815/

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