• 一个简单的wcf实现


    打开vs2010,新建项目如图

    添加新建项目

    添加服务引用

    添加新建项webform.aspx

        <div>
        <p><asp:TextBox ID="chuancan" runat="server"></asp:TextBox><asp:Button ID="btn_ok" 
                runat="server" Text="提交" onclick="btn_ok_Click" /></p>
        <p>
            <asp:Label ID="showtext" runat="server" Text=""></asp:Label></p>
        </div>
    

      webform.aspx.cs

            protected void btn_ok_Click(object sender, EventArgs e)
            {
                ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
                showtext.Text = client.HelloWorld(chuancan.Text.Trim().ToString());
            }
    

      

    wcf项目里的IService1.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 WcfService1
    {
        // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
        [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);
    
            // TODO: 在此添加您的服务操作
    
            [OperationContract]
            string HelloWorld(string user);//自己添加的一个方法
    } // 使用下面示例中说明的数据约定将复合类型添加到服务操作。 [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; } } } }

      Services1.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 WcfService1
    {
        // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
        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 HelloWorld(string user)
            {
                return "你好," + user + "!";
            }
        }
    }
    

      以上就是我的第一个wcf应用程序

    点此下载相关代码

    -------------------------------------------------

    保持专注,只做一件事,做好这件事!@

    -------------------------------------------------

  • 相关阅读:
    Opencv保存摄像头视频&&各种编码器下视频文件占用空间对比
    生活的 tricks
    生活的 tricks
    词汇的积累 —— 反义词、同义词
    词汇的积累 —— 反义词、同义词
    目标跟踪系列十一:Exploiting the Circulant Structure of Tracking-by-detection with Kernels代码思路
    Java中Integer类的方法
    php中 重载(二)
    协方差的意义
    malloc函数具体解释
  • 原文地址:https://www.cnblogs.com/24la/p/wcf.html
Copyright © 2020-2023  润新知