为什么 "no elements matching the key were found in the configuration element collection"?
Why "no elements matching the key were found in the configuration element collection"?
我正在以编程方式创建命名管道 WCF 服务和客户端。
服务代码:
serviceHost = new ServiceHost(typeof(CFCAccessPointService), new Uri(Names.Address));
serviceHost.AddServiceEndpoint(typeof (ICfcAccessPoint), new NetNamedPipeBinding(Names.Binding), Names.Contract);
serviceHost.Open();
客户代码:
var ctx = new InstanceContext(new StatusHandler());
s_proxy = new DuplexChannelFactory<ICfcAccessPoint>(ctx, new NetNamedPipeBinding(Names.Binding), new EndpointAddress(Names.Address));
和
public static class Names
{
public const string Address = "net.pipe://localhost/CFC/Plugins/GuestAccessService";
public const string Binding = "netNamedPipeBinding_ICfcAccessPoint";
public const string Contract = "GuestAccessClientServerInterface.ICfcAccessPoint";
}
确保客户端和服务保持不变。
但是如果我删除 Names.Binding 以便不指定任何绑定配置,我会收到在端点上找不到侦听器的错误。如果我包含它,我会得到 "no elements matching the key were found in the configuration element collection"...
我没有使用 .config 文件。
还缺少什么?
这意味着您的配置文件中没有与该名称的绑定。由于您已经声明您没有使用配置文件,所以这并不奇怪。是否存在无法在 web.config 或 app.config 中配置 WCF 端点的原因?这只是我的意见,但我发现当我需要对服务的各个属性进行随机调整时,配置方法要灵活得多。
无论哪种方式,MSDN docs 用于 NetNamedPipeBinding(string)
构造函数,签名如下所示:
public NetNamedPipeBinding(
string configurationName
)
这意味着使用此构造函数实例化 NetNamedPipeBinding
的唯一方法要求 web.config 或 app.config 文件中存在名称与该字符串匹配的绑定。看起来像这样:
<system.serviceModel>
<bindings>
<netNamedPipeBinding>
<binding name="netNamedPipeBinding_ICfcAccessPoint" />
<netNamedPipeBinding>
</bindings>
</system.serviceModel>
您可能正在寻找看起来更像这样的构造函数:
public NetNamedPipeBinding(
NetNamedPipeSecurityMode securityMode
)
这里是MSDNLink.
使用此构造函数,您的服务主机代码看起来更像这样:
serviceHost = new ServiceHost(typeof(CFCAccessPointService), new Uri(Names.Address));
serviceHost.AddServiceEndpoint(typeof (ICfcAccessPoint), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), Names.Contract);
serviceHost.Open();
您的客户端代码如下:
var ctx = new InstanceContext(new StatusHandler());
s_proxy = new DuplexChannelFactory<ICfcAccessPoint>(ctx, new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), new EndpointAddress(Names.Address));
这应该可以避免对配置文件的需要。
无论哪种方式,绑定都很好,实际上根本不需要参数。
问题出在合同上。当我更改为代码时:
public static class Names
{
public const string Address = "net.pipe://localhost/CFC/Plugins/GuestAccessService/";
public const string Binding = "";
public const string Contract = "CfcAccessPoint";
}
在服务端:
this.serviceHost.AddServiceEndpoint(typeof(ICfcAccessPoint), new NetNamedPipeBinding(), Names.Contract);
在客户端:
var ctx = new InstanceContext(this);
proxy = new DuplexChannelFactory<ICfcAccessPoint>(ctx, new NetNamedPipeBinding(), new EndpointAddress(Names.Address) + Names.Contract);
然后一切正常。该服务只是命名管道;客户端将地址添加到管道名称。
瞧!
我正在以编程方式创建命名管道 WCF 服务和客户端。
服务代码:
serviceHost = new ServiceHost(typeof(CFCAccessPointService), new Uri(Names.Address));
serviceHost.AddServiceEndpoint(typeof (ICfcAccessPoint), new NetNamedPipeBinding(Names.Binding), Names.Contract);
serviceHost.Open();
客户代码:
var ctx = new InstanceContext(new StatusHandler());
s_proxy = new DuplexChannelFactory<ICfcAccessPoint>(ctx, new NetNamedPipeBinding(Names.Binding), new EndpointAddress(Names.Address));
和
public static class Names
{
public const string Address = "net.pipe://localhost/CFC/Plugins/GuestAccessService";
public const string Binding = "netNamedPipeBinding_ICfcAccessPoint";
public const string Contract = "GuestAccessClientServerInterface.ICfcAccessPoint";
}
确保客户端和服务保持不变。
但是如果我删除 Names.Binding 以便不指定任何绑定配置,我会收到在端点上找不到侦听器的错误。如果我包含它,我会得到 "no elements matching the key were found in the configuration element collection"...
我没有使用 .config 文件。
还缺少什么?
这意味着您的配置文件中没有与该名称的绑定。由于您已经声明您没有使用配置文件,所以这并不奇怪。是否存在无法在 web.config 或 app.config 中配置 WCF 端点的原因?这只是我的意见,但我发现当我需要对服务的各个属性进行随机调整时,配置方法要灵活得多。
无论哪种方式,MSDN docs 用于 NetNamedPipeBinding(string)
构造函数,签名如下所示:
public NetNamedPipeBinding(
string configurationName
)
这意味着使用此构造函数实例化 NetNamedPipeBinding
的唯一方法要求 web.config 或 app.config 文件中存在名称与该字符串匹配的绑定。看起来像这样:
<system.serviceModel>
<bindings>
<netNamedPipeBinding>
<binding name="netNamedPipeBinding_ICfcAccessPoint" />
<netNamedPipeBinding>
</bindings>
</system.serviceModel>
您可能正在寻找看起来更像这样的构造函数:
public NetNamedPipeBinding(
NetNamedPipeSecurityMode securityMode
)
这里是MSDNLink.
使用此构造函数,您的服务主机代码看起来更像这样:
serviceHost = new ServiceHost(typeof(CFCAccessPointService), new Uri(Names.Address));
serviceHost.AddServiceEndpoint(typeof (ICfcAccessPoint), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), Names.Contract);
serviceHost.Open();
您的客户端代码如下:
var ctx = new InstanceContext(new StatusHandler());
s_proxy = new DuplexChannelFactory<ICfcAccessPoint>(ctx, new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), new EndpointAddress(Names.Address));
这应该可以避免对配置文件的需要。
无论哪种方式,绑定都很好,实际上根本不需要参数。
问题出在合同上。当我更改为代码时:
public static class Names
{
public const string Address = "net.pipe://localhost/CFC/Plugins/GuestAccessService/";
public const string Binding = "";
public const string Contract = "CfcAccessPoint";
}
在服务端:
this.serviceHost.AddServiceEndpoint(typeof(ICfcAccessPoint), new NetNamedPipeBinding(), Names.Contract);
在客户端:
var ctx = new InstanceContext(this);
proxy = new DuplexChannelFactory<ICfcAccessPoint>(ctx, new NetNamedPipeBinding(), new EndpointAddress(Names.Address) + Names.Contract);
然后一切正常。该服务只是命名管道;客户端将地址添加到管道名称。
瞧!