SoFunction
Updated on 2025-03-08

WCF configuration experience

According to the blog post of Mr. Jiang Jinnan, the end point of WCF consists of three elements, namely address (Address), binding (Binding) and contract (Contract). The abbreviation can be written as Endpoint = ABC.
Address: The address determines the location of the service and solves the problem of service addressing.
Binding: Binding implements all the details of communication, including network transmission, message encoding, and other corresponding processing of messages to implement certain functions. The types of binding include BasicHttpBinding, WsHttpBinding, NetTcpBinding, etc.
Contract: Contract is an abstraction of service operations and also a definition of message exchange mode and message structure.
The above content is excerpted from Teacher Jiang’s blog post. These understandings are helpful in configuring WCF.
Then let’s configure a WCF step by step.

First of all, the server side,
The core of a WCF is the endpoint, so write and list the endpoint first.

Copy the codeThe code is as follows:

<services>
      <service name="" behaviorConfiguration="te">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9091/logicService"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="ws2007HttpBinding" contract=""  bindingConfiguration="transportWS2007HttpBinding" />
      </service>
    </services>

From the <endpoint> several attributes address (address), binding (binding), Contract, these attributes are exactly the "ABC" mentioned above. Note that the binding is filled in the values ​​of BasicHttpBinding, WsHttpBinding, and NetTcpBinding. To be sure which binding is used, you need to set it in the bindingConfiguration, and the value is the name value of <binding> used. The fully qualified name of the contract interface of contract in the contract project. Here we will introduce the configuration of binding next. The address has not been filled in, and an address has been given here in <host>.
After introducing <endpoint>, take a look at the outside of <endpoint>. <endpoint> is contained in <service> of <services>. Here <serivces> is a collection that can contain multiple services, each service will have a specific name, and name is the fully qualified name of the class that implements the contract in the project. Here we make some settings for servicebehavior, and the specific content is in the <servicebehavior> named te.
Since there are configurations above and involve binding and behavior, the following are configurations of both.
Copy the codeThe code is as follows:

<bindings>
      <ws2007HttpBinding>
        <binding name="transportWS2007HttpBinding" maxReceivedMessageSize="2147483647"  maxBufferPoolSize="2147483647">
          <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
          <security mode="Message">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </ws2007HttpBinding>

        <basicHttpBinding>
          <binding name="newBinding" maxBufferPoolSize="21474835647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
            <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          </binding>
        </basicHttpBinding>
</bindings>


The bindings part, like services, is also a collection that contains various types of bindings. For example, the <binding> in <ws2007HttpBinding> is the exact binding. When using <endpoint>, the name of bindingConfiguration must be written externally, and the type of binding cannot be wrong. The child nodes and properties in <binding> will not be introduced one by one. If you want to transmit larger data through WCF, you must set the properties of the binding and <readerQuotas>.
Copy the codeThe code is as follows:

<behaviors>
      <serviceBehaviors>
        <behavior name="te">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

Finally came to behaviors. Similarly, behaviors are also a collection with two types. One is serviceBehaviors, which is used to configure service; the other is endpointBehaviors, which is used to configure endpoint. Both types are a collection, and the child nodes <behavior> are their children. The name is used to distinguish each behavior. As for the properties and children in it, I won’t say much. When using it, just fill in the behaviorConfiguration property of the corresponding service or endpoint.
The server configuration ends here, and the client is here.
Copy the codeThe code is as follows:

<client>
      <endpoint address="http://localhost:9091/logicService" binding="ws2007HttpBinding"
          bindingConfiguration="WS2007HttpBinding_ILogic" contract=""
          name="WS2007HttpBinding_ILogic">
      </endpoint>
    </client>

First of all, it is the endpoint. The client's endpoint is placed in the client, and there is also "ABC". The address here must be the same as the server configuration, otherwise the corresponding service will not be found. The binding type should also be the same as the server side, and contract is the fully qualified name of the class in the code generated by svcutil or other tools.
Copy the codeThe code is as follows:

<ws2007HttpBinding>
        <binding name="WS2007HttpBinding_ILogic" 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="2147483647"
            messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
            allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="1024" maxArrayLength="2147483647"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
              enabled="false" />
        </binding>
      </ws2007HttpBinding>

Another thing to mention is this binding. The binding on the client is configured with a little more things than the server. CloseTimeout, openTimeout, and receiveTimeout are roughly the same as the server.
In addition, if you want to transmit the comparison big data, you can match it as I do. In fact, this configuration is already suitable for transferring several M pictures. Since I am an introductory person, I don’t understand many things thoroughly enough. If you have any mistakes above, please criticize and point them out. Thanks!