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

假如想象 提交于 2019-11-29 06:55:25

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;
}

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!