码迷,mamicode.com
首页 > 其他好文 > 详细

WCF默认实例的解读

时间:2015-02-04 18:25:36      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

一:图片

技术分享

IService1.cs是定义的接口,包含对Service、方法和方法用的类的声明的声明

Service1.cs是对接口的实现,包含实现的方法

代码注释:

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

namespace WcfServiceLibraryTest
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
    [ServiceContract]
    public interface IService1//定义一个接口
    {
        [OperationContract]
        string GetData(int value);//定义一个方法,返回类型为string,参数为int

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);//定义一个方法,返回类型为CompositeType,参数为CompositeType,下面为对象的定义

        // TODO: 在此添加您的服务操作
    }

    // 使用下面示例中说明的数据协定将复合类型添加到服务操作
    [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; }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceLibraryTest
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“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;
        }
    }
}

 

WCF默认实例的解读

标签:

原文地址:http://www.cnblogs.com/hongmaju/p/4272961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!