惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
MongoDB | Blog
MongoDB | Blog
V
V2EX
博客园 - 【当耐特】
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
B
Blog
V
Visual Studio Blog
D
DataBreaches.Net
C
Check Point Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Fortinet All Blogs

博客园 - Animax!

The Tao of Programming Light weight Framework (AnyBase) -- 通信模块说明 关于WinIO.DLL的键盘输入模拟 Light weight Framework (AnyBase) -- Core 模块说明 开源项目 Light weight Framework (AnyBase) 发布 DB4o的缓存机制 Winfrom界面异步操作的一个解决方法 db4o 研究--性能测试 Asp.net动态数据(Dynamic Data) 笔记一 MVC — 笔记 WF笔记 – Workflow概念 LINQ TO SQL 笔记 — 存储过程、并发与事务 WCF笔记 - 绑定 WCF 笔记 正则表达式[转载整理] - Animax! - 博客园 LINQ 笔记 - LINQ to SQL 基本数据操作 - Animax! LINQ 笔记 - 语法与关键字 LINQ 笔记- Lambda Excel导入SQL - Animax! - 博客园
WCF Demo – Http、TCP Host - Animax!
Animax! · 2008-04-21 · via 博客园 - Animax!

2008-04-21 16:46  Animax!  阅读(1304)  评论()    收藏  举报

契约数据类:

using System.Runtime.Serialization;

using System.ServiceModel;

namespace PublicElements

{

    public class publicData

    {

        [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.Runtime.Serialization;

using System.ServiceModel;

namespace PublicElements

{

    public class publicInterface

    {

        [ServiceContract]

        public interface IService

        {

            [OperationContract]

            string GetData(int value);

            [OperationContract]

            publicData.CompositeType GetDataUsingDataContract(publicData.CompositeType composite);

        }

    }

}

服务逻辑类:

namespace WCFService

{

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

    public class Service : PublicElements.publicInterface.IService

    {

        public string GetData(int value)

        {

            return string.Format("You entered: {0}", value);

        }

        public PublicElements.publicData.CompositeType GetDataUsingDataContract(PublicElements.publicData.CompositeType composite)

        {

            if (composite.BoolValue)

            {

                composite.StringValue += "Suffix";

            }

            return composite;

        }

    }

}

Host配置文件:

<?xmlversion="1.0"encoding="utf-8"?>

<configuration>

    <!--配置监听的端口 -->

    <appSettings>

      <addkey="TcpService"value="net.tcp://localhost:9000/ServoceHost" />

      <addkey="HttpService"value="http://localhost:9001/ServoceHost" />

    </appSettings>

    <system.serviceModel>

        <services>

             <!-- 服务配置-->

            <servicebehaviorConfiguration="MyServiceTypeBehaviors"name="WCFService.Service">

              <! TCP 绑定 -->

               <endpointaddress=""binding="netTcpBinding"bindingConfiguration=""

                    contract="PublicElements.publicInterface+IService" />

               <!Http绑定 -->

               <endpointaddress=""binding="wsHttpBinding"bindingConfiguration=""

                    contract="PublicElements.publicInterface+IService" />

              <!为Http绑定公开元数据 -->

               <endpointaddress="mex"binding="mexHttpBinding"

contract="PublicElements.publicInterface+IService" />

            </service>

        </services>

      <!配置服务器行为,公开http元数据 -->

      <behaviors>

        <serviceBehaviors>

          <behaviorname="MyServiceTypeBehaviors" >

            <serviceMetadatahttpGetEnabled="true" />

          </behavior>

        </serviceBehaviors>

      </behaviors>

    </system.serviceModel>

</configuration>

Host

static void Main(string[] args)

{

            #region使用配置文件的方法

            Service WCFServer = new Service();

            Uri tcpAddress = new Uri(ConfigurationManager.AppSettings["TcpService"]);

            Uri httpAddress = new Uri(ConfigurationManager.AppSettings["HttpService"]);

            ServiceHost serviceHost = new ServiceHost(WCFServer, tcpAddress, httpAddress);

            serviceHost.Open();//

            Console.WriteLine("服务器已经打开");

            Console.WriteLine("按任意键关闭");

            Console.Read();

            serviceHost.Close();

            #endregion

}

这样客户端就可以像WebService那样直接使用服务引用了。但是需要Host时正在运行可能接收到服务的元数据,因此需要添加服务引用必须在Host运行的时候添加引用。