2009-12-07 7 views
7

Đối với bộ công cụ sử dụng dịch vụ WCF từ xa, tôi đã định cấu hình ChannelFactory<IMyService> trong UnityContainer.Cấu hình MaxItemsInObjectGraph của máy khách WCF khi sử dụng Unity

Bây giờ tôi muốn cấu hình hành vi đầu cuối của kênh này thông qua mã (sử dụng Unity) để áp dụng hành vi này:

<behaviors> 
    <endpointBehaviors> 
     <behavior name="BigGraph"> 
      <dataContractSerializer maxItemsInObjectGraph="1000000" /> 
     </behavior> 
     </endpointBehaviors> 
</behaviors> 

tôi thấy ví dụ này trên MSDN (http://msdn.microsoft.com/en-us/library/ms732038.aspx)

ChannelFactory<IDataService> factory = new ChannelFactory<IDataService>(binding, address); 
foreach (OperationDescription op in factory.Endpoint.Contract.Operations) 
{ 
    vardataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior; 
    if (dataContractBehavior != null) 
    { 
     dataContractBehavior.MaxItemsInObjectGraph = 100000; 
    } 
} 
IDataService client = factory.CreateChannel(); 

nhưng bây giờ tôi đang cố gắng làm điều này trong cấu hình Unity. Tôi có nên nhìn vào Interception không?

+0

Hiện tại tôi chỉ xây dựng nhà máy, áp dụng hành vi và thêm hành vi đó làm ví dụ cho vùng chứa. – veertien

Trả lời

1

Chúng tôi đang sử dụng phần mở rộng chính sách xây dựng để thống nhất thêm hành vi trên máy chủ dịch vụ. Trên máy khách, chúng tôi có ServiceFactory.

/// <summary> 
/// Factory for creating application service proxies used on the workstation 
/// </summary> 
/// <typeparam name="TInterface">Interface for the service contract</typeparam> 
public class ServiceFactory<TInterface> where TInterface : class 
{ 
    private readonly List<IEndpointBehavior> m_Behaviors = new List<IEndpointBehavior>(); 

    /// <summary> 
    /// Add a behavior that is added to the proxy endpoint when the channel is created. 
    /// </summary> 
    /// <param name="behavior">An <see cref="IEndpointBehavior"/> that should be added</param>. 
    public void AddBehavior(IEndpointBehavior behavior) 
    { 
     m_Behaviors.Add(behavior); 
    } 

    /// <summary> 
    /// Creates a channel of type <see cref="CommunicationObjectInterceptor{TInterface}"/> given the endpoint address which 
    /// will recreate its "inner channel" if it becomes in a faulted state. 
    /// </summary> 
    /// <param name="url">The endpoint address for the given channel to connect to</param>. 
    public TInterface CreateChannel(string url) 
    { 
     // create the channel using channelfactory adding the behaviors in m_Behaviors 
    } 
} 

Sau đó chúng ta cấu hình thống nhất với một InjectionFactory

new InjectionFactory(c => 
      { 
       var factory = new ServiceFactory<TInterface>(); 
       factory.AddBehavior(c.Resolve<IClientTokenBehavior>()); 
       return factory.CreateChannel(url); 
      }); 

Bằng cách làm nó theo cách này bạn cũng có thể giải quyết hành vi của bạn thông qua sự hợp nhất nếu bạn có một số phụ thuộc.

0

Tôi nghĩ bạn nên thêm một mức độ gián tiếp nữa, do đó bạn không cần phải gây rối với việc chặn hoặc một thứ gì đó tương tự. Vấn đề này có thể dễ dàng được giải quyết bằng cách tạo một lớp mới để bọc lên kênh WCF. Ví dụ:

public class MyServiceClient : IMyService 
{ 
    public MyServiceClient(IChannelFactory<IMyService> channel) 
    { 
    } 

    public void DoSomething() //DoSomething is the implementation of IMyService 
    { 
    //Initialize the behavior in the channel 
    //Calls channel.DoSomething 
    } 
}