积极答复者
配置问题

问题
-
想用代码在客户端创建代理, 提示只能添加一次. 请问该怎么解决?
代码:
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
this.Description.Behaviors.Add(smb);ServiceDebugBehavior sdb = new ServiceDebugBehavior();
sdb.IncludeExceptionDetailInFaults = true;
this.Description.Behaviors.Add(sdb);出错信息:
The value could not be added to the collection, as the collection already contains an item of the same type: 'System.ServiceModel.Description.ServiceDebugBehavior'. This collection only supports one instance of each type.
Parameter name: item
答案
-
其实WCF默认已经添加了这些服务行为,我们需要更新现有的配置就可以,加一个反而出错。
这个是我跟踪查找到的4个已有的服务行为,
+ [0] {System.ServiceModel.ServiceBehaviorAttribute} System.ServiceModel.Description.IServiceBehavior {System.ServiceModel.ServiceBehaviorAttribute}
+ [1] {Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior} System.ServiceModel.Description.IServiceBehavior {Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior}
+ [2] {System.ServiceModel.Description.ServiceAuthorizationBehavior} System.ServiceModel.Description.IServiceBehavior {System.ServiceModel.Description.ServiceAuthorizationBehavior}
+ [3] {System.ServiceModel.Description.ServiceDebugBehavior} System.ServiceModel.Description.IServiceBehavior {System.ServiceModel.Description.ServiceDebugBehavior}这样就可以了
ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb);ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.IncludeExceptionDetailInFaults = true;
host.Description.Behaviors.Add(sdb);
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
欢迎访问老徐的中文技术博客:Welcome to My Chinese Technical Blog
欢迎访问微软WCF中文技术论坛:Welcome to Microsoft Chinese WCF Forum
欢迎访问微软WCF英文技术论坛:Welcome to Microsoft English WCF Forum- 已建议为答案 Frank Xu LeiModerator 2010年4月26日 12:13
- 已标记为答案 fss199 2010年4月27日 11:10
全部回复
-
应该是已经包含了一个ServiceDebugBehavior ,你代码做个判断,把ServiceDebugBehavior 从服务行文里取取出来进行操作。
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
欢迎访问老徐的中文技术博客:Welcome to My Chinese Technical Blog
欢迎访问微软WCF中文技术论坛:Welcome to Microsoft Chinese WCF Forum
欢迎访问微软WCF英文技术论坛:Welcome to Microsoft English WCF Forum -
其实WCF默认已经添加了这些服务行为,我们需要更新现有的配置就可以,加一个反而出错。
这个是我跟踪查找到的4个已有的服务行为,
+ [0] {System.ServiceModel.ServiceBehaviorAttribute} System.ServiceModel.Description.IServiceBehavior {System.ServiceModel.ServiceBehaviorAttribute}
+ [1] {Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior} System.ServiceModel.Description.IServiceBehavior {Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior}
+ [2] {System.ServiceModel.Description.ServiceAuthorizationBehavior} System.ServiceModel.Description.IServiceBehavior {System.ServiceModel.Description.ServiceAuthorizationBehavior}
+ [3] {System.ServiceModel.Description.ServiceDebugBehavior} System.ServiceModel.Description.IServiceBehavior {System.ServiceModel.Description.ServiceDebugBehavior}这样就可以了
ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb);ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.IncludeExceptionDetailInFaults = true;
host.Description.Behaviors.Add(sdb);
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
欢迎访问老徐的中文技术博客:Welcome to My Chinese Technical Blog
欢迎访问微软WCF中文技术论坛:Welcome to Microsoft Chinese WCF Forum
欢迎访问微软WCF英文技术论坛:Welcome to Microsoft English WCF Forum- 已建议为答案 Frank Xu LeiModerator 2010年4月26日 12:13
- 已标记为答案 fss199 2010年4月27日 11:10
-
public PollingDuplexServiceHost(params Uri[] addresses)
: base(typeof(Servico), addresses)
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
this.Description.Behaviors.Add(smb);
}刚刚测试,以上代码可以正常运行,以下代码是根据您提供而改写的,提示信息:“远程服务器返回了错误: NotFound。”
public PollingDuplexServiceHost(params Uri[] addresses)
: base(typeof(Servico), addresses)
{
ServiceMetadataBehavior smb = this.Description.Behaviors.Find<ServiceMetadataBehavior>();
smb.HttpGetEnabled = true;
this.Description.Behaviors.Add(smb);
}