【问题标题】:Do WCF endpoints need to be defined in multiple locations?WCF 端点是否需要在多个位置定义?
【发布时间】:2013-12-13 06:21:38
【问题描述】:

我有一个带有 app.config 的 VS2012 解决方案。我在解决方案中有多个项目。让我们这样定义它:
Root.csproj >> 启动项目
->Orders.csproj
->Customers.csproj
->Purchasing.csproj

现在,当我向 Orders 项目添加服务引用时,它会将绑定和端点信息粘贴到 Orders.csproj 中的 app.config 中。但是,当我运行应用程序时,我得到了预期的异常:

异常:找不到引用的默认端点元素 ServiceModel 客户端中的合同“OrderService.IOrderService” 配置部分。这可能是因为没有配置文件 为您的应用程序找到,或者因为没有匹配的端点元素 该合同可以在客户端元素中找到。

这是因为 Root 中的 app.config 不包含绑定/端点配置。我可以轻松地将绑定和端点从 orders\app.config 复制到 root\app.config 并成功执行服务方法。我的问题是为什么我必须这样做?有没有办法告诉 root\app.config 从 orders\app.config 收集其他端点?如果服务方法需要成功执行,那么添加服务引用并不会自动将绑定和端点添加到 root\app.config 似乎很愚蠢。任何建议都非常感谢。谢谢。

【问题讨论】:

    标签: wcf app-config


    【解决方案1】:

    有服务器端“端点”…………

    但是在客户端........你必须添加一些指向服务器端端点的条目。

    要回答您的问题(否),您必须在客户端定义信息。 它基本上是在说“在这里可以找到您要使用的服务”

    它们看起来非常相似。

    服务器端:

    <endpoint
          address  = "http://localhost:8001/MammalControllerFascade"
          binding  = "wsHttpBinding" bindingConfiguration="WSHttpBindingName1"
          contract = "GranadaCoder.Applications.TrinugWCFDemoVersion1.Interfaces.Controllers.IMammalController" >
    </endpoint>
    
    <endpoint
          address  = "net.pipe://localhost/LocalMammalControllerFascade"
          binding  = "netNamedPipeBinding" bindingConfiguration="NamedPipeBindingName1"
          contract = "GranadaCoder.Applications.TrinugWCFDemoVersion1.Interfaces.Controllers.IMammalController" >
    </endpoint>
    

    客户端

    <client>
    
    
        <endpoint name="WSHttpEndPoint"
                 address  = "http://localhost:8001/MammalControllerFascade"
                 binding  = "wsHttpBinding"  bindingConfiguration="WSHttpBindingName1"
                 contract = "GranadaCoder.Applications.TrinugWCFDemoVersion1.Interfaces.Controllers.IMammalController"  >
    
    
        </endpoint >
    
    
        <endpoint name="NamedPipeEndPoint"
             address  = "net.pipe://localhost/LocalMammalControllerFascade"
             binding  = "netNamedPipeBinding"  bindingConfiguration="NamedPipedBindingName1"
             contract = "GranadaCoder.Applications.TrinugWCFDemoVersion1.Interfaces.Controllers.IMammalController"  >
        </endpoint >
    
    
    </client>
    

    注意,它们是相似的。请注意,在客户端,ENDPOINT 有一个名称。 这样客户端就可以按名称引用 ENDPOINT。

    编辑:

    您是否同时运行主机和客户端?

    您不能只在调试模式下运行客户端。必须启动该服务。

    右键单击解决方案,属性.......并设置2个启动项目。 (如果您是自托管)

    如果您不是自托管.....那么您如何“提供服务器”? (什么在运行服务器端代码)在调试期间?

    编辑:

    我将我的“部分信息”存储在不同的文件中。所以我可以维护它们,而不会在同一个 app.config(或 web.config)中出现乱码。

    这是一个示例:

    APP.CONFIG(或 web.config)

    <system.serviceModel>
    
        <behaviors configSource="WCFBehaviors.config">
        </behaviors>
    
        <bindings configSource="WCFBindings.config">
        </bindings>
    
        <client configSource="WCFClient.config">
        </client>
    
        <services configSource="WCFServices.config">
        </services>
    
    </system.serviceModel>
    

    我将展示上面的文件之一。

    “WCFClient.config”的内容见下文。其他文件遵循相同的模式。

    <client>
    
        <endpoint name="WSHttpEndPoint"
                 address  = "http://localhost:8001/MammalControllerFascade"
                 binding  = "wsHttpBinding"  bindingConfiguration="WSHttpBindingName1"
                 contract = "GranadaCoder.Applications.TrinugWCFDemoVersion1.Interfaces.Controllers.IMammalController"  >
    
    
        </endpoint >
    
        <endpoint name="NamedPipeEndPoint"
             address  = "net.pipe://localhost/LocalMammalControllerFascade"
             binding  = "netNamedPipeBinding"  bindingConfiguration="NamedPipedBindingName1"
             contract = "GranadaCoder.Applications.TrinugWCFDemoVersion1.Interfaces.Controllers.IMammalController"  >
        </endpoint >
    
    </client>
    

    【讨论】:

    • 谢谢。我已经发布了我的服务,它们被托管在 IIS7 中。我之前的整个描述是指其中包含多个项目的客户端根解决方案。我的主要问题是为什么客户端端点配置仅自动添加到 Order.csproj\app.config 如果 Root.sln\app.config 要求服务方法成功执行而没有找不到端点错误?
    • 据我所知,.sln 文件没有 app.config。
    • 所以我这样做的方式是............我创建了一个 Console.App。我把我的 CLIENT .config 信息保存在那里。如果我有其他需要信息的表示层,我会使用构建后事件将文件复制到其他 csproj。我将编辑我的答案以展示我是如何做到这一点的。
    • edit 不是我如何将 wcf 设置分离到单独的文件中的。恕我直言,它使维护更容易。唯一的缺点是您必须手动将文件复制到输出目录(app.config 会自动复制到该目录)
    • .sln 注释为真。假设 Root 是一个 csproj 并且有一个 app.config 并且是启动项目。它的 app.config 中必须有端点,但 orders\app.config 不需要它们?
    猜你喜欢
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    相关资源
    最近更新 更多