wcf (1)

WCF(Windows Communication Foundation) -
绑定Binding:Http以basicHttpBinding为例,Tcp以netTcpBinding为例。
2、宿主
Hello.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceHost2.Binding
{
    class Hello
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(WCF.ServiceLib.Binding.Hello)))
            {
                // 写代码的方式做host
                // host.AddServiceEndpoint(typeof(WCF.ServiceLib.Binding.IHello), new NetTcpBinding(), "net.tcp://localhost:54321/Binding/Hello");
                host.Open();

                Console.WriteLine("服务已启动");
                Console.WriteLine("按<ENTER>停止服务");
                Console.ReadLine();
            }
        }
    }
}


App.config

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <!--name - 提供服务的类名-->
      <!--behaviorConfiguration - 指定相关的行为配置-->
      <service name="WCF.ServiceLib.Binding.Hello" behaviorConfiguration="BindingBehavior">
        <!--address - 服务地址-->
        <!--binding - 通信方式-->
        <!--contract - 服务契约-->
        <!--<endpoint binding="basicHttpBinding" contract="WCF.ServiceLib.Binding.IHello" address="Hello" />-->
        <endpoint binding="netTcpBinding" contract="WCF.ServiceLib.Binding.IHello" address="net.tcp://localhost:54321/Binding/Hello" />
        <!--元数据交换的endpoint-->
        <!--注:address是mex,它会和host/baseAddresses节点中的baseAddress做拼接,即提供元数据交换的地址为:http://localhost:12345/Binding/mex-->
        <endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />
        <host>
          <baseAddresses>

            <add baseAddress="http://localhost:12345/Binding/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="BindingBehavior">
          <!--httpGetEnabled - 使用get方式提供服务-->
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

 WCF(Windows Communication Foundation) -
宿主(Hosting):WCF服务可以宿主在IIS, Application, WAS,
WindowsService。本文以宿主在WindowsService为例。

 WCF(Windows Communication Foundation) -
消息处理:通过操作契约的IsOneWay参数实现异步调用,基于Http, TCP, Named Pipe, MSMQ的双向通讯。

 /// 不使用OneWay(同步调用)
/// 使用OneWay(异步调用)

 2、宿主 OneWay.cs

 

using (ServiceHost host = new ServiceHost(typeof(WCF.ServiceLib.Message.OneWay)))
{
    host.Open();

    Console.WriteLine("服务已启动(WCF.ServiceLib.Message.OneWay)");
    Console.WriteLine("按<ENTER>停止服务");
    Console.ReadLine();

}

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 
<system.serviceModel>
   
<services>
     
<!--name - 提供服务的类名-->
     
<!--behaviorConfiguration - 指定相关的行为配置-->
     
<service name="WCF.ServiceLib.Message.OneWay" behaviorConfiguration="MessageBehavior">
       
<!--address - 服务地址-->
       
<!--binding - 通信方式-->
       
<!--contract - 服务契约-->
       
<endpoint address="" binding="basicHttpBinding" contract="WCF.ServiceLib.Message.IOneWay"/>
       
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
       
<host>
         
<baseAddresses>
           
<add baseAddress="http://localhost:12345/Message/OneWay/"/>
         
</baseAddresses>
       
</host>
     
</service>
   
</services>
   
<behaviors>
     
<serviceBehaviors>
       
<behavior name="MessageBehavior">
         
<!--httpGetEnabled - 使用get方式提供服务-->
         
<serviceMetadata httpGetEnabled="true"/>
         
<serviceDebug includeExceptionDetailInFaults="true"/>
       
</behavior>
     
</serviceBehaviors>
   
</behaviors>
 
</system.serviceModel>
</configuration>

 

 

 

 

posted @ 2012-08-05 21:47  小薇林  阅读(184)  评论(0编辑  收藏  举报