當前位置:首頁 » ASP.NET技術教程

.NET教程WCF Helloworld入門教程

2011-06-02 14:00 本站整理 瀏覽(68313)

1.打開vs,file->new->project->wcf->wcf service application


2.建立好一個wcf之后在Iservice1.svc添加代碼如下:詳細見注釋


  


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;


namespace myServer
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {


        [OperationContract]
        string GetData(int value);


        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);


        //自己定義的方法
        [OperationContract]
        string getName(string name);


        // TODO: Add your service operations here
    }



    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";


        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }


        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }


}


3.service1.svc->service1.svc.cs添加代碼如下:詳細見注釋


  


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;


namespace myServer
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }


        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }



        //自己定義的方法
        public string getName(string name)
        {
            return name;
        }


    }
}


4.好,你成功建立一個wcf了。然后運行一下之后將url復制下來,比如我的就是http://localhost:3285/


5.在同一個解決方案里面右擊add->project->visual C#->windows->console application


6.在你添加的那個項目里面的references右擊add service reference 將你的url復制到address單擊Go之后改變你的namespace為myServer之后確定


7.若是一切順利的話,之后Program.cs如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//注意引入namespace(myServer是我剛剛設置的server名稱)
using ConsoleApplication1.myServer;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1Client sc = new Service1Client();//new 對象
            Console.WriteLine(sc.getName("zhangyongbin"));//調用server的方法
            Console.ReadKey();//為了暫停輸出窗體
        }
    }
}


之后運行程序就行了


中国福彩25选5