代码之家  ›  专栏  ›  技术社区  ›  Maxime Rouiller

ChannelFactory在端点上没有地址,为什么?

  •  3
  • Maxime Rouiller  · 技术社区  · 16 年前

    创建ChannelFactory的新实例时:

    var factory = new ChannelFactory<IMyService>();
    

    当我创建一个新通道时,我有一个异常,端点的地址为空。

    我在web.config中的配置如前所述,所有内容都与预期的一样(特别是端点的地址)。

    var factoryWithClientBase = new MyServiceClientBase().ChannelFactory;
    Console.WriteLine(factoryWithClientBase.Endpoint.Address); //output the configuration inside the web.config
    
    
    var factoryWithChannelFactory = new ChannelFactory<IMyService>();
    Console.WriteLine(factoryWithChannelFactory.Endpoint.Address); //output nothing (null)
    

    为什么?

    3 回复  |  直到 16 年前
        1
  •  9
  •   AnAngel    16 年前

    如果您在Web.Config中为终结点提供这样的名称,它会起作用吗:

    <endpoint address="http://localhost:2000/MyService/" binding="wsHttpBinding"
        behaviorConfiguration="wsHttpBehaviour" contract="IService"
        name="MyWsHttpEndpoint" />
    

    并使用ChannelFactory创建通道,如下所示:

    var factory = new ChannelFactory<IMyService>("MyWsHttpEndpoint");
    
        2
  •  5
  •   Maxime Rouiller    16 年前

    我终于找到了答案。ChannelFactory不从Web.Config/app.Config读取信息。ClientBase(然后与ChannelFactory一起工作)确实如此。

    所有这些花费了我几个小时的工作,但我终于找到了一个合理的理由。

    :)

        3
  •  3
  •   suparthawijaya    15 年前

    新操作员实例化的ChannelFactory不从web.config读取。上面的Angel回答是正确的。 您只需确保为端点提供名称。 并确保端点契约设置正确(非常重要)

    <endpoint address="http://localhost:2000/MyService/" binding="wsHttpBinding"
    behaviorConfiguration="wsHttpBehaviour" contract="TheCorrectNamespace.IService"
    name="MyWsHttpEndpoint" />
    

    询问者没有发布他的web.config,因此我认为您很可能犯了我犯的错误(为端点指定了错误的契约)