我有带有 WCF 服务的客户端-服务器应用程序,我需要以某种状态从客户端向服务器发送 ComObject,呈现为 COM 接口。ComObject 不可序列化,因此我需要在服务器端创建一个新实例并恢复正确的状态。
如何在客户端获取 ComObject 的这种状态并在服务器端创建接口实现实例?
ComObject 的定义:
public class SyncSessionContext
{
...
private CoreInterop.ISyncSessionState rawState;
...
}
COM接口定义
internal static class CoreInterop
{
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("b8a940fe-9f01-483b-9434-c37d361225d9")]
[ComImport]
public interface ISyncSessionState
{
[MethodImpl(MethodImplOptions.InternalCall | MethodImplOptions.PreserveSig, MethodCodeType = MethodCodeType.Runtime)]
int GetInfoForChangeApplication([MarshalAs(UnmanagedType.LPArray), In, Out] byte[] ppbChangeApplierInfo, [In, Out] ref uint pcbChangeApplierInfo);
...other methods
}
}
我的客户端代码:
public override void BeginSession(SyncProviderPosition position, SyncSessionContext syncSessionContext)
{
var field = typeof(SyncSessionContext).GetField("rawState", BindingFlags.Instance | BindingFlags.NonPublic);
// Nonserializable correct instance
var rawState = field.GetValue(syncSessionContext);
//extract state...
var state = ?????
//calling wcf service
proxy.BeginSession(position, state);
}
我的服务器端代码:
public void BeginSession(SyncProviderPosition position, object state)
{
//initializing and restoring state
var rawState = ?????
syncSessionContext = new SyncSessionContext(IdFormats(), null);
var field = typeof(SyncSessionContext).GetField("rawState", BindingFlags.Instance | BindingFlags.NonPublic);
field.SetValue(syncSessionContext, rawState);
KnowledgeSyncProvider.BeginSession(position, syncSessionContext);
}