消費WCF服務託管在IIS5/6


在IIS中承載5/6 WCF服務的消費過程下面詳細包括建立代理和控制台應用程式進行了討論。

第1步:一旦服務託管在IIS中,接下來我們要建立消費這種服務的用戶端應用程式。在建立用戶端應用程式,我們需要建立代理的服務。這個代理所使用的用戶端應用程式與服務進行互動。要建立代理,執行Visual Studio 2008命令提示。使用服務工具,我們可以建立代理類和它的組態資訊。

svcutil http://localhost/IISHostedService/Service.svc

Consuming WCF hosted in IIS

 

執行此命令後,我們會發現在預設位置生成了兩個檔案。

  • MyService.cs - 代理類的WCF服務
  • output.config - 有關該服務的組態資訊。

第2步:現在,我們將開始建立使用Visual Studio 2008(用戶端應用)的控制台應用程式。

Consuming WCF hosted in IIS

 

第3步:新增參照“System.ServiceModel”;這是WCF的核心DLL。

第4步:建立一個代理類。

Consuming WCF hosted in IIS

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyServiceClient
{
  Class Program
  {
     Static void Main(string[] args)
     {
        //Creating Proxy for the MyService
        ServiceClient Client = newServiceClient();
        Console.WriteLine("Client calling the service...");
        Console.WriteLine("Hello Ram");
        Console.Read();
     }
  }
}

 

Consuming WCF hosted in IIS