• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#WCF服务端搭建和客户端调用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#压缩文件,C#压缩文件夹,C#获取文件发布时间:2022-07-10
下一篇:
C#客户端网络请求对HttpClient的封装发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap