zoukankan      html  css  js  c++  java
  • 我的第一个wcf

    vs2012中新建一个解决方案

    新建WCF项目RestApi

    添加实体类

       [DataContract]
        public class Employee
        {
            private Guid id;
            private string name;
            private DateTime birthdate;
    
            [DataMember]
            public Guid Id
            {
                get { return id; }
                set { id = value; }
            }
    
            [DataMember]
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            [DataMember]
            public DateTime Birthdate
            {
                get { return birthdate; }
                set { birthdate = value; }
            }
        }

    添加接口类IServiceTest

     1  // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
     2     [ServiceContract(Name = "ToSender", Namespace = "http://ToSender.com/webservices/")]
     3     public interface ICostService
     4     {
     5         [OperationContract]
     6         Guid AddEmployee(Employee employee);
     7 
     8         [OperationContract]
     9         void DeleteEmployee(string id);
    10 
    11 
    12         [OperationContract]
    13         Employee UpdateEmployee(Employee employee);
    14 
    15 
    16         [OperationContract]
    17         Employee GetEmployee(string id);
    18 
    19         CompositeType GetDataUsingDataContract(CompositeType composite);        // TODO: 在此添加您的服务操作
    20 }
    View Code

    添加svc文件CostService.svc

     1  public class CostService : ICostService
     2     {
     3        
     4  
     5         public Guid AddEmployee(Employee employee)
     6         {
     7               
     8             return Guid.NewGuid();
     9         }
    10 
    11        
    12     
    13         public void DeleteEmployee(string id)
    14         {
    15             return;
    16         }
    17 
    18         
    19       
    20         public Employee UpdateEmployee(Employee employee)
    21         {
    22             return employee;
    23         }
    24 
    25          
    26       
    27         public Employee GetEmployee(string id)
    28         {
    29             return new Employee() { Id = Guid.NewGuid(), Name = "Neil Klugman", Birthdate = new DateTime(1930, 1, 1) };
    30         }
    31 
    32 
    33         public CompositeType GetDataUsingDataContract(CompositeType composite)
    34         {
    35             if (composite == null)
    36             {
    37                 throw new ArgumentNullException("composite");
    38             }
    39             if (composite.BoolValue)
    40             {
    41                 composite.StringValue += "Suffix";
    42             }
    43             return composite;
    44         }
    45 }
    View Code

    Web.config文件的配置

     1 <?xml version="1.0"?>
     2 <configuration>
     3  
     4   <system.web>
     5     <compilation debug="true" targetFramework="4.0"/>
     6     <authentication mode="None"/>
     7     <httpRuntime/>
     8   </system.web>
     9   <system.serviceModel>
    10     <bindings>
    11       <webHttpBinding>
    12         <!--这个是接收大数据加的-->
    13         <binding name="webBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
    14           <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
    15         </binding>
    16       </webHttpBinding>
    17     </bindings>
    18     <behaviors>
    19       <endpointBehaviors>
    20         <behavior name="SandwichServices.CostServiceBehavior">
    21           <!--開啟 WCF WEB HTTP 說明頁面-->
    22           <webHttp helpEnabled="true"/>
    23         </behavior>
    24       </endpointBehaviors>
    25       <serviceBehaviors>
    26         <behavior name="SandwichServices.CostServiceServiceBehavior">
    27           <serviceMetadata httpGetEnabled="true"/>
    28         </behavior>
    29       </serviceBehaviors>
    30     </behaviors>
    31  
    32     <services>
    33       <service name="Api.CostService" behaviorConfiguration="SandwichServices.CostServiceServiceBehavior">
    34         <endpoint address="" behaviorConfiguration="SandwichServices.CostServiceBehavior" binding=" mexHttpBinding" contract="Api.ICostService"/>
    35         <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
    36       </service>
    37     </services>
    38   </system.serviceModel>
    39 </configuration>
    View Code

    至此一个完整的wcf完成,在vs2012中选中  svc文件,按F5调试,将自动启动wcfTestClicent和vs自带的iis(用于寄宿wcf服务的)

    在wcf客户端添加服务

  • 相关阅读:
    指向函数的指针 分类: C/C++ 2015-07-13 11:03 14人阅读 评论(0) 收藏
    Nginx平台构架 分类: Nginx 2015-07-13 10:55 205人阅读 评论(0) 收藏
    Nginx介绍 分类: Nginx 服务器搭建 2015-07-13 10:50 19人阅读 评论(0) 收藏
    C++ Virtual介绍 分类: C/C++ 2015-06-16 21:36 26人阅读 评论(0) 收藏
    Ubuntu vim+ ctags(包含系统函数) + taglist 配置 分类: vim ubuntu 2015-06-09 18:19 195人阅读 评论(0) 收藏
    Makefile 入门与基本语法 分类: C/C++ ubuntu 2015-05-18 11:16 466人阅读 评论(0) 收藏
    计算机视觉顶会 2015-05-03 16:17 47人阅读 评论(0) 收藏
    python如何使用 os.path.exists()--Learning from stackoverflow 分类: python 2015-04-23 20:48 139人阅读 评论(0) 收藏
    Python调用C可执行程序(subprocess) 分类: python 服务器搭建 C/C++ shell 2015-04-13 21:03 87人阅读 评论(0) 收藏
    C Struct Hack
  • 原文地址:https://www.cnblogs.com/netqq/p/4157061.html
Copyright © 2011-2022 走看看