2

I'm trying to get data from a WCF Service and display it in a website.
The communication looks like this:
Website --> WCF service --> CRM Server --> WCF Service --> Website

Now my problem is, sometimes there are larger data to get, around 5k rows. (and my Host PC runs out of Ram) I want to stream 1-10 rows to the website then the next and so on.

My ServiceContract looks like this:

public interface ICommunicationService
{    
    [OperationContract]
    IEnumerable<Row> GetCrmData(string view);

}

And my implementation:

public IEnumerable<Row> GetCrmData(string view)
{
    var data = new DataFromCrm(view);
    return data.GetRows(MetaInformation);
}

the GetRows method looks exactly like this: http://msdn.microsoft.com/en-us/library/gg327917.aspx except in the foreach I'm filling the Row class and yield returning the result. (paging and cooking is disabled atm).

foreach (var c in returnCollection.Entities)
{
    var row = new Row();
    row.RecordId = c.Attributes[ID].ToString();
    foreach (var info in metaInfo)
    {
        row.Cells.Add(c.Attributes[info.AttributeName]);
    }
    yield return row;
}

1. Is the yield return used right?

Bindings
WCF Service:

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<services>
  <service behaviorConfiguration="ServiceBehavior" 
           name="ComToCrmService.CommunicationService">
    <endpoint binding="basicHttpBinding" 
              bindingNamespace="http://localhost:9006/CommunicationService.svc" 
              contract="ComToCrmContracts.ICommunicationService" />
  </service>
</services>

WCF Client

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ICommunicationService" 
                 closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" 
                 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
                 maxBufferSize="65536" 
                 maxReceivedMessageSize="4294967294" 
                 messageEncoding="Text" 
                 textEncoding="utf-8" 
                 transferMode="Streamed" 
                 useDefaultWebProxy="true">
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint binding="basicHttpBinding" 
                bindingConfiguration="BasicHttpBinding_ICommunicationService" 
                contract="ComToCrmReference.ICommunicationService" 
                name="BasicHttpBinding_ICommunicationService"
                address="http://dev11.meta10.com:9007/WCFTestService/CommunicationService.svc" />
    </client>
  </system.serviceModel>

2. Are the bindings correct?

3. Is there a failure in my thinking? remember I'm trying to get 1-10 rows out of 5000 display them on the website, get the next 1-10 rows and so on.
there are only data no binary data or something similar.

4. is it even possible with just one request?

1 Answer 1

6

First and foremost, remember that WCF lives in the paradigm of Service Orientation - not Object Orientation. Many mis-understandings surrounding WCF lie in it's ability to wrap up the service call and present it in an Object Orientated manner.

The fact that you are able to return a IEnumerable<Row> from your service is an example of this. We are fooled in to thinking that we are programming to an abstraction, when in actual fact WCF has to serialize the result of your service call across the wire (very much a concrete implementation), which is then cast to the the interface on the client.

Consequently, you can't perform the "lazy" evaluation of the sequence that you require - and yes - you will be pulling the whole results set across the wire even if you only want the first 10.

WCF does support streaming (http://msdn.microsoft.com/en-us/library/ms733742.aspx) but a simpler way may be to add parameters to your service contract to indicate the page information you require:

public interface ICommunicationService
{
    [OperationContract]
    Row[] GetCrmData(string view, int pageNumber, int pageSize);
}

This contract is far more in keeping with a stateless service orientated application, respecting the notion that you should not be passing large amounts of data around your system, but only using what you require.

Note that I have replace the IEnumerable<Row> with an array - removing the impression that you are programming to an interface over the wire.

2
  • if I'm right, your suggest to load only 10 rows (if i just show 10 rows at the time) and then load more pages when site is loaded?
    – domiSchenk
    Aug 28, 2013 at 19:35
  • In its simplest form yes, although you could be clever about it and eagerly load the first 5 pages say on start up, or some other caching algorithm tailored to the usage patterns of your application.
    – Lawrence
    Aug 28, 2013 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.