开发者

configuring WCF with <services> tag

I am trying to solve a WCF error found in my previous question. Basically, the error is:

The maximum string content length quota (8192) has been exceeded while reading XML data.

And someone suggested to use a services tag in my web.config to resolve my issue.

Now, I am facing a different problem. I can’t figure out how am I suppose to configure the services tag in my web.config to work correctly on my server. I always get the following error when I try to use the services tag:

The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.

Here is my web.config with the services tag added:

<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" />
      <开发者_Go百科;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>

Please note that by removing the services tag everything works fine, but then I will not be able to resolve my original problem posted on my previous question.

so could someone please tell me if I am doing something wrong on my web.config, specifically in my services tag?!


Okay, let's tackle this:

First, you need to define a custom basicHttpBinding binding configuration with some custom settings:

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

This section needs to be in both your server-side's web.config, as well as your client side's config.

Secondly, on the server-side, you need to have a <services> tag that defines your service and its endpoints and their configuration:

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

Points to check:

  • your service name must be the fully qualified name (YourNamespace.YourClassName) of your service class - the class that implements your service contract
  • your service contract in the endpoint must also be the fully qualified name of your service contract (YourNamespace.IYourServiceContract)
  • the behaviorConfiguration of your <service> tag must reference and match exactly to the name= attribute as defined in your <behaviors> section

And thirdly, on the client side, you need something like this:

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

You need to reference the endpoint defined in your service's definition on the server side, you need to use the same binding and binding configuration, and you need to use the service contract as defined in your service reference.


For those using the built in service reference, just use the .Endpoint.Binding =THE NEW BINDING

ex:

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;


Use this setting for the your bindings,

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