WindowForm+WCF的简单开发流程

参考:http://www.cnblogs.com/huangxincheng/p/4558747.html  十五天精通WCF——第一天 三种Binding让你KO80%的业务html

理论:WCF的通信方式基于HTTP协议,传输消息为soapide

 

一、接口spa

第一步须要定义一个接口code

并不知道不定义该接口是否可行,接口既然是做为一种行为规范,因此不定义应该也是可行的orm

注意:必定要引入 System.ServiceModel;xml

 1   using System.Runtime.Serialization;
 2   using System.ServiceModel;
 3   
 4   namespace MyService
 5   {
 6       [ServiceContract] //只是接口或类在应用程序中定义的服务协议 ——能够理解为该标记将类或接口设置为服务协议
 7       public interface IHomeService
 8       {
 9           [OperationContract] //指定方法定义一个操做,该操做是该协议的一部分 ——该标记将该方法定义为协议中必须实现的
10          int GetLength(string name);
11      }
12  }

 

二、实现类htm

有接口就必定会有实现该接口的类blog

注意:引用 System.Messaging; System.Threading;dns

 1 using System;
 2 using System.Messaging;
 3 using System.Threading;
 4 
 5 namespace MyService
 6 {
 7     public class HomeService : IHomeService
 8     {
 9         public int GetLength(string name)
10         {
11             return name.Length;
12         }
13     }
14 }

三、启动服务接口

到这一步,WCF能够说基本配置成型,但还不能被外部程序调用

目前你能够在WCF中添加一个Form.cs调用以前实现的方法,也可按找参考文章中的作法

具体看需求

 

四、配置config文件

完成这一步,WCF将能够被外部程序调用

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="IHomeServiceBinding" />
      </netTcpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="WCF连接地址" />  <!--设置能够被远程调用-->
          <serviceDebug includeExceptionDetailInFaults="true" />  
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service name="MyService.HomeService">
        <endpoint address="http://127.0.0.1:1920/HomeService" binding="basicHttpBinding" contract="MyService.IHomeService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://127.0.0.1:1920"/>
          </baseAddresses>
        </host>
      </service>
    </services>

  </system.serviceModel>
</configuration>

五、经过WindowForm添加服务引用调用WCF

VS中与添加Web Service操做相同。添加成功后将会在App.config中自动配置节点。

相关文章
相关标签/搜索