zoukankan      html  css  js  c++  java
  • wcf 学习笔记一 服务寄存到iis(个人写,非转载)

      这两有时间,想研究一下wcf ,通过博客园找了一个大神写的日记,跟着学习 http://www.cnblogs.com/artech/tag/WCF/

         我是找到他最早的文章一步一步学习的地址:http://www.cnblogs.com/artech/archive/2007/02/26/656901.html  我下面所讲的都是基于这篇文章的

      第一个把服务寄存在hosting的我就不在描述了,大神已经写的相当好,跟着做就可以了,我这里只是说一下把服务器寄存到iis上的问题

      大神把services寄存到iis的方案我不是很明白也没能实现,我抛开了他的想法,自己创建了一个新的项目如图:

    现在看一下解决方案的结构图:

    现在要做的就是用wcfServices寄存到iis上替代Sercice,新创建的 wcfServices 项目中会生成,svc文件 (在学习大神的文章时,我没有找到svc的创建方式,我用txt文件改后缀搞定的,只是项目一直没有达到预期效果,所以我也不确定我所做的是否正确)我们现在修改他里面的内容,在这之前我已经创建了一个服务契约 Contracts 和服务的实现 Services 所svc文件生成的东西我就不想用了,看代码

    我继承了 Services 服务实现类 CalculatorService  

     

     

    (如果你的配置总是提示:当前已禁用此服务的元数据发布。  从这里看起就可以了

    下一步就是关键了,我也顿在这半天,修改 web.config 文件  

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="wori">
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service behaviorConfiguration="wori" name="WcfServices.Service1">
            <endpoint binding="basicHttpBinding" contract="Contracts.ICalculator" address="" />
          </service>
        </services>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
    
    </configuration>

    这里说一下我对这个配置文件的理解

    <behavior name="wori">  name里的值, 要跟 behaviorConfiguration="wori" 里的值保持一样

    <service behaviorConfiguration="wori" name="WcfServices.Service1">  name :实现了服务契约的服务实现类 因为 WcfServices.Service1 服务实现类 继承了 Services.CalculatorService   所以这里我就写的 WcfServices.Service1 当然 你也可以写 Services.CalculatorService

    <endpoint binding="basicHttpBinding" contract="Contracts.ICalculator" address="" />
    • 地址(Address):地址决定了服务的位置,解决了服务寻址的问题,《WCF技术剖析(卷1)》第2章提供了对地址和寻址机制的详细介绍;
    • 绑定(Binding):绑定实现了通信的所有细节,包括网络传输、消息编码,以及其他为实现某种功能(比如安全、可靠传输、事务等)对消息进行的相应处理。WCF中具有一系列的系统定义绑定,比如BasicHttpBinding、WsHttpBinding、NetTcpBinding等,《WCF技术剖析(卷1)》第3章提供对绑定的详细介绍;
    • 契约(Contract):契约是对服务操作的抽象,也是对消息交换模式以及消息结构的定义。《WCF技术剖析(卷1)》第4章提供对服务契约的详细介绍。
    因为我刚刚没有理解Binding 所有这里还是刚开始的wcHttpBinding 这决定了他到底用什么协议来传递。具体能填写什么,看官方地址:http://msdn.microsoft.com/zh-cn/library/ms731399
    左侧是所有可填写值,具体代表着什么,可以自己进看一下


    上面配置完成后就可以把WcfServices 部署到iis上了,这里可以用vs的部署 ,右键- 》 属性 -》 web 点击创建虚拟目录




    至于client 这面就回到了 http://www.cnblogs.com/artech/archive/2007/02/26/656901.html  第五步

    最后client中的代码如下:
    using (ChannelFactory<ICalculator> casess = new ChannelFactory<ICalculator>(new BasicHttpBinding(), "http://localhost/WcfServices/Service1.svc"))
                {
                    ICalculator proxy = casess.CreateChannel();
                    using (proxy as IDisposable)
                    {
                        Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2));
                        Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2));
                        Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2));
                        Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2));
                    }
                }


  • 相关阅读:
    SGU 271 Book Pile (双端队列)
    POJ 3110 Jenny's First Exam (贪心)
    HDU 4310 Hero (贪心)
    ZOJ 2132 The Most Frequent Number (贪心)
    POJ 3388 Japanese Puzzle (二分)
    UVaLive 4628 Jack's socks (贪心)
    POJ 2433 Landscaping (贪心)
    CodeForces 946D Timetable (DP)
    Android Studio教程从入门到精通
    Android Tips – 填坑手册
  • 原文地址:https://www.cnblogs.com/sjns/p/2579555.html
Copyright © 2011-2022 走看看