码迷,mamicode.com
首页 > 其他好文 > 详细

我的第一个wcf

时间:2014-12-11 12:12:16      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

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

bubuko.com,布布扣
 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

bubuko.com,布布扣
 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文件的配置

bubuko.com,布布扣
 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服务的)

 

bubuko.com,布布扣

bubuko.com,布布扣

在wcf客户端添加服务

bubuko.com,布布扣

我的第一个wcf

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/netqq/p/4157061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!