WCF 实例

 一、 WCF接口

 // 注意: 如果更改此处的接口名称“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; }
        }
    }

 

 

 

三、接口具体实现

     // 注意: 如果更改此处的类名“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 文件中。System.Configuration 不支持库的配置文件。-->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1" behaviorConfiguration="WcfServiceLibrary1.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:9999/WcfServiceLibrary1/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
        <endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">
          <!--
              部署时,应删除或替换下列标识元素,以反映
              在其下运行部署服务的标识。删除之后,WCF 将
              自动推导相应标识。
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- 元数据交换终结点由服务用于向客户端做自我描述。-->
        <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceLibrary1.Service1Behavior">
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false 并删除上面的元数据终结点  -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- 要接收故障异常详细信息以进行调试,
          请将下值设置为 true。在部署前
            设置为 false 以避免泄漏异常信息-->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

 

http://localhost:9999/WcfServiceLibrary1/Service1/   WcfServiceLibrary1/Service1为WCF命名空间名称

binding="mexHttpBinding": 绑定方式

此处要注意的是需要修改下最大的缓存空间,数据量大时经常会超出。默认为65320好象

 

五、服务端宿主(一般是控制台)

    引用项目或是放在同一个项目同

           using (ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary1.Service1)))
            {

                host.Opened += delegate 
                {
                    Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
                }; 
                host.Open(); 
                Console.Read();

 六、客户端调用

     1、通过添加服务引用,同样也要配置app.config文件,有修改只有更新引用就可以了

     2、通过生成的引用文件,直接添加。如果有修改需要重新通过工具生成代码文件

         

       若要测试此服务,需要创建一个客户端,并将其用于调用该服务。可以使用下列语法,从命令行中使用 svcutil.exe 工具来进行此操作:      http://localhost:9999/WcfServiceLibrary1/Service1/?wsdl

 

 

七、其它方式 通过IIS寄宿服务

    寄宿IIS的服务寄宿比较简单,基本上包含两个步骤:为WCF服务创建.svc文件和创建IIS虚拟目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值