当前位置: 移动技术网 > IT编程>开发语言>.net > .net WCF简单实例详解(5)

.net WCF简单实例详解(5)

2019年05月28日  | 移动技术网IT编程  | 我要评论

一羽月土米水日古余,scrss.exe,袁郑健博客

本文为大家分享了.net wcf简单实例,供大家参考,具体内容如下

1.创建wcf项目

2.系统自动生成iwcfservice

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“iservice1”。
  [servicecontract]
  public interface iwcfservice
  {

    [operationcontract]
    string getdata(int value);

    [operationcontract]
    compositetype getdatausingdatacontract(compositetype composite);

    // todo: 在此添加您的服务操作
  }


  // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
  [datacontract]
  public class compositetype
  {
    bool boolvalue = true;
    string stringvalue = "hello ";

    [datamember]
    public bool boolvalue
    {
      get { return boolvalue; }
      set { boolvalue = value; }
    }

    [datamember]
    public string stringvalue
    {
      get { return stringvalue; }
      set { stringvalue = value; }
    }
  }

(1)服务契约:servicecontract(服务)和operationcontract  (方法)

(2)数据契约:datacontract(类)和datamember(属性) 用于类和结构上

(3)消息契约:messagecontract 用于soap消息

3.wcf服务类

public class wcfservice : iwcfservice
  {
    public string getdata(int value)
    {
      return string.format("you entered: {0}", value);
    }

    public compositetype getdatausingdatacontract(compositetype composite)
    {
      if (composite == null)
      {
        throw new argumentnullexception("composite");
      }
      if (composite.boolvalue)
      {
        composite.stringvalue += "suffix";
      }
      return composite;
    }
  }

4.服务配置文件

<system.servicemodel>
  <!--配置绑定节点start-->
  <bindings>
   <basichttpbinding>
    <binding name="basichttpbinding0" maxreceivedmessagesize="2147483647">
     <readerquotas maxstringcontentlength="2147483647"/>
     <security mode="none" />
    </binding>
   </basichttpbinding>
   <nettcpbinding>
    <binding name="nettcpbinding0" maxreceivedmessagesize="2147483647">
     <readerquotas maxstringcontentlength="2147483647"/>
     <security mode="none" />
    </binding>
   </nettcpbinding>
   <wshttpbinding></wshttpbinding>
  </bindings>
  <!--配置绑定节点end-->
  
  <!--配置服务节点start-->
  <services>
   <!--配置某一服务,在这里可以指定服务名称-->
   <service name="wcfservicetest.wcfservice">
    <endpoint address="aaa" binding="basichttpbinding" bindingconfiguration="basichttpbinding0"
     name="basichttpbinding_wcfservice" contract="wcfservicetest.iwcfservice">
     <identity>
      <dns value="localhost"/>
     </identity>
    </endpoint>
    <endpoint address="" binding="nettcpbinding" bindingconfiguration="nettcpbinding0"
     name="nettcpbinding_wcfservice" contract="wcfservicetest.iwcfservice">
     <identity>
      <dns value="localhost"/>
     </identity>
    </endpoint>
   </service>
  </services>
  <!--配置服务节点end-->

  <behaviors>
   <servicebehaviors>
    <behavior>
     <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
     <servicemetadata httpgetenabled="true" httpsgetenabled="true"/>
     <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
     <servicedebug includeexceptiondetailinfaults="false"/>
    </behavior>
   </servicebehaviors>
  </behaviors>
  <protocolmapping>
    <add binding="basichttpsbinding" scheme="https" />
  </protocolmapping>  
  <servicehostingenvironment aspnetcompatibilityenabled="true" multiplesitebindingsenabled="true" />
 </system.servicemodel>

5.iis部署wcf服务

6.添加客户端项目并添加服务引用

7.main程序中添加wcf服务并调用方法

 class program
  {
    static void main(string[] args)
    {
      var client = new wcfservice.wcfserviceclient();
      try
      {
        var str = client.getdata(2046);
        console.writeline(string.format("内容:{0}", str));
        client.close();
      }
      catch (exception ex)
      {
        console.writeline("出现异常!");
        client.abort();
      }
      console.readline();
    }
  }

8.客户端配置文件

<system.servicemodel>
    <bindings>
      <basichttpbinding>
        <binding name="basichttpbinding_wcfservice" />
      </basichttpbinding>
      <nettcpbinding>
        <binding name="nettcpbinding_wcfservice">
          <security mode="none" />
        </binding>
      </nettcpbinding>
    </bindings>
    <client>
      <!--<endpoint address="http://localhost/wcfservicetest/wcfservice.svc"
        binding="basichttpbinding" bindingconfiguration="basichttpbinding_wcfservice"
        contract="wcfservice.iwcfservice" name="basichttpbinding_wcfservice" />-->
      <endpoint address="net.tcp://localhost/wcfservicetest/wcfservice.svc"
        binding="nettcpbinding" bindingconfiguration="nettcpbinding_wcfservice"
        contract="wcfservice.iwcfservice" name="nettcpbinding_wcfservice">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.servicemodel>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网