How to debug a StackOverflowException with WCF

How to debug a StackOverflowException with WCF

Old forum URL: forums.lhotka.net/forums/t/4845.aspx


SWo2008 posted on Wednesday, May 14, 2008

Hi

I'm trying to setup the WCF DataPortal for our app. I've tried out our app with remoting and web services and it seems fine, so I'm following the instructions in the v3 PDF booklet.

Here's an exert from the app.config

  <appSettings>
    <add key="CslaAuthentication" value="Windows"/>
    <add key="CslaDataPortalProxy"
         value="Csla.DataPortalClient.WcfProxy, Csla"/> 
  </appSettings>

  <system.serviceModel>
    <client>
      <endpoint name="WcfDataPortal"
      address="http://localhost/WcfHost/WcfPortal.svc"
      binding="wsHttpBinding"
      contract="Csla.Server.Hosts.IWcfPortal" />
    </client>
  </system.serviceModel>

and the web.config

 <appSettings>
    <add key="CslaAuthentication" value="Windows"/>
  </appSettings>
 
  <connectionStrings>
    <add name="InfoFlex"
         connectionString=...
  </connectionStrings>

  <system.serviceModel>
    <services>
      <!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
      <service name="Csla.Server.Hosts.WcfPortal" behaviorConfiguration="returnFaults">
        <endpoint contract="Csla.Server.Hosts.IWcfPortal" binding="wsHttpBinding"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="returnFaults">
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

    </behaviors>

(IE returns the expected WCF page when invoked)

When I step the code, it reaches the WcfProxy.Fetch and then jumps to invoke the NetDataContractOperation.CreateSerializer instruction and then goes off for a while before returning with the stack overflow error.

I'm new to WCF so am struggling to know where to look to advance my understanding of the problem. I can only assume that there's a problem with Serialization but, I thought  my settings in my config file would use the normal binary serializer rather than the NetDataContractSerializer. (But I really have very limited understanding here).

Many thanks in advance for any suggestions

Simon

ajj3085 replied on Wednesday, May 14, 2008

AFAIK, you can't, unless you build an application to host the CLR and only then can you catch a StackOverflowException.

In my experience with Csla, this is usually due to a property getter or setter trying to get or set the property, instead of the field.

For example:
public string MyProperty {
   get { return MyProperty; // this will cause it }
   set{
         // some code
        MyProperty = value; // as will this
        // other code
    }
}

That would be my first place to check.  Other places would be rule methods or if you have any recursive code make sure it's written properly.

Good luck.
andy

SWo2008 replied on Thursday, May 15, 2008

Thanks very much Andy.

Just a couple of points.

a) The thing is that I can switch to use the remoting or web services data portal and the thing works fine. I would have thought that if there were properties/rules calling themselves that I would have got a similar stack overflow error when these properties/rules etc were serialized or at least invoked.

b) I'm not even sure why (and this is no doubt due to my lack of understanding) a NetDataContractSerializer is being instantiated. In Rocky's instructions on setting up WCF he says

"You must configure CSLA .NET to use NDCS rather than the BinaryFormatter. For reasons of
backward compatibility, the default is to use the BinaryFormatter.
...
To configure CSLA .NET to use the NDCS you must add an element to the appSettings of your
app.config or web.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
    <add key="CslaAuthentication" value="Csla" />
    <add key="CslaSerializationFormatter" value="NetDataContractSerializer"/>
Now as you can see from my app.config, I haven't included the reference to the CSLASerializationFormatter being the NetDataContractSerializer, so am unsure why one is being newed up unless it is now wrapping the old BinarySerializer. I thought I'd be using the default which is working for remotng/webservices.

There are a couple of other things I should perhaps add

1) I am using v3.5 of everything (well not quite everything but dotnet and csla)
2) The project tracker obviously works (one difference is that we're using Windows authentication rather than CSLA atm - but obviously that's okay when using Remoting etc)
3) I tried forcing the serialization by adding
    <add key="CslaSerializationFormatter" value="BinaryFormatter"/>
   to my web/app.config ... but it didn't make any difference.

Thanks again

Simon


ajj3085 replied on Thursday, May 15, 2008

Hi,

I'm not sure then, if it's working with remoting.  I think the reason the NetData serializer is being used is because you're using Wcf for your data portal, which will use the NDCS to serialize your objects. 

So it could be you need some attributes somewhere to tell NDCS not to serialize a certain field; do you store any parent references in your BOs?

SWo2008 replied on Thursday, May 15, 2008

Again, thanks for your time, Andy.

From reading what Rocky wrote in the CSLA v3 document, CSLA(WCF?) defaults to the 'old' binaryformatter by default with WCF. I even tried forcing this in case something wasn't working right by adding

    <add key="CslaSerializationFormatter" value="BinaryFormatter"/>

to my web/app.config (I tried both). When I stepped the code, I stepped to the same line where the NDCS is newed up, so I have to conclude that either the NDCS wraps the old binaryformatter and delegates to it internally or else something is not working quite right and I'm being forced to use the NDCS (against my wishes at the moment!), and my BOs are certainly not setup up with DataContract attribute.

Wrt my BOs having parent references, they may do, but the serialization works with remoting/webservices so again I have tended to exclude this as being the problem. If it serializes okay with remoting then I can't see why it shouldn't with WCF ... except that you may well be right in that there is more configuration I need to do, which I don't know about.

Perhaps a way forward is to change my BOs to use the DataContract and see if that works.

Thanks very much though, and any further thoughts would be greatly appreciated.

Simon


 

ajj3085 replied on Thursday, May 15, 2008

I meant try to use the CslaSerializationFormatter so that it WILL use the NDCS.  I think Wcf requires the NDCS, so if you don't want to use it I think you'll have to not use Wcf.

SWo2008 replied on Thursday, May 15, 2008

thx andy

I've progressed it a bit more. I was using my own test harness and not VS unit testing. When I run it through a unit test, I'm getting a "the underlying connection was closed" error, which wasn't getting gen'ed or was being hidden when I was stepping with my own test harness.

I thought there may be a couple of possibilities

a) Something to do with
http://forums.lhotka.net/forums/thread/14905.aspx
http://www.lhotka.net/weblog/WCFNetDataContractSerializerAndSecurityException.aspx

Though as we're using Windows authentication, atm, I'm not sure what the consequences of this are in relation to logout(). It also may explain why the project tracker wcf portal works fine and our's doesn't.

b) I've read the for large object graphs, wcf can timeout. I assume the there's an attribute I can set for this.

Perhaps you can suggest any others for this error.

thx again

S

Copyright (c) Marimer LLC