For a toolkit that uses a remote WCF service, I have configured a ChannelFactory<IMyService>
in a UnityContainer.
Now I want to configure this channel's endpoint behavior through code (using Unity) to apply this behavior:
<behaviors>
<endpointBehaviors>
<behavior name="BigGraph">
<dataContractSerializer maxItemsInObjectGraph="1000000" />
</behavior>
</endpointBehaviors>
</behaviors>
I found this example on 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();
but now I am stuck trying to do this in a Unity configuration. Should I look into Interception?
We are using a build policy extension in unity to add behaviors on the service host. On the client we have a 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
}
}
Then we configure unity with an InjectionFactory
new InjectionFactory(c =>
{
var factory = new ServiceFactory<TInterface>();
factory.AddBehavior(c.Resolve<IClientTokenBehavior>());
return factory.CreateChannel(url);
});
By doing it this way you can also resolve your behavior through unity if you have some dependencies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With