Up Configuration and deployment code samples
DevForce 2010 Resource Center » Code samples » Configuration and deployment code samples » Code sample: Configure EntityServer communications with WCF

Code sample: Configure EntityServer communications with WCF

Last modified on August 15, 2012 17:22

You can take control of the configuration of WCF communications in your EntityServer by directly working with the WCF <system.serviceModel> configuration section.


Problem

The <system.serviceModel> section is defined in the server's config file. When the EntityServer is hosted by IIS this will be the web.config, otherwise it will be either ServerService.exe.config or ServerConsole.exe.config depending on the host.

When you define the <system.serviceModel> in your configuration file, you're telling DevForce that you're taking control over the configuration of the WCF services comprising the EntityServer.

You can use the WCF Service Configuration Editor available from the Visual Studio Tools menu to edit the <system.serviceModel> information.

Solution

We can't possibly explain all the nearly endless complexities of WCF configuration, but here we'll show you a few samples.

The standard configuration

DevForce will automatically determine the WCF configuration based on your environment.  The sample below is what the standard configuration would look like if instead defined in config.

Here we define two services, "EntityService" and "EntityServer", and each has two endpoints - an endpoint for non-Silverlight client applications, and another for Silverlight clients.  The endpoints configured are based on the supportedClientApplicationType value on the <serverSettings>.  Here we're assuming an Enterprise license.  All endpoints use compressed binary formatted messages over http.  

XML
<configuration>

  <system.serviceModel>

    <services>

      <service name="EntityService">
       
       <!--- Endpoint for non-Silverlight clients -->
        <endpoint address=""
                  binding="customBinding" bindingConfiguration="compressedBinaryBinding"
                  contract="IdeaBlade.EntityModel.IEntityServiceContract" />
       
       <!--- Endpoint for Silverlight clients -->
        <endpoint address="sl"
                  binding="customBinding" bindingConfiguration="compressedBinaryBinding"
                  contract="IdeaBlade.EntityModel.IEntityServiceContract" />
      </service>

      <service name="EntityServer">

       <!--- Endpoint for non-Silverlight clients -->
        <endpoint address=""
                  binding="customBinding" bindingConfiguration="compressedBinaryBinding"
                  contract="IdeaBlade.EntityModel.IEntityServerContract" />

       <!--- Endpoint for Silverlight clients -->
        <endpoint address="sl"
                  binding="customBinding" bindingConfiguration="compressedBinaryBinding"
                  contract="IdeaBlade.EntityModel.IEntityServerContract" />
      </service>
     
    </services>

    <bindings>
     
      <customBinding>
       
        <binding name="compressedBinaryBinding">
          <gzipMessageEncoding>
            <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" />
          </gzipMessageEncoding>
          <httpTransport maxReceivedMessageSize="2147483647" />
        </binding>

      </customBinding>
     
    </bindings>

    <extensions>

      <bindingElementExtensions>
        <add name="gzipMessageEncoding" type="IdeaBlade.Core.Wcf.Extensions.GZipMessageEncodingElement, IdeaBlade.Core"/>
      </bindingElementExtensions>

    </extensions>

  </system.serviceModel>
</configuration>

Example

Use Windows credentials when not hosted in IIS

Below is a sample binding adding Windows authentication and message security to the standard DevForce binding.  This is not supported for Silverlight clients.

XML
     <customBinding>

        <binding name="wsBindingCustom">
          <gzipMessageEncoding>
            <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" />
          </gzipMessageEncoding>
          <security authenticationMode="SspiNegotiated">
          </security>
          <httpTransport maxReceivedMessageSize="2147483647" />
        </binding>
       
      </customBinding>

Configure TCP bindings

This sample shows several different binding possibilities.  You can define any number of binding definitions in your configuration, but it's the binding and bindingConfiguration attributes on the endpoint which determine the binding used.  In this sample the "tcpBindingDefault" binding is used.  If you try this on your own and wish to use another binding, be sure that you change the binding and bindingConfiguration on both endpoints.

This sample also shows that you can use either standard or custom WCF bindings.  DevForce uses a CustomBinding, but this is not a requirement for your application.  The standard bindings are better documented by Microsoft and therefore usually easier to use. Standard bindings do not offer compression.

Note that the addresses used here do not use the *.svc extension because the services are not hosted in IIS.  You can use net.tcp with an IIS-hosted EntityServer if you install and configure the Windows Process Activation Service.  net.tcp is the name for the TCP scheme in WCF. 

You can use net.tcp with Silverlight clients, but we don't yet have a sample.

XML
<configuration>
  <system.serviceModel>

    <services>
      <service name="EntityService">
        <endpoint
          address=""
          binding="netTcpBinding" bindingConfiguration="tcpBindingDefault"
          contract="IdeaBlade.EntityModel.IEntityServiceContract" />
      </service>
      <service name="EntityServer">
        <endpoint
          address=""
          binding="netTcpBinding" bindingConfiguration="tcpBindingDefault"
          contract="IdeaBlade.EntityModel.IEntityServerContract" />
      </service>
    </services>

    <bindings>
      <netTcpBinding>

     <!-- Standard tcp binding - uses Windows auth and transport security by default. -->
        <binding name="tcpBindingDefault" maxReceivedMessageSize="2147483647" />

       <!-- Tcp binding using message security. -->
        <binding name="tcpBindingMessage" maxReceivedMessageSize="2147483647">
          <security mode="Message" />
        </binding>

      </netTcpBinding>

      <customBinding>
       
       <!-- DevForce custom binding with TCP and compression.  -->
        <binding name="tcpBindingCustom">
          <gzipMessageEncoding>
            <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" />
          </gzipMessageEncoding>
          <tcpTransport maxReceivedMessageSize="2147483647" />
        </binding>

       <!-- Another custom binding, with TCP, compression and transport security.  -->
        <binding name="tcpBindingCustomSecure">
          <gzipMessageEncoding>
            <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" />
          </gzipMessageEncoding>
          <windowsStreamSecurity />
          <tcpTransport maxReceivedMessageSize="2147483647" />
        </binding>

      </customBinding>

    </bindings>

    <extensions>
      <bindingElementExtensions>
        <add name="gzipMessageEncoding" type="IdeaBlade.Core.Wcf.Extensions.GZipMessageEncodingElement, IdeaBlade.Core"/>
      </bindingElementExtensions>
    </extensions>

  </system.serviceModel>
</configuration>

Additional resources

Securing Access to Services for Silverlight
Custom Bindings

Created by DevForce on March 08, 2011 18:02

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade