共用方式為


遠端處理範例:在網際網路資訊服務 (IIS) 中裝載

本主題專門說明一項為了在現有應用程式中提供回溯相容性而保留的舊有技術,不建議用於新的開發工作。分散式應用程式應使用  Windows Communication Foundation (WCF) 進行開發。

下列範例會以稍微複雜的方式來實作基本 Web 服務。使用 BinaryFormatter 的原因是因為其承載較為精簡,而且系統可以少花一些時間在資料流的序列化與還原序列化上。此外,如果網際網路資訊服務 (IIS) 使用的是 Windows 整合驗證 (亦稱為 NTLM 驗證),則伺服器會驗證用戶端,然後將 IIS 能夠驗證的識別傳回到用戶端上。最後,您可以將用戶端組態檔中的 URL 變更為使用 "https" 做為通訊協定配置,並將 IIS 設為針對該虛擬目錄要求使用 Secure Sockets Layer (SSL) 加密以協助保護您的 Web 服務 (範例不會示範此處理序)。

c2swb8ah.Caution(zh-tw,VS.100).gif注意:
.NET Framework 遠端處理依預設不進行驗證或加密。因此,建議您採取所有必要的步驟,以確認用戶端或伺服器的識別 (Identity),然後再與其進行遠端互動。由於 .NET Framework 遠端處理應用程式需要 FullTrust 權限才能執行,所以如果某個未經授權的用戶端被授與伺服器的存取權,該用戶端就可以執行程式碼,如同它已完全受信任。請務必驗證您的端點並加密通訊資料流,方法包括在 IIS 中裝載遠端型別或建置自訂通道接收組來進行這項工作。

若要編譯和執行這個範例

  1. 將所有檔案儲存在名為 RemoteIIS 的目錄中。

  2. 在命令提示字元下輸入下列命令,以編譯整個範例:

    vbc /t:library ServiceClass.vb
    vbc /r:System.Runtime.Remoting.dll /r:ServiceClass.dll Client.vb
    
    csc /t:library ServiceClass.cs
    csc /r:System.Runtime.Remoting.dll /r:ServiceClass.dll Client.cs
    
  3. 建立 \bin 子目錄並將 ServiceClass.dll 複製到該目錄。

  4. 使用 IIS 建立應用程式。將應用程式別名稱為 "HttpBinary" 並將來源目錄設為 "RemoteIIS" 目錄。

  5. 將此虛擬目錄的驗證方法設為整合式 Windows 驗證 (先前稱為 NTLM 驗證)。如果選取了匿名存取,則 HttpContext.Current.User.Identity.Name 將為 null,而且 GetServerString 將會傳回 "***unavailable***" 的使用者別名。若要避免發生這種情況,請取消選取匿名存取。

  6. 請確定已啟動 IIS;在命令提示字元下,於 "RemoteIIS" 目錄中輸入 client。

這個應用程式會在單一電腦或網路上執行。如果您要透過網路執行這個應用程式,您必須以遠端電腦的名稱取代用戶端組態中的 "localhost"

ServiceClass

Imports System
Imports System.Runtime.Remoting
Imports System.Web

Public Interface IService
    Function GetServerTime() As DateTime
    Function GetServerString() As String
End Interface

Public Class ServiceClass
    Inherits MarshalByRefObject
    Implements IService

    Private InstanceHash As Integer

    Public Sub New()
        InstanceHash = Me.GetHashCode()
    End Sub

    Public Function GetServerTime() As Date Implements IService.GetServerTime
        Return DateTime.Now
    End Function

    Public Function GetServerString() As String Implements IService.GetServerString
        ' Use the HttpContext to acquire what IIS thinks the client's identity is.
        Dim temp As String = HttpContext.Current.User.Identity.Name

        If (temp Is Nothing Or temp.Equals(String.Empty)) Then
            temp = "**unavailable**"
        End If

        Return "Hi there. You are being served by instance number: " _
            & InstanceHash.ToString() _
            & ". Your alias is: " _
            & temp
    End Function
