zoukankan      html  css  js  c++  java
  • [WCF] Restful 自定义宿主

    IPersonRetriever:

    /*
     * 由SharpDevelop创建。
     * 用户: Administrator
     * 日期: 2017/6/2
     * 时间: 22:13
     * 
     * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
     */
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace WcfRESTful
    {
    	/// <summary>
    	/// Description of IPersonRetriever.
    	/// </summary>
    	[ServiceContract]
    	public interface IPersonRetriever
    	{
    		[OperationContract]
    		[WebInvokeAttribute(UriTemplate = "Persons",Method="POST", ResponseFormat = WebMessageFormat.Json)]
    		Person GetPerson();
    	}
    	
    	[DataContract]
    	public class Person
    	{
    		[DataMember]
    		public string Name { get; set; }
    		[DataMember]
    		public int Age { get; set; }
    		[DataMember]
    		public string Birthday { get; set; }
    	}
    }
    

     PersonRetriever:

    /*
     * 由SharpDevelop创建。
     * 用户: Administrator
     * 日期: 2017/6/2
     * 时间: 22:15
     * 
     * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
     */
    using System;
    using System.ServiceModel.Web;
    
    namespace WcfRESTful
    {
    	/// <summary>
    	/// Description of PersonRetriever.
    	/// </summary>
    	public class PersonRetriever: IPersonRetriever
    	{
    		public Person GetPerson()
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                return new Person { Name = "Test", Age = 22, Birthday = DateTime.Now.ToString("yyyy-mm-dd HH:MM:ss:ffff") };
            }
        }
    }
    

     Program :

    /*
     * 由SharpDevelop创建。
     * 用户: Administrator
     * 日期: 2017/6/2
     * 时间: 22:19
     * 
     * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
     */
    using System;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    
    namespace WcfRESTful
    {
    	class Program
    	{
    		public static void Main(string[] args)
    		{
    			Console.WriteLine("Hello World!");
    			
    			// TODO: Implement Functionality Here
    			
    			Uri baseAddress = new Uri("http://127.0.0.1:9998/PersonRetriever");
    			using (ServiceHost host = new ServiceHost(typeof(PersonRetriever), baseAddress)) {
    				WebHttpBinding binding = new WebHttpBinding();
    				ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IPersonRetriever), binding, baseAddress);
    				WebHttpBehavior httpBehavior = new WebHttpBehavior();
    				endpoint.Behaviors.Add(httpBehavior);
    				host.Opened += delegate {
    					Console.WriteLine("Hosted successfully.");
    				};
    				host.Open();
    				Console.ReadLine();
    			}
    			
    			Console.Write("Press any key to continue . . . ");
    			Console.ReadKey(true);
    		}
    	}
    }
    

     截图 :

    源码: http://files.cnblogs.com/files/Areas/WcfRESTful.zip

  • 相关阅读:
    所有抽样单元都有相等的被选取机会 ,说法错误
    银行存款余额表由谁编制的
    资本公积——资本溢价与资本公积——其他资本公积
    货币单元抽祥
    企业安全生产费用账务的处理
    Tableau代写制作地图可视化和树形图、条形图
    Tableau 代写数据可视化:探索性图形分析新生儿死亡率数据
    R、Python代写、Open Refine采集pdf数据,清理数据和格式化数据
    使用GIS编程代写制作静态地图和处理地理数据
    用R语言编程代写制作交互式图表和地图
  • 原文地址:https://www.cnblogs.com/Areas/p/6935547.html
Copyright © 2011-2022 走看看