tags:

views:

101

answers:

2

I have a custom behavior for a service where I want to specify a receive timeout value, I have created a behavior and on the build service header.

I use the declarative attribute to apply the behavior or as I thought. But the behavior seems to make no difference, i.e. the set timeout value is not being applied as expected.

The same behavior when applied explicitly through does work. Any ideas?

Behavior:

[AttributeUsage(AttributeTargets.Class)]
public class BuildServiceBindingBehavior : Attribute, IServiceBehavior
{
    public BuildServiceBindingBehavior( string p_receiveTime )
    {
        ReceiveTimeout = TimeSpan.Parse( p_receiveTime );
    }

    #region IServiceBehavior Members

    public void AddBindingParameters( ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters )
    {
    }

    public void ApplyDispatchBehavior( ServiceDescription serviceDescription, ServiceHostBase serviceHostBase )
    {
        // add this behavior to each endpoint
        foreach ( var endPoint in serviceDescription.Endpoints )
        {
            endPoint.Binding.ReceiveTimeout = ReceiveTimeout;
        }
    }

    public void Validate( ServiceDescription serviceDescription, ServiceHostBase serviceHostBase )
    {
    }

    #endregion

    internal TimeSpan ReceiveTimeout { get; set; }
}

Service code:

[ServiceBehavior(Name = "DotNetBuildsService",
                 InstanceContextMode = InstanceContextMode.PerSession,
                 ConcurrencyMode = ConcurrencyMode.Single
                 )]
// Set receieve time out
[BuildServiceBindingBehavior( "0:0:1" )]
public class BuildService : IBuildTasksService
{
  //implementation code
}
A: 

The problem is that your behavior runs too late. By that point, the binding properties have already been applied to the channel listener, so modifying the binding isn't going to have any effect there.

I don't think you can (or at least, I don't remember seeing anywhere you could) change the receive timeout dynamically like this, since most channel listeners are closed in that respect (i.e. you can't change their settings once they are opened).

Why do you need to change the ReceiveTimeout like this?

tomasr
Withing the company, we have a shared assembly that creates the service host etc. So we have to use this, so the only way of extending the timeout properly I feel is using behavior.However I have managed to get it working using declrative attributes. I had to move the code in ApplyDispatchBehavior() to AddBindingParameters().
Rubans
A: 

Move the code in ApplyDispatchBehavior() to AddBindingParameters().

Rubans