第一个WCF DEMO,将WCF服务部署在控制台上

     因为项目框架是WCF+MVC,由于之前未实际运用过,所以做了个小DEMO,以下是具体过程。

  新建WCF服务库

     
     新建后会在解决方案里看到以下目录
    
     我们要修改的是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="WcfServiceLibrary.Service1" behaviorConfiguration="behaviorName">
        <host>
          <baseAddresses>
            <add baseAddress = "net.tcp://localhost:8732/Service1" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
        <endpoint address ="" binding="netTcpBinding" contract="WcfServiceLibrary.IService1"  bindingConfiguration="wsTcpBindingName" >
          <!-- 
              部署时,应删除或替换下列标识元素,以反映
             用来运行所部署服务的标识。删除之后,WCF 将
              自动推断相应标识。
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 --> 
        <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorName">
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false 并删除上面的元数据终结点  -->
          <serviceMetadata httpGetEnabled="False"/>
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false 
            以避免泄漏异常信息-->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding name="wsTcpBindingName" maxBufferPoolSize="12000000" maxReceivedMessageSize="12000000" >
          <readerQuotas maxStringContentLength="1200000" maxArrayLength="12000000"/>
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>

</configuration>

       传输方式是net.tcp,要注意的间bingdings,behaviors,services三个节点之点的名称要对应,还有就是发布服务的时候要将behaviors节点的serviceMetadata httpGetEnabled 设为false.

  测试服务成功了以后,新建一个控制台项目,引用WCF服务,然后代码启动服务

     

Uri uri = new Uri("net.tcp://localhost:8000/Service1/");
            using (ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary.Service1), uri))
            {
                BindingElement bindingElement = new TcpTransportBindingElement();
                CustomBinding binding = new CustomBinding(bindingElement);
                host.AddServiceEndpoint(typeof(IService1), binding, "Service1");
                ServiceMetadataBehavior metadataBehavior;
                metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (metadataBehavior == null)
                {
                    metadataBehavior = new ServiceMetadataBehavior();
                    host.Description.Behaviors.Add(metadataBehavior);
                }
                host.Open();
                Console.WriteLine("WCF服务已启动");

      运行结果:

    

       附上DEMO:MVCDemo.rar

posted on 2013-04-23 11:22  hardy_Wang  阅读(795)  评论(0编辑  收藏  举报

导航