zoukankan      html  css  js  c++  java
  • C# WCF服务端搭建和客户端调用

    1) 打开Visual Studio 2012,在菜单上点击文件—>新建—>项目—>WCF服务应用程序。在弹出界面的“名称”对应的文本框中输入“WcfServiceTest”,然后点击“确定”按钮。

    如下图:

    2) 把Visual Studio 2012自动生成的两个文件IService1.cs改名为IService.cs接口类、Service1.cs改名为Service.cs实现类。

    如下图:

    3) 由于Visual Studio 2012默认生成的app.config中的配置信息是以IService来写的配置。

    如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.web>
        <compilation targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"  multipleSiteBindingsEnabled="true" />
        <behaviors>
          <endpointBehaviors>
            <behavior name="RestBehavior">
              <webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IService" />
          </basicHttpBinding>
          <webHttpBinding>
            <binding name="webHttpBindingConfig" sendTimeout="00:00:40" bypassProxyOnLocal="false"
              maxReceivedMessageSize="652428800">
              <readerQuotas maxStringContentLength="65242880" maxArrayLength="65242880" />
            </binding>
          </webHttpBinding>
        </bindings>
        <services>
          <service name="WcfService.IService">
            <endpoint address="" behaviorConfiguration="RestBehavior" binding="webHttpBinding"
              bindingConfiguration="webHttpBindingConfig" contract="WcfService.IService">
              <identity>
                <dns value="Localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>
        <standardEndpoints/>
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
            在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>
    
    </configuration>

    4) 右键WcfServiceTest项目选址[发布]。

    如下图:

    5) 控制面板 - 管理工具 - Internet 信息服务(IIS)管理器 - 选址[网站]节点 添加网站。

    如下图:

    6) 新建一个测试项目添加服务引用。

    如下图:

    7) 服务接口调用如下。

                //1.ServiceClient sc = new ServiceClient();
                //2.ServiceClient sc = new ServiceClient("BasicHttpBinding_IService", "http://192.168.99.64:9999/WcfService.Service.svc");
                ServiceClient sc = new ServiceClient();
                string strRst = sc.GetData(100);



  • 相关阅读:
    案例8高级打砖块游戏
    案例17——Flash地图应用
    案例11——用遮障制作聚光灯效果及冷却动画
    案例1:用Flash CS工具制作进度条
    Flex 中可以应用于 ActionScript 类的元标签
    案例7用Flash Builder做虚拟计算器
    案例16——没特效还玩毛个Flash
    案例9不要在加载时面对苍白的屏幕
    案例15——游戏物品栏的制作(AS3高级应用)
    案例2心跳模拟
  • 原文地址:https://www.cnblogs.com/SuperMetalMax/p/6214358.html
Copyright © 2011-2022 走看看