WCF Helloworld 入门教程

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();//为了暂停输出窗体
        }
    }
}

之后运行程序就行了

寻找21世纪的伯牙
原文地址:https://www.cnblogs.com/2814/p/2068364.html