使用<services>标记配置WCF

Eya*_*yad 7 wcf web-config

我正在尝试解决上一个问题中发现的WCF错误.基本上,错误是:

读取XML数据时已超出最大字符串内容长度配额(8192).

有人建议在我的web.config中使用服务标签来解决我的问题.

现在,我面临着一个不同的问题.我无法弄清楚我如何配置web.config中的服务标签以在我的服务器上正常工作.当我尝试使用services标记时,我总是收到以下错误:

服务器没有提供有意义的回复; 这可能是由于合同不匹配,过早的会话关闭或内部服务器错误造成的.

这是我添加了services标签的web.config :

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding
      name="BasicHttpBinding_Service1"
      closeTimeout="00:01:00"
      openTimeout="00:01:00"
      receiveTimeout="00:10:00"
      sendTimeout="00:01:00"
      allowCookies="false"
      bypassProxyOnLocal="false"
      hostNameComparisonMode="StrongWildcard"
      maxBufferSize="65536"
      maxBufferPoolSize="524288"
      maxReceivedMessageSize="65536"
      messageEncoding="Text"
      textEncoding="utf-8"
      transferMode="Buffered"
      useDefaultWebProxy="true">
      <readerQuotas
        maxDepth="32"
        maxStringContentLength="10000"
        maxArrayLength="16384"
        maxBytesPerRead="4096"
        maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint
    address="http://localhost:53931/WCF/Service1.svc"
    binding="basicHttpBinding"
    bindingConfiguration="BasicHttpBinding_Service1"
    contract="ServiceReference.Service1"
    name="BasicHttpBinding_Service1" />
</client>
<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<!--PROBLEM SOMEWHERE IN THE SERVICES TAG-->
<services>
  <service
    behaviorConfiguration="NewBehavior"
    name="AspPersonalWebsite.ServiceReference">
    <endpoint
      address="http://localhost:53931/WCF/Service1.svc"
      binding="basicHttpBinding"
      contract="ServiceReference.Service1"
      bindingConfiguration="BasicHttpBinding_Service1" />
  </service>
</services>
Run Code Online (Sandbox Code Playgroud)

请注意,通过删除服务标签一切正常,但我将无法解决我在上一个问题上发布的原始问题.

那么有人可以告诉我,如果我在web.config上做错了什么,特别是在我的服务标签中?!

mar*_*c_s 26

好的,我们来解决这个问题:

首先,您需要basicHttpBinding使用一些自定义设置定义自定义绑定配置:

<bindings>
  <basicHttpBinding>
    <binding name="LargeSettings"
             maxBufferSize="524288"
             maxBufferPoolSize="524288"
             maxReceivedMessageSize="6553600">
        <readerQuotas maxDepth="32" maxStringContentLength="100000"
                      maxArrayLength="16384" maxBytesPerRead="4096"
                      maxNameTableCharCount="16384" />
      <security mode="None" />
    </binding>
  </basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

此部分需要位于服务器端的web.config以及客户端的配置中.

其次,在服务器端,您需要有一个<services>标记来定义您的服务及其端点及其配置:

<services>
   <service name="YourNamespace.YourClassName"
            behaviorConfiguration="ServiceWithMetadata">
      <endpoint name="Default"
                address="http://localhost:53931/WCF/Service1.svc"
                binding="basicHttpBinding"
                bindingConfiguration="LargeSettings"
                contract="YourNamespace.IServiceContract" />
   </service>
</services>
<behaviors>
   <serviceBehaviors>
      <behavior name="ServiceWithMetadata">
         <serviceMetadata httpGetEnabled="true" />
         <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
   </serviceBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

要点检查:

  • 您的服务名称必须是YourNamespace.YourClassName服务类的完全限定名称() - 实现服务合同的类
  • 您在端点中的服务合同也必须是服务合同的完全限定名称(YourNamespace.IYourServiceContract)
  • 您的<service>标记的behaviorConfiguration 必须引用并完全匹配name=您的<behaviors>部分中定义的属性

第三,在客户端,你需要的东西是这样的:

<client>
  <endpoint name="Default"
            address="http://localhost:53931/WCF/Service1.svc"
            binding="basicHttpBinding"
            bindingConfiguration="LargeSettings"
            contract="ServiceReference.IYourService" />
</client>
Run Code Online (Sandbox Code Playgroud)

您需要在服务器端引用服务定义中定义的端点,您需要使用相同的绑定和绑定配置,并且需要使用服务引用中定义的服务合同.