我的第一個wcf

來源:互聯網
上載者:User

標籤: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

 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用戶端添加服務

我的第一個wcf

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.