WCF第一例

ABC: Address,Binding(BasicHttpBinding/WSHttpBinding/NetTcpBinding etc.),Contract

1.添加一个WCF服务库项目

//IService1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;



namespace HelloIndigoWCF

{

    // 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。

    [ServiceContract]

    public interface IService1

    {

        [OperationContract]

        string GetData(int value);



        [OperationContract]

        CompositeType GetDataUsingDataContract(CompositeType composite);



        // 任务: 在此处添加服务操作

    }



    // 使用下面示例中说明的数据协定将复合类型添加到服务操作

    [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; }

        }

    }

}



//Service1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;



namespace HelloIndigoWCF

{

    // 注意: 如果更改此处的类名“IService1”,也必须更新 App.config 中对“IService1”的引用。

    public class Service1 : IService1

    {

        public string GetData(int value)

        {

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

        }



        public CompositeType GetDataUsingDataContract(CompositeType composite)

        {

            if (composite.BoolValue)

            {

                composite.StringValue += "Suffix";

            }

            return composite;

        }

    }

}



App.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <system.web>

    <compilation debug="true" />

  </system.web>

  <!-- 部署服务库项目时,必须将配置文件的内容添加到 

  主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->

  <system.serviceModel>

    <services>

      <service name="HelloIndigoWCF.Service1" behaviorConfiguration="HelloIndigoWCF.Service1Behavior">

        <host>

          <baseAddresses>

            <add baseAddress = "http://localhost:65534/Design_Time_Addresses/HelloIndigoWCF/Service1/" />

          </baseAddresses>

        </host>

        <!-- Service Endpoints -->

        <!-- 除非完全限定,否则地址将与上面提供的基址相关 -->

        <endpoint address ="" binding="wsHttpBinding" contract="HelloIndigoWCF.IService1">

          <!-- 

              部署时,应删除或替换下列标识元素,以反映

              在其下运行部署服务的标识。删除之后,WCF 将

              自动推导相应标识。

          -->

          <identity>

            <dns value="localhost"/>

          </identity>

        </endpoint>

        <!-- Metadata Endpoints -->

        <!-- 元数据交换终结点由服务用于向客户端做自我描述。--> 

        <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="HelloIndigoWCF.Service1Behavior">

          <!-- 为避免泄漏元数据信息,

          请在部署前将以下值设置为 false 并删除上面的元数据终结点  -->

          <serviceMetadata httpGetEnabled="True"/>

          <!-- 要接收故障异常详细信息以进行调试, 

          请将下值设置为 true。在部署前 

            设置为 false 以避免泄漏异常信息-->

          <serviceDebug includeExceptionDetailInFaults="true" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

</configuration>



2.Host项目

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;



namespace Host

{

    class Program

    {

        internal static ServiceHost myServiceHost = null;

        internal static void StartService()

        {

            myServiceHost = new ServiceHost(typeof(HelloIndigoWCF.Service1));

            myServiceHost.AddServiceEndpoint(typeof(HelloIndigoWCF.IService1), new WSHttpBinding(), "http://localhost:3391/abc");

            myServiceHost.Open();

        }

        internal static void StopService()

        {

            if (myServiceHost.State != CommunicationState.Closed)

                myServiceHost.Close();

        }



        static void Main(string[] args)

        {

            //using (ServiceHost host = new ServiceHost(typeof(HelloEmployee)))

            //{

            //    host.AddServiceEndpoint(typeof(IHelloEmployee), new WSHttpBinding(), "http://localhost:9002/MyEmployee");

            //    host.Open();

            //    Console.ReadLine();

            //}

            StartService();

            Console.WriteLine("the server is running ...");

            Console.ReadLine();

            StopService();



        }

    }

}



3.ClientApp

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



using System.ServiceModel;



namespace Client

{

    class Program

    {

        static void Main(string[] args)

        {

            HelloIndigoWCF.IService1 proxy = ChannelFactory<HelloIndigoWCF.IService1>.CreateChannel(new WSHttpBinding()

                , new EndpointAddress("http://localhost:3391/abc"));



            //HelloIndigoWCF.IService1 proxy = ChannelFactory<HelloIndigoWCF.IService1>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/HelloIndigo"));

            //localhost.HelloIndigoServiceClient proxy = new Client.localhost.HelloIndigoServiceClient();



            string s = proxy.GetData(1024);



            Console.WriteLine(s);

            Console.ReadLine();



        }

    }

}



你可能感兴趣的:(WCF)