2011-01-27 24 views
9

私はその構成ファイルを使用せずにプログラムでmaxItemsInObjectGraphを追加する方法はありますか?

EndpointAddress address = new EndpointAddress("http://example.com/services/OrderService.svc"); 

ようEndpointAddressを作成しているしかし、私はプログラム的にこのエンドポイントに行動を追加できませんでした。あなたは、それをエンドポイントに適用する必要があり、クライアント上で

[ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)] 

行動はあなたがServiceBehavior属性でそれを追加する必要がサーバー上:

<behaviors> 
    <endpointBehaviors> 
    <behavior name="NewBehavior"> 
     <dataContractSerializer maxItemsInObjectGraph="6553600" /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 

答えて

28

以下の通りです。この例では、あなたのChannelFactory内のすべてのエンドポイントに追加する方法を見ることができます。サーバー側の

var factory = new ChannelFactory<IInterface>(...); 
foreach (OperationDescription op in factory.Endpoint.Contract.Operations) 
    { 
     var dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>(); 
     if (dataContractBehavior != null) 
     { 
      dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue; 
     } 
    } 
+1

はそれが完璧に働いた、感謝を –

+0

ニース - 昨日それを使用しました。ありがとう – Liam

+0

しかし、サイクル参照を保持するために、たとえば独自のDataOntrolSerializerOperationBehaviorを実装する場合は、コンストラクタのMaxItemsInObjectGraphをDataContractSerializerに指定する必要があります。 –

2

、あなたも行うことができます

ServiceHost host = new ServiceHost(); 
ServiceBehaviorAttribute sba = host .Description.Behaviors.Find<ServiceBehaviorAttribute>(); 
      if (sba == null) 
      { 
       sba = new ServiceBehaviorAttribute(); 
       sba.MaxItemsInObjectGraph = int.MaxValue; 
       host.Description.Behaviors.Add(sba); 
} 
関連する問題