【问题标题】:How to? WCF customBinding over Https如何? WCF customBinding over Https
【发布时间】:2011-07-17 00:37:35
【问题描述】:

我正在尝试在我们面向外部的网络场上设置一个供内部使用的 WCF 服务(我们内部没有网络场,我需要此服务来进行故障转移和负载平衡)。

要求:

  • PerSession 状态,因为我们需要服务为每个会话保留可变数据。
  • HTTPS。经过大量谷歌搜索后,我阅读了我需要创建一个 customBinding,我已经完成了,但不确定它是否正确。
  • 消息更大,因为参数之一是 byte[] 数组,最大可以为 5mb。
  • 无需手动编辑客户端 app.config。即,我需要开发人员只添加服务引用,然后开始使用该对象,而无需繁琐地更改 app.config。

注意:我之前已经让这个服务在 HTTP 下正常工作(使用 wsHttpBinding)。 我也让它在 HTTPS 下工作,但它不支持 PerSession 状态,并且每次函数调用都会丢失内部变量值。

我目前从测试工具中收到此错误:

在 ServiceModel 客户端配置部分中找不到引用合同“AppMonitor.IAppMonitorWcfService”的默认端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。

注意:错误是在测试工具 EXE 上出现的,它在服务引用下直接引用了 WCF 服务。这不是 exe 引用另一个对象的问题,然后引用了我读过的 WCF 服务。

浏览到 URL 时 WSDL 正确显示。

Web.Config:

  <system.serviceModel>
    <services>
      <service name="AppMonitor.AppMonitorWcfService" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="customBinding" bindingConfiguration="EnablePerSessionUnderHttps" contract="AppMonitor.IAppMonitorWcfService"/>
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>

    <bindings>
      <customBinding>
        <binding name="EnablePerSessionUnderHttps" maxReceivedMessageSize="5242880">
          <reliableSession ordered="true"/>
          <textMessageEncoding>
            <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          </textMessageEncoding>
          <httpsTransport authenticationScheme="Anonymous" requireClientCertificate="false"/>
        </binding>
      </customBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

EXE 的 App.config(添加服务引用时自动生成):

<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="CustomBinding_IAppMonitorWcfService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="true" />
                    <security mode="Transport">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client />
    </system.serviceModel>
</configuration>

我不确定 app.config 为什么显示 wsHttpBinding?这不应该是customBinding吗? 我真的不想编辑 app.config,因为该服务将被数十名开发人员使用,我希望他们能够添加服务参考,然后他们就走了......

使用 VS2008、.NET 3.51。 我认为服务器是IIS7,Win Server 2008,如果需要可以确认。

【问题讨论】:

    标签: wcf binding https


    【解决方案1】:

    好的,你的问题是你没有为你的服务定义一个地址。记住端点的ABC:Address-Binding-Contract。

    您需要为服务定义一个地址,该地址几乎由您的 .svc 文件管理,并且可以从 IIS 浏览该文件。无论该 .svc 文件的 URL 是什么,您都可以附加您定义的地址(在您的情况下为空字符串),然后将其添加到顶部。所以如果我把 myservice.svc 文件放在根目录下,它就变成了

    https://localhost/myservice.svc/&lt;ServiceAddress&gt;

    在你的情况下ServiceAddress="" 所以 URL 是https://localhost/myservice.svc/

    请参阅here 了解更多信息。

    【讨论】:

    • web.config中有一个地址,所以我只假设您的意思是客户端app.config中缺少该地址?为什么它不见了?我所做的只是在“添加服务引用”选项中输入相同的 URL(我可以浏览并查看 WSDL 页面),然后它就找到了该服务。然后我输入了一个 NameSpace,然后单击 GO,就是这样。这就是我过去完成 WCF 的方式。任何想法为什么它没有将地址放在 app.config 中?
    • 好的,很明显的问题是客户端app.config在标签下没有任何信息。我知道需要知道 a) 为什么在执行“添加服务参考”时没有自动填充; b)如何让它自动填充? c)或者,更改 web.config 绑定或其他内容,以便它像之前一样自动填充。 必须让使用我们服务的开发者不必去编辑他们的 app.configs 来使用这个服务。他们应该能够在添加服务参考中输入 URL,然后开始使用该服务。感谢您的帮助!
    • 注意:我读到我必须从这篇文章中创建一个 customBinding:stackoverflow.com/questions/4396961/…。我还读到我遇到的端点详细信息未填充到客户端 app.config 的问题是由于绑定问题(但未提供解决方案)。我可以使用 wsHttpBinding 代替(或其他?),并且仍然有 PerSession 上下文,通过 https?
    猜你喜欢
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 2021-08-31
    • 2012-05-01
    相关资源
    最近更新 更多