End Class
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Threading;
using System.Web;

public interface IService
{
   DateTime GetServerTime();
   string GetServerString();
}

// IService exists to demonstrate the possibility of publishing only the interface.
public class ServiceClass : MarshalByRefObject, IService
{
   private int InstanceHash;

   public ServiceClass()
   {
      InstanceHash = this.GetHashCode();
   }

   public DateTime GetServerTime()
   {
      return DateTime.Now;
   }

   public string GetServerString()
   {
      // Use the HttpContext to acquire what IIS thinks the client's identity is.
      string temp = HttpContext.Current.User.Identity.Name;
      if (temp == null || temp.Equals(string.Empty))
         temp = "**unavailable**";
      return "Hi there. You are being served by instance number: " 
         + InstanceHash.ToString() 
         + ". Your alias is: " 
         + temp;
   }
}

Web.config

<configuration>
   <system.runtime.remoting>
      <application>
         <service>
            <wellknown 
               mode="SingleCall" objectUri="SAService.rem"
               type="ServiceClass, ServiceClass"/>
         </service>
         <channels>
            <channel ref="http"/>
         </channels>
      </application>
   </system.runtime.remoting>
</configuration>

Client

Imports System
Imports System.Collections
Imports System.Net
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Security.Principal

Public Class Client

    Public Shared Sub Main()
        ' Tells the system about the remote object and customizes the HttpChannel
        ' to use the binary formatter (which understands that base64 encoding is needed).
        RemotingConfiguration.Configure("Client.exe.config", False)

        ' New proxy for the ServiceClass.
        ' If you publish only the IService interface, you must use Activator.GetObject.
        Dim service As ServiceClass = New ServiceClass()

        ' Programmatically customizes the properties given to the channel. This sample uses the
        '  application configuration file.
        Dim Props As IDictionary = ChannelServices.GetChannelSinkProperties(service)
        Props.Item("credentials") = CredentialCache.DefaultCredentials

        ' Reports the client identity name.
        Console.WriteLine("ConsoleIdentity: " & WindowsIdentity.GetCurrent().Name)

        ' Writes what the server returned.
        Console.WriteLine("The server says : " & service.GetServerString())
        Console.WriteLine("Server time is: " & service.GetServerTime())
    End Sub
End Class
using System;
using System.Collections;
using System.Net;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Security.Principal;

class Client
{
    static void Main(string[] args)
    {
        // Tells the system about the remote object and customizes the HttpChannel
        // to use the binary formatter (which understands that base64 encoding is needed).
        RemotingConfiguration.Configure("Client.exe.config", false);

        // New proxy for the ServiceClass.
        // If you publish only the IService interface, you must use Activator.GetObject.
        ServiceClass service = new ServiceClass();

        // Programmatically customizes the properties given to the channel. This sample uses the
        // application configuration file.
        IDictionary Props = ChannelServices.GetChannelSinkProperties(service);
        Props["credentials"] = CredentialCache.DefaultCredentials;

        // Reports the client identity name.
        Console.WriteLine("ConsoleIdentity: " + WindowsIdentity.GetCurrent().Name);

        // Writes what the server returned.
        Console.WriteLine("The server says : " + service.GetServerString());
        Console.WriteLine("Server time is: " + service.GetServerTime());
    }
}

Client.exe.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <channels>
        <channel ref="http" useDefaultCredentials="true" port="0">
          <clientProviders>
            <formatter 
               ref="binary"
                  />
          </clientProviders>
        </channel>
      </channels>
      <client>
        <wellknown 
           url="https://localhost:80/HttpBinary/SAService.rem"
           type="ServiceClass, ServiceClass"
            />
      </client>
    </application>
  </system.runtime.remoting>
</configuration>

另請參閱

概念

遠端應用程式的組態
在網際網路資訊服務 (IIS) 中裝載遠端物件

其他資源

遠端處理範例