远程处理示例:CallContext
该示例使用 CallContext 类和 ILogicalThreadAffinative 接口在跨越应用程序域边界或上下文边界的调用的任一端上的线程之间传递数据。
该应用程序可在单台计算机上运行或通过网络运行。如果要在网络上运行该应用程序,必须用远程计算机的名称替换客户端配置中的“localhost”。
虽然此示例不编译 ServiceClass.vb,但在此处包含它是为了说明 ILogicalThreadAffinative。
警告
.NET 远程处理在默认情况下不进行身份验证或加密。因此,建议您在与客户端或服务器进行远程交互之前,采取任何必要的措施以确认它们的身份。因为 .NET 远程处理应用程序需要 FullTrust 权限才能执行,所以,未经授权的客户端如果被授予访问您服务器的权限,该客户端就可能像完全受信任的客户端一样执行代码。应始终验证终结点的身份并将通信流加密,通过在 Internet 信息服务 (IIS) 中承载远程类型,或者通过生成自定义信道接收对来完成这项工作。
编译此示例
在命令提示符处键入以下命令:
csc /r:System.Runtime.Remoting.dll /t:library ServiceClass.cs
csc /r:System.Runtime.Remoting.dll /r:ServiceClass.dll client.cs
vbc /r:System.Runtime.Remoting.dll /r:ServiceClass.dll clientvb.vb
csc /r:System.Runtime.Remoting.dll /r:ServiceClass.dll server.cs
ServiceClass.cs
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Channels;
public class ContextBoundType : ContextBoundObject{
private DateTime starttime;
public ContextBoundType(){
Console.WriteLine("A ContextBoundType instance has been created.");
starttime = DateTime.Now;
}
~ContextBoundType(){
Console.WriteLine("ContextBoundType being collected after " + (new TimeSpan(DateTime.Now.Ticks - starttime.Ticks)).ToString() + " seconds.");
}
public DateTime GetServerTime(){
Console.WriteLine("Time requested by a client.");
// This call overwrites the client's
// CallContextString.
CallContext.SetData("ServerThreadData", new CallContextString("This is the server side replacement."));
return DateTime.Now;
}
}
// One method of communicating between client and server is
// to use the CallContext. Calling CallContext essentially puts the data
// in a Thread Local Store. This means that the information is available
// to that thread or that "logical" thread (across application domains) only.
[Serializable]
public class CallContextString : ILogicalThreadAffinative{
String _str ="";
public CallContextString(String str){
_str = str;
Console.WriteLine("CallContextString created.");
}
public override String ToString(){
return _str;
}
}
ServiceClass.vb
包含此文件仅是出于文档目的。
Imports System
Imports System.Collections
Imports System.Diagnostics
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Messaging
Imports System.Runtime.Remoting.Contexts
Imports System.Runtime.Remoting.Channels
Public Class ContextBoundType
Inherits ContextBoundObject
Private StartTime As DateTime
Public Sub New()
Console.WriteLine("A ContextBoundType instance has been created. (Constructor)")
StartTime = DateTime.Now
End Sub 'New
Protected Overrides Sub Finalize()
Console.WriteLine("ContextBoundType being collected after " & (New TimeSpan(DateTime.Now.Ticks - starttime.Ticks)).ToString() & " seconds.")
MyBase.Finalize()
End Sub 'Finalize
Public Function GetServerTime() As DateTime
Console.WriteLine("Time requested by a client.")
' This call overwrites the client's
' CallContextString.
CallContext.SetData("ServerThreadData", New CallContextString("This is the server side replacement."))
Return DateTime.Now
End Function 'GetServerTime
End Class 'ContextBoundType
' One of the methods of communicating between client and server is
' to use the CallContext. Calling CallContext essentially puts the data
' in a Thread Local Store. This means that the information is available
' to that thread or that "logical" thread (across application domains) only.
<Serializable> _
Public Class CallContextString
Implements ILogicalThreadAffinative
Private _str As String = ""
Public Sub New(ByVal Contents As String)
_str = Contents
Console.WriteLine("CallContextString created with string ""{0}""", Contents)
End Sub 'New
Public Overrides Function ToString() As String
Return _str
End Function 'ToString
End Class 'CallContextString
ClientVB.vb
Imports System
Imports System.Reflection
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Contexts
Imports System.Runtime.Remoting.Messaging
Public Class ClientProcess
<MTAThread()> _
Public Shared Sub Main()
Dim Channel As New HttpChannel()
ChannelServices.RegisterChannel(Channel)
Dim RemoteType As New WellKnownClientTypeEntry(GetType(ContextBoundType), "https://localhost:8080/TcpCBOService")
RemotingConfiguration.RegisterWellKnownClientType(RemoteType)
' This sets a CallContextDataSlot on this thread. CallContextString
' will convert the ThreadLocalDataStore into a property on the
' message, which will then be merged into the ThreadLocalDataStore
' on the server. This is a nice way to pass information from client
' to server threads and back again. NOTE: This data store has
' logical thread scope. If an application domain has more than one
' thread making calls, each thread will have its own logical
' CallContext, and the server thread will keep each separate.
CallContext.SetData("ServerThreadData", New CallContextString("This is the thread data inserted on the client thread."))
Console.WriteLine("CallContextString prior to the call: " & CallContext.GetData("ServerThreadData").ToString())
Dim service As New ContextBoundType()
Console.WriteLine("Server time is: " & service.GetServerTime().ToLongTimeString())
Console.WriteLine("CallContextString after the call: " & CallContext.GetData("ServerThreadData").ToString())
End Sub 'Main
End Class 'ClientProcess
Client.cs
using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
public class ClientProcess{
public static void Main(string[] Args){
HttpChannel channel = new HttpChannel();
ChannelServices.RegisterChannel(channel);
WellKnownClientTypeEntry remotetype = new WellKnownClientTypeEntry(typeof(ContextBoundType),"https://localhost:8080/TcpCBOService");
RemotingConfiguration.RegisterWellKnownClientType(remotetype);
// This sets a CallContextDataSlot on this thread.
// CallContextString will convert the ThreadLocalDataStore into
// a property on the message, which will then be merged into the
// ThreadLocalDataStore on the server. This is a nice way to pass
// information from client to server threads and back again.
// NOTE: This data store has logical thread scope. If an
// application domain has more than one thread making calls,
// each thread will have its own logical CallContext, and the
// server thread will keep each separate.
CallContext.SetData("ServerThreadData", new CallContextString("This is the thread data inserted on the client thread."));
Console.WriteLine("CallContextString prior to the call: " + CallContext.GetData("ServerThreadData").ToString());
ContextBoundType service = new ContextBoundType();
Console.WriteLine("Server time is: " + service.GetServerTime().ToLongTimeString());
Console.WriteLine("CallContextString after the call: " + CallContext.GetData("ServerThreadData").ToString());
}
}
Server.cs
using System;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Threading;
public class ServerProcess{
public static void Main(string[] Args){
HttpChannel channel = new HttpChannel(8080);
ChannelServices.RegisterChannel(channel);
WellKnownServiceTypeEntry WKSTE = new WellKnownServiceTypeEntry(typeof(ContextBoundType),"TcpCBOService", WellKnownObjectMode.SingleCall);
RemotingConfiguration.RegisterWellKnownServiceType(WKSTE);
Console.WriteLine("Press enter to stop this process.");
Console.ReadLine();
}
}
请参见
参考
CallContext
ILogicalThreadAffinative