WCF开发

无废话WCF入门教程一:https://www.cnblogs.com/iamlilinfeng/archive/2012/09/25/2700049.html

WCF合并了Web服务、网络通讯,.net Remoting、消息队列和Enterprise Services的功能并集成在Visual Studio中。

特点:面向服务开发,支持分布式开发!

重要步骤:

创建WCF服务应用程序

  • [ServiceContract],来说明接口是一个WCF的接口,如果不加的话,将不能被外部调用。
  • [OperationContract],来说明该方法是一个WCF接口的方法
  • 将xxx.svc“设为起始页”,然后F5运行一下,可以测试服务
  • 在IIS画面中,鼠标右键浏览XXX.svc,可以copy网址。
  • 创建WCF的客户端project,添加Service引用时,copy刚才的网址。
  • 双击Service引用,可以看到实现WCF的接口的类,可以使用它们。

================

无废话WCF入门教程二

WCF几个概念:

地址A(Address):Where to locate the WCF Service?

通讯 B(Binding):How to communicate with service?

能干什么 C(Contract):What functionalities do the Service provide?

这些被封装到endPoint里,参考下面服务端和客户端的endpoint内容。(注意,这里服务端的endpoint手动加,默认是省略的)

可以看出服务端和客户端endpoint内容是完全匹配的。

<!--Service Web.config-->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WCFService">
        <endpoint address="http://localhost/User.svc" binding="basicHttpBinding"
           contract="WCFService.IUser" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

<!--Client Web.config-->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <client>
      <endpoint address="http://localhost/User.svc" binding="basicHttpBinding"
         contract="WCFService.IUser" />
    </client>
  </system.serviceModel>
</configuration>

举例:

某一天,公司的领导让你去送一份合同文件,送文件的过程你可以选择的交通方式为“打车”、“公交”、“地铁”,当然费用是根据发票来报销的,到了对方公司后你要找到某经理,并且要一份收到合同文件的回执和相关文件。

地址:对方公司

通讯:打车,坐公交,坐地铁等。实际程序为:XML?Text?二进制?...采用哪种传输协议进行传输,TCP?Http?以及采用什么样的机制解决安全问题,SSL加密等

能干什么:找到某经理,并且要一份收到合同文件的回执和相关文件

================

无废话WCF入门教程三(WCF的宿主) https://www.cnblogs.com/iamlilinfeng/archive/2012/10/01/2706353.html

WCF服务分:WCF服务应用程序和WCF服务库

  • WCF服务应用程序使用默认的IIS作为宿主,使用非常方便。
  • WCF服务库类似与其他库项目,只是一个dll,运行时还需要宿主。宿主用控制台程序,winForm等等都可以。

主要试了下文中的WinForm宿主,在WinForm宿主项目中引用了WCF服务库。

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                host = new ServiceHost(typeof(Service1));
                host.Open();
                label1.Visible = true;
                this.label1.Text = "WCF中的HTTP监听已启动....";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);                
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            host.Close();
        }

有两个注意点:

注意点1:WCF服务库中app.config中  <system.serviceModel>节点前的注释:

<!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->

所以需要将WCF服务库中app.config的<system.serviceModel>节点中的内容需要copy到WinForm宿主中的app.config中。

注意点2:运行的时候,出现了异常,异常消息:Have you come across this issue HTTP could not register URL http://+ . Another application has already registered this URL with HTTP.SYS

参考了https://blogs.msdn.microsoft.com/keithmg/2009/08/10/http-could-not-register-url-http-another-application-has-already-registered-this-url-with-http-sys/ 中的remove the wcf lib from the solution and reference it

把WCF服务库类从solution中remove了,然后reference它的dll。

================

无废话WCF入门教程四(WCF的配置文件)

主要讲了配置,例如安全选项等等,暂时用不到,跳过

================

无废话WCF入门教程五(WCF的通信模式) https://www.cnblogs.com/iamlilinfeng/archive/2012/10/03/2710698.html

主要讲了三种调用模式

  • 默认是等待服务结束才返回(同步调用)
  • 单向模式,调用立刻返回,使用 IsOneWay=true 标记。结果都没有,感觉没用??
  • 双向模式,即客户端传了个回调函数,服务端得到结果后会调用这个回调函数。

猜你喜欢

转载自blog.csdn.net/jfyy/article/details/82992279
WCF