wcf - 为什么我的 ChannelFactory 看不到我的端点配置?

标签 wcf

我一直在关注 Miguel Castro 关于 WCF 的优秀文章 here一切都运行良好,除了我有以下代码

public AdminClient()
{
    ChannelFactory<IProductAdmin> factory = new ChannelFactory<IProductAdmin>();
    productAdminChannel = factory.CreateChannel();
}

在我的 app.config 文件中,我有以下配置:

<system.serviceModel>
    <client>
        <endpoint address="net.tcp://localhost:8002/ProductBrowser"
                  binding="netTcpBinding"
                  contract="Contracts.IProductAdmin" />
    </client>
</system.serviceModel>

但是,当我运行 AdminClient 的构造函数时,我收到一个异常,指出端点未定义。但是,如果我更改配置以给端点命名,然后按如下方式创建工厂,则它可以工作。

public AdminClient()
{
    var fac = new ChannelFactory<IProductAdmin>("admin");
    productAdminChannel = fac.CreateChannel();
}
<小时/>
<system.serviceModel>
    <client>
        <endpoint name="admin" 
                  address="net.tcp://localhost:8002/ProductBrowser"
                  binding="netTcpBinding"
                  contract="Contracts.IProductAdmin" />
    </client>
</system.serviceModel>

我希望对此有一个解释。 MSDN 中的文档没有太大帮助...

最佳答案

使用“*”使用第一个合格端点。

public AdminClient()
{
    ChannelFactory<IProductAdmin> factory  
         = new ChannelFactory<IProductAdmin>("*");

    productAdminChannel = factory.CreateChannel();
}

MSDN Example

关于wcf - 为什么我的 ChannelFactory 看不到我的端点配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/426041/

相关文章:

c# - wcf rest ierrorhandler webfaultexception

c# - 在 C# 中显示引用地址(调试 WCF)

c# - 使 DataContract 中的 DataMember 使用隐式转换为字符串运算符

ajax - 'JSON' 仅在 IE 中是未定义的错误

.net - 简单的轻量级服务层

wcf - 如何将 WCF 服务作为 x64 运行

wcf - [Silverlight] WCF 客户端的编程配置

wcf - 为什么 Tcp 比 http 快?

wcf - 长轮询 Windows Phone,60 秒超时

c# - 无法序列化类型 'MyDTO' - Wcf 服务