• 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世纪的伯牙
  • 相关阅读:
    mac/unix系统:C++实现一个端口扫描器
    C++:通过gethostbyname函数,根据服务器的域名,获取服务器IP
    PostMan Setting Proxy 设置 代理
    企业架构 Red Hat Drools KIE Project 三大核心产品
    IDS,IPS,IPD
    Vehicle routing with Optaplanner graph-theory
    SonarQube Detection of Injection Flaws in Java, C#, PHP
    Spring AOP Log
    Code Quality and Security | SonarQube
    Gradle vs. Maven: Performance, Compatibility, Speed, & Builds
  • 原文地址:https://www.cnblogs.com/2814/p/2068364.html
Copyright © 2020-2023  润新知