我正在使用内部编写的.NET远程服务,该服务具有以下配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="7361">
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
<lifetime leaseTime="5M" sponsorshipTimeout="2M" renewOnCallTime="2M" leaseManagerPollTime="10S" />
<service>
<activated type="MyCompany.MyNamespace.MyClass, MyAssembly.MyClass" />
</service>
</application>
</system.runtime.remoting>
</configuration>
我使用以下代码在远程服务器上创建对象的实例:
UrlAttribute att = new UrlAttribute("tcp://remoteserver:7361/");
MyCompany.MyNamespace.MyClass o = (MyCompany.MyNamespace.MyClass)
Activator.CreateInstance(typeof(MyCompany.MyNamespace.MyClass), new object[] {}, new object[] {att});
比我创建了MyCompany.MyNamespace.MyClass
的两个实现更了解这一点的人。其中一个位于MyCompany.MyAssemblyProxy.dll
,另一个位于MyCompany.MyAssembly.dll
。
代理的类层次结构如下所示:
[Serializable, SoapType(XmlNamespace=@"http://schemas.microsoft.com/clr/nsassem/MyCompany.Transaction/MyCompany.MyAssembly%2C%20Version%3D1.0.7300.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D541b4aff0f04b60a", XmlTypeNamespace=@"http://schemas.microsoft.com/clr/nsassem/MyCompany.Transaction/MyCompany.MyAssembly%2C%20Version%3D1.0.7300.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D541b4aff0f04b60a")]
public class MyClassBase : System.MarshalByRefObject
{
// ... etc ...
}
[Serializable, SoapType(XmlNamespace=@"http://schemas.microsoft.com/clr/nsassem/MyCompany.Transaction/MyCompany.MyAssembly%2C%20Version%3D1.0.7300.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D541b4aff0f04b60a", XmlTypeNamespace=@"http://schemas.microsoft.com/clr/nsassem/MyCompany.Transaction/MyCompany.MyAssembly%2C%20Version%3D1.0.7300.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D541b4aff0f04b60a")]
public class MyClass : MyClassBase, MyCompany.Services.IMyAuthenticate
{
// ... etc ...
}
代理中的类看起来非常像MyCompany.MyAssembly.dll
中实现的空接口副本。客户端应用程序引用MyCompany.MyAssemblyProxy.dll
,而服务器(通过.NET远程处理的所有隐形魔法)以某种方式最终从MyClass
创建MyCompany.MyAssembly.dll
的实例。
请注意,代理属性中的版本硬编码为1.0.7300.0,但我们正在构建的文件的AssemblyVersion大约为8.0.0.999。在以下任何一种情况下,一切正常:
MyCompany.MyAssemblyProxy.dll
,MyCompany.MyAssembly.dll
安装在服务器的GAC中,服务器上的版本与客户端上的版本相同或不同,或者MyCompany.MyAssembly.dll
的相同版本,而不是引用MyCompany.MyAssemblyProxy.dll
。我们的代码都不在GAC中。在以下时间失败:
MyCompany.MyAssemblyProxy.dll
且服务器未在GAC中安装MyCompany.MyAssembly.dll
(错误消息类似于Cannot load type 'soap:MyClass, http://schemas.microsoft.com/clr/nsassem/MyCompany.Transaction/MyCompany.MyAssembly%2C%20Version%3D1.0.7300.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D541b4aff0f04b60a'.
),或者MyCompany.MyAssembly.dll
,服务器在GAC中的版本为MyCompany.MyAssembly.dll
。我正在尝试保留不同版本的灵活性,同时无需将程序集安装到GAC中,但我无法弄清楚如何。