How can I set the maxItemsInObjectGraph property programmatically from a Silverlight Application?
Asked Answered
B

3

8

I have a Silverlight 3.0 application that is using a WCF service to communicate with the database, and when I have large amounts of data being returned from the service methods I get Service Not Found errors. I am fairly confident that the solution to it is to simply update the maxItemsInObjectGraph property, but I am creating the service client progrogrammatically and cannot find where to set this property. Here is what I am doing right now:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None)
{
    MaxReceivedMessageSize = int.MaxValue,                  
    MaxBufferSize = int.MaxValue
};                        

MyService.MyServiceServiceClient client = new MyService.MyServiceProxyServiceClient(binding, new EndpointAddress(new Uri(Application.Current.Host.Source, "../MyService.svc")));
Boehmer answered 18/3, 2010 at 21:59 Comment(0)
I
28

It's not defined in binding, but in Service Behavior.

In Silveright, maxItemsInObjectGraph defaults to int.MaxValue.

Here is an article on how to change it for .NET application, but not Silverlight: Programattically setting the MaxItemsInObjectGraph property in client

A snippet of the code:

protected ISecurityAdministrationService GetSecAdminClient()
{
     ChannelFactory<ISecurityAdministrationService> factory = new    ChannelFactory<ISecurityAdministrationService>(wsSecAdminBinding, SecAdminEndpointAddress);
     foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
     {
       DataContractSerializerOperationBehavior dataContractBehavior =op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;
       if (dataContractBehavior != null)
       {
             dataContractBehavior.MaxItemsInObjectGraph = 2147483647;
       }
     }
    ISecurityAdministrationService client = factory.CreateChannel();
    return client;
}
I answered 18/3, 2010 at 23:15 Comment(5)
I've looked at that, but for some reason the OperationDescription does not have a Behaviors property.Boehmer
I see. I guess because the client is a Silverilght application, the System.ServiceModel.dll is different than the .NET one. But DataContractSerializer maxItemsInObjectGraph on Silverlight defaults to int.MaxValue. Are you going to set it to a smaller value in your code?I
No. Maybe this was a misunderstanding on my part, but I thought that it defaulted to substantially less then int.MaxValue. I have had aspx pages that used WCF services and had a similar problem, and I was able to modify the maxItemsInObjectGraph in the web.config to be 2147483646 and it fixed the problem.Boehmer
Turns out you were right, there was no need to do this since in silverlight it does default to int.maxValueBoehmer
Thanks!!!. I don't think you need "as DataContractSerializerOperationBehavior" though, Find<> already returns the casted type.Astrakhan
C
3

The following is a function that I've used inside a client object that inherits from

System.ServiceModel.ClientBase(Of IServiceName)

The purpose of the method is to programatically set the MaxItemsInObjectGraph value for each operation. This allows me to have much more complex structures.

    Private Sub IncreaseObjectCount()
        For Each op As System.ServiceModel.Description.OperationDescription In Me.Endpoint.Contract.Operations
            For Each dscob As System.ServiceModel.Description.DataContractSerializerOperationBehavior In op.Behaviors.FindAll(Of System.ServiceModel.Description.DataContractSerializerOperationBehavior)()
                dcsob.MaxItemsInObjectGraph = Integer.MaxValue
            Next dcsob
        Next op
    End Sub

I usually call it in the constructors of the object.

Credible answered 26/10, 2011 at 3:23 Comment(0)
A
1

Change the maxItemsInObjectGraph in your WCF service for each endpoint, changing it in Silverlight means the client will be able to support the behavior, but the service must support it aswell.

After changing it in your service, regenerate the proxy/update web service, and you will get a new ServiceReference.config, that will include the new maxItemsInObjectGraph value

Ambary answered 19/3, 2010 at 17:57 Comment(1)
The service is already updated. The problem is all of those settings that get put into the ServiceReference.config get ignored when I do I setup the client in code.Boehmer

© 2022 - 2024 — McMap. All rights reserved.