WCF 和 Assembly.GetExecutingAssembly

WCF and Assembly.GetExecutingAssembly

我需要验证我的 wcf 服务的调用代码是否未被篡改。 调用代码位于客户端 pc 上的一个 dll 文件中。我通过 Assembly.GetExecutingAssembly().

获取调用代码

为了做一些测试,我在我的 wcf 服务中创建了一个名为 GetChecksum 的方法来通过他的校验和验证调用代码的完整性。 但是做好这项工作我需要在调用完成后将正在执行的程序集发送到我的 wcf 服务。

服务器上的缩短代码是这样的:

[ServiceContract]
[ServiceKnownType(typeof(System.Reflection.Assembly))]
public interface IContrato
{
    [OperationContract]
    Byte[] GetChecksum( Object obj, out String debugging);
}


[ServiceKnownType(typeof(System.Reflection.Assembly))]
public class RegisterIndiNT : IContrato
{
    Boolean GetChecksum( Object o, out String debugging )
    {
        // code to cast Object to Assembly and to compute the checksum.
    }
}

缩短的调用代码如下:

Uri uri = new Uri(@"http://mywebserver.com/RegisterIndiNT.svc");
WCFOnlyContract.IContrato proxy = ChannelFactory<WCFOnlyContract.IContrato>.CreateChannel(
                new BasicHttpBinding(), new EndpointAddress(uri));

(proxy as ICommunicationObject).Open();

String debugging = String.Empty;
result = proxy.GetChecksum( Assembly.GetExecutingAssembly(), out debugging);

(proxy as ICommunicationObject).Close();

错误发生在调用 Checksum 方法内部之前。 看起来如果 System.Reflection.Assembly 类型无法序列化。 我已将此类型添加到 knowntypes 属性(接口和 class)但不起作用。我得到下一个例外:

Exception.Message=Error al intentar serializar el parámetro http://tempuri.org/: obj. El mensaje de InnerException fue 'No se espera el tipo 'System.Reflection.RuntimeAssembly' con el nombre de contrato de datos 'RuntimeAssembly:http://schemas.datacontract.org/2004/07/System.Reflection'. Si está usando DataContractSerializer, intente usar DataContractResolver o agregar tipos no conocidos estáticamente a la lista de tipos conocidos (por ejemplo, usando el atributo KnownTypeAttribute o agregándolos a la lista de tipos conocidos que se pasa a DataContractSerializer).'. Consulte InnerException para obtener más información.

我需要程序集的序列化发生在客户端代码之外。 将程序集在客户端序列化为发送到 wcf 后不是解决方案。

非常感谢任何帮助。提前致谢。

你是对的,它没有像你期望的那样序列化。

class 程序集实现 ISerializable

而GetObjectData是这样实现的:

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
  if (info==null)
    throw new ArgumentNullException("info");

  Contract.EndContractBlock();

  UnitySerializationHolder.GetUnitySerializationInfo(info,   UnitySerializationHolder.AssemblyUnity, 
  this.FullName, 
  this);
}

internal static void GetUnitySerializationInfo(
            SerializationInfo info, int unityType, String data, RuntimeAssembly assembly)
{
  info.SetType(typeof(UnitySerializationHolder));
  info.AddValue("Data", data, typeof(String));
  info.AddValue("UnityType", unityType);

  String assemName;

  if (assembly == null) 
     assemName = String.Empty;
  else 
     assemName = assembly.FullName;

  info.AddValue("AssemblyName", assemName);
}

所以只有程序集的名称被序列化而不是代码等

http://referencesource.microsoft.com/#mscorlib/system/reflection/assembly.cs,73b5be5e9c2474b2