代码改变世界

WCF Demo – Http、TCP Host

2008-04-21 16:46  Animax!  阅读(1267)  评论(1编辑  收藏  举报

契约数据类:

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