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

推荐订阅源

V
V2EX
V
Vulnerabilities – Threatpost
MongoDB | Blog
MongoDB | Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
Know Your Adversary
Know Your Adversary
aimingoo的专栏
aimingoo的专栏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
A
Arctic Wolf
L
LangChain Blog
L
LINUX DO - 热门话题
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
I
Intezer
爱范儿
爱范儿
P
Palo Alto Networks Blog
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
G
GRAHAM CLULEY
S
Securelist
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Cisco Talos Blog
Cisco Talos Blog
Security Latest
Security Latest
Martin Fowler
Martin Fowler
AWS News Blog
AWS News Blog
L
Lohrmann on Cybersecurity
C
Cybersecurity and Infrastructure Security Agency CISA
酷 壳 – CoolShell
酷 壳 – CoolShell
Recorded Future
Recorded Future
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
V2EX - 技术
V2EX - 技术
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
LINUX DO - 最新话题
博客园 - Franky
P
Privacy & Cybersecurity Law Blog
Simon Willison's Weblog
Simon Willison's Weblog
W
WeLiveSecurity
Cyberwarzone
Cyberwarzone
The Hacker News
The Hacker News
A
About on SuperTechFans

博客园 - 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运行的时候添加引用。