wcf-rest

1.Service Layer

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services
{
    public class PersonService:IPerson
    {
        Person IPerson.GetPerson(string personid)
        {
            Person p = new Person() { PersonId = personid, PersonName = "lizhch" };
            return p;
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel.Web;
using System.Runtime.Serialization;

namespace Services
{
    [ServiceContract]
    public interface IPerson
    {
        [WebGet(UriTemplate = "{personid}")]
        Person GetPerson(string personid);
    }


    [System.Runtime.Serialization.DataContract]
    public class Person
    {
        [DataMember]
        public string PersonId{get;set;}

        [DataMember]
        public string PersonName { get; set; }
    }
}

 

 

2.Host Layer

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel;
using Services;
using System.ServiceModel.Web;

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WebServiceHost host = new WebServiceHost(typeof(PersonService)))
            {
                try
                {
                    host.Open();
                    Console.WriteLine("服务已启动");
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    host.Abort();
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
                finally 
                {
                    host.Close();
                }
                
            }
        }
    }
}

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.serviceModel>
    <services>
      <service name="Services.PersonService">
        <endpoint address="http://localhost:8888/Person" binding="webHttpBinding" contract="Services.IPerson"></endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

 

 

 

 

 

3.Browser Address

http://localhost:8888/Person/20

 <Person xmlns="http://schemas.datacontract.org/2004/07/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <PersonId>20</PersonId> 
  <PersonName>lizhch</PersonName> 
  </Person>

 

posted @ 2013-11-17 19:06  feidaochuanqing  阅读(215)  评论(0编辑  收藏  举报