zoukankan      html  css  js  c++  java
  • WCF配置文件(服务器端)

    WCF 的配置文件并不复杂,相对于手工编写配置文件,我更倾向于使用 SDK 所附带的工具 —— "Service Configuration Editor"。使用这个工具,我们可以非常方便地创建或修改服务器和客户端的配置文件。

    uploads/200704/07_173348_wcfconfigeditor.gif


    WCF 配置文件基本元素:
    <system.ServiceModel>

      <services>
        <service>
          <endpoint/>
        </service>
      </services>

      <bindings>
        <!-- Specify one or more of the system-provided binding elements,
         for example, <basicHttpBinding> -->
        <!-- Alternatively, <customBinding> elements. -->

        <binding>
        <!-- For example, a <BasicHttpBinding> element. -->
        </binding>
      </bindings>

      <behaviors>
        <!-- One or more of the system-provided or custom behavior elements. -->

        <behavior>
        <!-- For example, a <throttling> element. -->
        </behavior>
      </behaviors>

    </system.ServiceModel>

    WCF 的配置信息必须写在 App.config 或 Web.config 中,我觉得像 Remoting 那样可以使用单独的配置文件应该更好一些。现在的项目开发中,各种各样的组件都往 App.config 中写信息,感觉非常凌乱,不便于维护。

    在 编写服务器端配置文件时,记得添加 serviceMetadata Behaviors,否则我们无法使用 Svcutil.exe 或 VS2005 创建客户端代理文件。

    以下配置文件的演示:

    Server App.config
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.serviceModel>
        <services>
          <service behaviorConfiguration="NewBehavior" name="Learn.Library.WCF.MyService">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/MyService" />
              </baseAddresses>
            </host>
            <endpoint address="" binding="basicHttpBinding"
              contract="Learn.Library.WCF.IContract" />
            <endpoint address="mex" binding="mexHttpBinding"
              contract="IMetadataExchange" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="NewBehavior">
              <serviceMetadata httpGetEnabled="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>

    Client App.config
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.serviceModel>
        <client>
          <endpoint address="http://localhost:8080/MyService" binding="basicHttpBinding"
            contract="ConsoleApplication1.localhost.ICalculate"
            name="BasicHttpBinding_ICalculate" />
        </client>
      </system.serviceModel>
    </configuration>

    使 用 VS2005 的 "Add Service Reference..." 功能时,会自动创建客户端代理文件和配置文件。

    uploads/200704/07_173540_wcfconfigeadd.gif


    --------------

    Service Configuration Editor 使用方法 图文演示

    1. 打开编辑器,创建新的配置文件。
    uploads/200704/10_104755_config002.gif


    2. 创建新的服务设置。
    uploads/200704/10_104759_config003.gif


    3. 手工输入,或使用 "Browser..." 选择服务所在程序集。
    uploads/200704/10_104804_config004.gif


    4. 确认契约是否正确。
    uploads/200704/10_104808_config005.gif


    5. 选择通讯方式。
    uploads/200704/10_104812_config006.gif


    6. 选择操作方式。
    uploads/200704/10_104816_config007.gif


    7. 将服务端点地址清空,不要理会提示。我们在后面使用 host 配置,这样便于日后调整路径。
    uploads/200704/10_104821_config008.gif


    8. 完成服务设置。
    uploads/200704/10_104825_config009.gif


    9. 查看服务设置。
    uploads/200704/10_104829_config010.gif


    10. 添加服务行为设置。
    uploads/200704/10_104833_config011.gif


    11. 添加 serviceMetadata,以便发布元数据。
    uploads/200704/10_104838_config013.gif


    12. 将 HttpGetEnabled 置为 True。
    uploads/200704/10_104842_config014.gif


    13. 修改服务行为设置。
    uploads/200704/10_104846_config016.gif


    14. 在 Host 上添加一个 Base Address,这样其它相关地址可以全部使用局部路径,便于日后维护更改。
    uploads/200704/10_104851_config017.gif


    15. 添加一个新的服务端点,用于配置 WS-MetadataExchange。(可选)
    uploads/200704/10_104855_config018.gif


    16. 注意 MetadataExchange 的相关参数设置。
    uploads/200704/10_104859_config019.gif


    17. 保存配置文件。
    uploads/200704/10_104905_config020.gif


    18. 运行服务器,在浏览器中输入 Host Base Address,我们就可以看到服务页面了。
    uploads/200704/10_104909_config021.gif


    19. 单击链接,查看 WSDL 信息。
    uploads/200704/10_104912_config022.gif


    客户端的配置方式比 较简单,不再演示。 (转载)
  • 相关阅读:
    【BZOJ4637】期望 Kruskal+矩阵树定理
    IEnumerable是什么
    (转)IIS windows认证
    使用pgrouting进行最短路径搜索
    Nginx设置防止IP及非配置域名访问
    java 调用c# web api 代码
    一台机器部署多个tomcat服务 nginx反向代理多个服务 笔记
    利用Kettle 从Excel中抽取数据写入SQLite
    sql server 统计信息
    sql server 索引碎片相关问题
  • 原文地址:https://www.cnblogs.com/guanjie20/p/1743153.html
Copyright © 2011-2022 走看看