如何? WCF customBinding over Https

时间:2011-03-16 23:31:56

标签: wcf binding https

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

要求:

  • 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,可以确认是否需要。

1 个答案:

答案 0 :(得分:0)

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

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

https://localhost/myservice.svc/<ServiceAddress>

在您的情况下ServiceAddress="",因此网址为https://localhost/myservice.svc/

有关详细信息,请参阅here