WCF Facility: The value could not be added to the collection when adding Service or Endpoint behaviors

803 views
Skip to first unread message

Ruprict

unread,
Jul 23, 2008, 1:22:31 PM7/23/08
to Castle Project Development List
When using Binsor and adding an EndpointBehavior as a component, I
receive the following error (in build 901)

The value could not be added to the collection, as the collection
already contains an item of the same type:

Looking through the facility code, it seems that an IServiceBehavior
(actually adds ServiceDebugBehavior?) and an IEndpointBehavior is
already added to the container by the facility. So, adding another of
either of these in the Binsor config causes the above error.

In build 901, the facility code actually looks for IServiceBehaviors,
and removes the ServiceDebugBehavior if one is in the config. It does
not, however, do this for IEndpointBehavior.

I also received the following question on one of my blog posts, so I
am putting it here for the group to address (I am actually on vacation
in the NC mountains...) as it is the same issue.

I am trying to configure a WCF service using Binsor and the latest
trunk of Castle and Rhino.Commons.

I am using the following snippet to register service:
component ’svc’, IMyService, MyServiceImpl:
ServiceModel =
DefaultServiceModel().Hosted().AddEndpoints(WcfEndpoint.BoundTo(BasicHttpBinding()))

component
‘metadata_behavior’,IServiceBehavior,ServiceMetadataBehavior:
HttpGetEnabled = false

When accessing the service, I receive an error message:
The value could not be added to the collection, as the collection
already contains an item of the same type:
‘System.ServiceModel.Description.ServiceMetadataBehavior’. This
collection only supports one instance of each type.
Parameter name: item

How can I get rid of this error?

Any help appreciated.
Thanks,
Ruprict

Craig Neuwirt

unread,
Jul 23, 2008, 2:54:44 PM7/23/08
to castle-pro...@googlegroups.com
Which behavior is already present in your scenario?

Ruprict

unread,
Jul 23, 2008, 9:33:33 PM7/23/08
to Castle Project Development List
Well, this particular scenario is a bit contrived, but I can reproduce
the error with this Binsor config:

import System;
import System.Reflection
import System.ServiceModel
import System.ServiceModel.Description from System.ServiceModel
import Castle.Facilities.WcfIntegration
import WCFBinsor.Services

facility WcfFacility

component 'test.svc', IContract, ServiceImpl:
ServiceModel = DefaultServiceModel().Hosted() \
.AddEndpoints(WcfEndpoint.BoundTo(BasicHttpBinding()))

component 'metadata.behavior',IServiceBehavior,
ServiceMetadataBehavior:
HttpGetEnabled = true

component 'auth.behavior',IServiceBehavior,
ServiceAuthorizationBehavior:
ImpersonateCallerForAllOperations = true


So, I guess the real question is: Is there a scenario that would
require >1 out-of-the-box IServiceBehavior, and if so, are we forced
to write a custom behavior to handle that?

Add to that question the sub-question of does this also affect
IEndpointBehavior(s).....I seem to remember that we were doing
something with endpoint behaviors and had a similar issue, but can't
remember what we did about it (Again, I am in the NC mountains,
sneaking time on the PC while my wife thinks I am "resting")

Does that make sense?




On Jul 23, 2:54 pm, "Craig Neuwirt" <cneuw...@gmail.com> wrote:
> Which behavior is already present in your scenario?
>

João Bragança

unread,
Jul 23, 2008, 10:16:30 PM7/23/08
to Castle Project Development List
Try registering the behaviors without the interface and with the scope
attribute:

component 'behaviors.serviceDebug', ServiceDebugBehavior:
@scope = 'services'

component 'behaviors.serviceAuthorization',
ServiceAuthorizaionBehavior:
@scope = 'services'
ImpersonateCallerForAllOperations = true


