【问题标题】:configuring WCF with <services> tag使用 <services> 标签配置 WCF
【发布时间】:2011-01-19 03:14:08
【问题描述】:

我正在尝试解决 WCF 错误 found in my previous question。基本上,错误是:

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

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

现在,我面临一个不同的问题。我不知道我应该如何在我的 web.config 中配置 services 标记以在我的服务器上正常工作。当我尝试使用 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>

请注意,删除 services 标记后一切正常,但我将无法解决我在previous question 上发布的原始问题。

如果我在 web.config 上做错了,特别是在我的 services 标签中,有人可以告诉我吗?!

【问题讨论】:

  • 嗯,问题真的是:IS这真的是您在此处配置的服务吗?我的意思是服务的服务器端,服务代码存在和执行的地方??

标签: wcf web-config


【解决方案1】:

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

首先,你需要定义一个自定义的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>

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

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

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

检查要点:

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

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

<client>
  <endpoint name="Default"
            address="http://localhost:53931/WCF/Service1.svc"
            binding="basicHttpBinding"
            bindingConfiguration="LargeSettings"
            contract="ServiceReference.IYourService" />
</client>

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

【讨论】:

  • 您已经回答了我关于服务标签的具体问题。但是,我仍然收到:读取 XML 数据时已超出最大字符串内容长度配额 (8192)。 :)
【解决方案2】:

对于那些使用内置服务参考的人,只需使用.Endpoint.Binding =THE NEW BINDING

例如:

BasicHttpBinding b = new BasicHttpBinding();
b.Security.Mode = BasicHttpSecurityMode.Transport;
...
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;               

MyWebServiceReference.ServiceReferenceSoapClient objRE = new MyWebServiceReference.ServiceReferenceSoapClient("ServiceReferenceSoap", "URI");
objRE.Endpoint.Binding = b;

【讨论】:

    【解决方案3】:

    将此设置用于您的绑定,

     <basicHttpBinding>
            <binding maxReceivedMessageSize="2147483647"  messageEncoding="Text" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" >
              <readerQuotas  maxStringContentLength="525288"></readerQuotas>
            </binding>
          </basicHttpBinding>
    

    【讨论】:

    • 您通过在互联网场景中使用这些绑定将您的服务暴露于 DOS 攻击。
    • 您在这里的基本意思是允许传入的请求最大为 2 GB!想象一下,如果黑客向您的服务发送 10 个 2 GB 的请求会发生什么。 WCF 将尝试在内存中分配 2GB 缓冲区来存储每个请求,并且可能会失败。在多次失败后,您的 AppPool 可能会被终止,您的服务将不再可用......
    猜你喜欢
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    相关资源
    最近更新 更多