zoukankan      html  css  js  c++  java
  • 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世纪的伯牙
  • 相关阅读:
    C++ *this与this的区别(系个人转载,个人再添加相关内容)
    C++ 友元(系转载多人博客,添加个人见解)
    C++模板详解(系转载,但是个人添加了一些内容)
    实验三:klee的执行重现机制(示例分析)
    klee错误汇报二:KLEE的optimize选项的一个困惑
    KLEE错误汇报一:One phenomenon after the execution using klee
    实验一:使用符号执行工具klee对软件进行破解(来自于klee官网)
    个人发现的createProcess调用漏洞
    docker运行时设置redis密码并替换redis默认的dump.rdb
    saltstack入门个人笔记
  • 原文地址:https://www.cnblogs.com/2814/p/2068364.html
Copyright © 2011-2022 走看看