zoukankan      html  css  js  c++  java
  • WCF入门(七)---自托管消费WCF服务

    费自托管WCF服务的整个过程,一步步地解释以及充足的编码和屏幕截图是非常有必要。

    第1步:服务托管,现在我们需要实现的代理类客户端。创建代理的方式不同。

    • 使用svcutil.exe,我们可以创建代理类和配置文件以及端点。
    • 添加服务引用到客户端应用程序。
    • 实现 ClientBase<T> 类

    这三种方法,实现ClientBase<T>类是最好的做法。如果使用了两个rest方法,需要创建一个代理类,每一次当我们做出改变服务的实现。但是,这不是对ClientBase<T>类情况。这将创建代理只能在运行,所以它会打理一切。

    为此,创建一个代理类,其中包括refrencesof System.ServiceModel和MyCalculatorService。

    Consuming WCF that is Self hosted
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using MyCalculatorService;
    
    namespace MyCalculatorServiceProxy
    {
      Public class MyCalculatorServiceProxy : 
      //WCF create proxy for ISimpleCalculator using ClientBase
      ClientBase<ISimpleCalculator>,
      ISimpleCalculator
      {
         Public int Add(int num1, int num2)
         {
            //Call base to do funtion
            returnbase.Channel.Add(num1, num2);
         }
      }
    }
    View Code

    现在,创建一个控制台应用程序,其中包括System.ServiceModel和MyCalculatorServiceProxy的参考。

    Consuming WCF that is Self hosted
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using MyCalculatorServiceProxy;
    
    namespace MyCalculatorServiceClient
    {
      classProgram
      {
         Static void Main(string[] args)
         {
            MyCalculatorServiceProxy.MyCalculatorServiceProxy proxy = newMyCalculatorServiceProxy.MyCalculatorServiceProxy();
            Console.WriteLine("Client is running at " + DateTime.Now.ToString());
            Console.WriteLine("Sum of two numbers... 5+5 =" + proxy.Add(5, 5));
            Console.ReadLine();
         }
      }
    }
    View Code

    步骤2:结束点(相同服务)的信息应该被添加到客户端应用程序的配置文件。

     1 <?xmlversion="1.0"encoding="utf-8" ?>
     2 <configuration>
     3   <system.serviceModel>
     4      <client>
     5         <endpoint address ="http://localhost:8090/MyCalculatorServiceProxy/ISimpleCalculator"
     6                 binding ="wsHttpBinding"
     7                 contract ="MyCalculatorServiceProxy.ISimpleCalculator">
     8         </endpoint>
     9      </client>
    10   </system.serviceModel>
    11 </configuration>
    View Code

    步骤3:运行客户端应用程序之前,需要运行的服务。客户端应用程序的输出如下所示。

    Consuming WCF that is Self hosted
    原文地址:http://www.yiibai.com/wcf/wcf_consuming_services_iis.html
  • 相关阅读:
    guava学习--集合2&Range
    guava学习--集合1
    guava学习--FluentIterable
    guava学习--Supplier Suppliers
    guava--Joiner、Splitter、MapJoinner、MapSplitter
    python_输入一个数,判断是否是素数
    python_33_文件操作2
    python_32_文件操作1
    python_31_集合
    python_输出100:200内的素数
  • 原文地址:https://www.cnblogs.com/CSharpLover/p/5687336.html
Copyright © 2011-2022 走看看