当前位置: 移动技术网 > IT编程>开发语言>.net > WCF学习笔记一之通过配置web.config可以通过http访问接口

WCF学习笔记一之通过配置web.config可以通过http访问接口

2018年12月11日  | 移动技术网IT编程  | 我要评论

金瓶梅2快播,安卓捕鱼达人2破解版,北大荒大米

一、准备

这里涉及到三个文件,现在只是简单的把代码贴出来,后面再详细的讲一下。

三个文件分别是(都是wcf服务应用程序项目下的):

1、iservice1.cs

2、service1.svc

3、web.config

 

wcf的契约文件:iservice1.cs

 1 using system;
 2 using system.collections.generic;
 3 using system.linq;
 4 using system.runtime.serialization;
 5 using system.servicemodel;
 6 using system.servicemodel.web;
 7 using system.text;
 8 using dal;
 9 
10 namespace httpvisitwcf2
11 {
12     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“iservice1”。
13     [servicecontract]
14     public interface iservice1
15     {
16 
17         [operationcontract]
18         [webget(uritemplate="/getdata/{value}",requestformat=webmessageformat.json,responseformat=webmessageformat.json)]
19         testmodel getdata(string value);
20 
21         [operationcontract]
22         compositetype getdatausingdatacontract(compositetype composite);
23 
24         // todo: 在此添加您的服务操作
25     }
26 
27 
28     // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
29     [datacontract]
30     public class compositetype
31     {
32         bool boolvalue = true;
33         string stringvalue = "hello ";
34 
35         [datamember]
36         public bool boolvalue
37         {
38             get { return boolvalue; }
39             set { boolvalue = value; }
40         }
41 
42         [datamember]
43         public string stringvalue
44         {
45             get { return stringvalue; }
46             set { stringvalue = value; }
47         }
48     }
49 }
iservice1

 

wcf契约的实现:service1.svc.cs

 1 using system;
 2 using system.collections.generic;
 3 using system.linq;
 4 using system.runtime.serialization;
 5 using system.servicemodel;
 6 using system.servicemodel.web;
 7 using system.text;
 8 using dal;
 9 using newtonsoft;
10 
11 namespace httpvisitwcf2
12 {
13     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“service1”。
14     public class service1 : iservice1
15     {
16         public testmodel getdata(string value)
17         {
18             testmodel tm = new testmodel();
19             tm.name = "lilei";
20             tm.age = "18"+datetime.now;
21             string ret = newtonsoft.json.jsonconvert.serializeobject(tm);
22             testmodel temp = newtonsoft.json.jsonconvert.deserializeobject<testmodel>(ret);
23             return  tm;
24         }
25 
26         public compositetype getdatausingdatacontract(compositetype composite)
27         {
28             if (composite == null)
29             {
30                 throw new argumentnullexception("composite");
31             }
32             if (composite.boolvalue)
33             {
34                 composite.stringvalue += "suffix";
35             }
36             return composite;
37         }
38     }
39 }
service1

 

wcf实现通过http访问wcf接口的web配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetframework="4.0" />
  </system.web>
  
  
  <system.servicemodel>
    <bindings>
      <webhttpbinding>
        <binding name="webbinding"></binding>
      </webhttpbinding>
    </bindings>

    <services>
      <service name="httpvisitwcf2.service1" behaviorconfiguration="servicebehavior">
        <endpoint address="" behaviorconfiguration="webbehavior" binding="webhttpbinding" bindingconfiguration="webbinding" contract="httpvisitwcf2.iservice1"/>
      </service>
    </services>

    <!--<behaviors>
      <servicebehaviors>
        <behavior>
          --><!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --><!--
          <servicemetadata httpgetenabled="true"/>
          --><!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --><!--
          <servicedebug includeexceptiondetailinfaults="false"/>
        </behavior>
      </servicebehaviors>
    </behaviors>-->

    <behaviors>
      <endpointbehaviors>
        <behavior name="webbehavior">
          <!--这里必须设置-->
          <webhttp/>
        </behavior>
      </endpointbehaviors>
      <servicebehaviors>
        <behavior name="servicebehavior">
          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
          <servicemetadata httpgetenabled="true"/>
          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
          <servicedebug includeexceptiondetailinfaults="false"/>
        </behavior>
      </servicebehaviors>
    </behaviors>

    <servicehostingenvironment multiplesitebindingsenabled="true" />
  </system.servicemodel>




  <system.webserver>
    <modules runallmanagedmodulesforallrequests="true"/>
  </system.webserver>
  
</configuration>

 

 

二、解释一下

上面这三个文件是最简单的实现了,创建一个项目把代码贴过去就可以了。

为什么要用http访问wcf接口呢?我个人的理解就是实现前后端的分离。前段可以不用有后台代码,通过js从api那里获取数据就可以了,这样的话可以更大程度的解耦前后端。

 

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

相关文章:

验证码:
移动技术网