Unless you need to call Kernel.ResolveAll<IServiceBehavior>() you
should be fine. That's how I do it and I don't have this problem.
> > > DefaultServiceModel().Hosted().AddEndpoints(WcfEndpoint.BoundTo(BasicHttpBi­nding()))
>
> > > component
> > > 'metadata_behavior',IServiceBehavior,ServiceMetadataBehavior:
> > > HttpGetEnabled = false
>
> > > When accessing the service, I receive an error message:
> > > The value could not be added to the collection, as the collection
> > > already contains an item of the same type:
> > > 'System.ServiceModel.Description.ServiceMetadataBehavior'. This
> > > collection only supports one instance of each type.
> > > Parameter name: item
>
> > > How can I get rid of this error?
>
> > > Any help appreciated.
> > > Thanks,
> > > Ruprict- Hide quoted text -
>
> - Show quoted text -

Craig Neuwirt

unread,
Jul 24, 2008, 8:03:30 AM7/24/08
to castle-pro...@googlegroups.com
On Wed, Jul 23, 2008 at 8:33 PM, Ruprict <glenn.g...@gmail.com> wrote:

Well, this particular scenario is a bit contrived, but I can reproduce
the error with this Binsor config:

import System;
import System.Reflection
import System.ServiceModel
import System.ServiceModel.Description from System.ServiceModel
import Castle.Facilities.WcfIntegration
import WCFBinsor.Services

facility WcfFacility

component 'test.svc', IContract, ServiceImpl:
       ServiceModel = DefaultServiceModel().Hosted() \
       .AddEndpoints(WcfEndpoint.BoundTo(BasicHttpBinding()))

component 'metadata.behavior',IServiceBehavior,
ServiceMetadataBehavior:
       HttpGetEnabled = true

component 'auth.behavior',IServiceBehavior,
ServiceAuthorizationBehavior:
       ImpersonateCallerForAllOperations = true


So, I guess the real question is: Is there a scenario that would
require >1 out-of-the-box IServiceBehavior, and if so, are we forced
to write a custom behavior to handle that?

The debug behavior is added automatically if not present.  All other
behaviors must be explicitly added.  I am not sure I understanding this question


Add to that question the sub-question of does this also affect
IEndpointBehavior(s).....I seem to remember that we were doing
something with endpoint behaviors and had a similar issue, but can't
remember what we did about it (Again, I am in the NC mountains,
sneaking time on the PC while my wife thinks I am "resting")

Does that make sense?

If the metadata behavior is coming from the web.config or app.config, you
would get that error which is expected. 
 

Sorry for any confusion, but I am not sure I see where the problem is.
In your above example, is the metadata behavior complaining about
already being added?

Ruprict

unread,
Jul 28, 2008, 1:38:16 PM7/28/08
to Castle Project Development List
So, I can't reproduce the IEndpointBehavior error, so disregard that
bit. (ahem)

However, I still might want to apply ServiceMetadataBehavior,
ServiceDebugBehavior, and ServiceAuthorizationBehavior on my services,
the Boo for which looks like:

import System;
import System.Reflection
import System.ServiceModel
import System.ServiceModel.Description from System.ServiceModel
import Castle.Facilities.WcfIntegration
import WCFBinsor.Services

facility WcfFacility

component 'test.svc', IContract, ServiceImpl:
ServiceModel = DefaultServiceModel().Hosted() \
.AddEndpoints(WcfEndpoint.BoundTo(BasicHttpBinding()))

component 'metadata.behavior',IServiceBehavior,
ServiceMetadataBehavior:
HttpGetEnabled = true

component 'debug.behavior',IServiceBehavior, ServiceDebugBehavior:
IncludeExceptionDetailInFaults = true

component 'auth.behavior', IServiceBehavior,
ServiceAuthorizationBehavior:
ImpersonateCallerForAllOperations = true

This fails with:

The value could not be added to the collection, as the collection
already contains an item of the same type:
'System.ServiceModel.Description.ServiceAuthorizationBehavior'. This
collection only supports one instance of each type.
Parameter name: item

Even MORE odd is, if you take away the auth.behavior, it works.....

Does that help? Again, I apologize for how cryptic this thread has
been and again will blame my vacation. Anyone else can feel free to
blame my intelligence......

Thanks,
Glenn


On Jul 24, 8:03 am, "Craig Neuwirt" <cneuw...@gmail.com> wrote:

Craig Neuwirt

unread,
Jul 28, 2008, 1:57:45 PM7/28/08
to castle-pro...@googlegroups.com
ok, I'll try and pin down this issue and let you know.
Reply all
Reply to author
Forward
0 new messages