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);



  • 相关阅读:
    @Value注解无法为static 变量赋值
    CloseableHttpClient方式配置代理服务器访问外网
    微信小程序上传图片
    centos7 无界面静默安装 oracle
    Linux安装配置JDK
    java导出Excel定义导出模板
    ECharts柱状图
    Java获取客户端真实IP
    Eclipse控制台输出log日志中文乱码
    $_SRRVER常用用法
  • 原文地址:https://www.cnblogs.com/SuperMetalMax/p/6214358.html
Copyright © 2011-2022 走看看