sen

导航

学习笔记之 WCF基础

Posted on 2009-11-23 01:23  sen  阅读(375)  评论(0)    收藏  举报

WCF包括了:

  1. 服务契约
  2. 数据契约
  3. 操作契约

一,服务契约:

WCF

步1:指定一个接口为服务契约,让其暴露(用接口而不用类的好处是为了:1,松耦合2,一个类有多个不同的操作)

  // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
    [ServiceContract (Name = "MyWCFEndPoint" ,Namespace="MyServerNameSpace") ]
    public interface IService1
    {
        [OperationContract (Name="GetServiceData" )]//这些面指定Name都是为了和客户端的低耦合性(服务端发生变化而不改客户端)
        string GetData( [ MessageParameter( Name ="_ServiceValue")]int value);//这里的Name和上面一样的作用
        //[OperationContract]把这个操作契约去掉不在客户端显示
        //[return: MessageParameter(Name = "MyReturnValue")]这个不是很明白乍么用
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

接口的实现(下面只是做了简单的实现)

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

接下来就是配置服务器的Endpoint (下面给出两种宿主的配置方法) ,当然可以用代码来写,这将在以后讲到

Web.config(用IIS作为宿主),如果用VS2008新加一个WCF application的话会自动用这种配置方式,且帮你配置好


  <system.serviceModel>
    <services>
      <service name="myWCFService.Service1" behaviorConfiguration="myWCFService.Service1Behavior">
        <!-- 这里配置了一个子路径为ABC的endpoint 如果要访问这个契约的话 那么要指明全路径+ "\ABC"
        比如 把 Service1.svc的路径是http://localhost:49206/Service1.svc那么当在客户端指定时就要写成
          <endpoint address="http://localhost:49206/Service1.svc/ABC" binding="wsHttpBinding" .../> -->
        <endpoint address="ABC" binding="wsHttpBinding" contract="myWCFService.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="myWCFService.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/><!--该值指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索-->
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

App.config(用WinForm / console作为宿主) + ServiceHost

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
            <service name="BusinessServices.ServiceA" behaviorConfiguration="serviceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000"/><!--基地址 ,这里指定此主机访问的方法这将会让主机开启8000口进行监听 -->
          </baseAddresses>
        </host>
        <!-- 客户端完整的URI是: <endpoint address="http://localhost:8000/ServiceA" binding="basicHttpBinding" .../> -->
        <endpoint address="ServiceA" contract="BusinessServiceContracts.IServiceA" binding="basicHttpBinding"  />
        <!-- 公开服务的元数据 IMetadataExchange契约 绑定协议 mexHttpBinding -->
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      </service>
      </services>
    </system.serviceModel>
</configuration>

上面主机其实是一个ServiceHost 类:(下面是MSDN的一句原话)

当您没有使用 Internet 信息服务 (IIS) 或 Windows 激活服务 (WAS) 公开服务时,请使用 ServiceHost 类来配置和公开服务以供客户端应用程序使用