.NET Remoting configuration files allow you to specify parameters for most aspects of the remoting framework. These files can define tasks as simple as registering a channel and specifying a Type as a server-activated object, or can be as complex as defining a whole chain of IMessageSinks with custom properties.
通过.Net Remoting配置文件可以为Remote Objects设定许多参数,如Channel、SAO服务端激活对象类型(Singleton/SingleCall)等等,方便以后在不用修改代码或重新编译的情况下,改变Remote Objects的行为。
1,如下是Server端典型的Remoting配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="http"/>
</channels>
<service>
<wellknown mode="Singleton"
type="ComponentHost.CustomerManager, ComponentHost"
objectUri="CustomerManager.soap" />
</service>
</application>
</system.runtime.remoting>
</configuration>
(1)当Remote Objects部署在Console/Windows Form、Windows Services下时(上面的配置文件channel需要设置port属性),相应Server端声明Remote Objects的代码可以简化为:
string filename = "server.exe.config";
RemotingConfiguration.Configure(filename);
(2)如果Remote Objects部署在IIS时,根本就不需要任何代码声明。但是需要将上述配置文件命名为:web.config,并且将Remote Objects的DLL文件安置在web application的BIN文件夹。
一般在实际应用中,基本上将Remote Objects部署在IIS环境中,好处是(I)不需要编写额外的代码;(II)只要启动机器,远程对象就启动了。不需要你半夜三更跑到公司去登录,然后启动发生故障的远程服务;(III)容易与IIS认证服务进行集成;(IV)可能还有更多优点,我现在没有想到。
(3)如果需要声明多个远程对象,只需要在<service>与</service>之间添加相应的Remote Objects配置信息即可。
(4)另外需要注意type属性为:<namespace>.<class>, <assembly>
2,如下是Client端典型的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown type="ComponentHost.CustomerManager, RemotingTest"
url="http://localhost/ComponentHost/CustomerManager.soap" />
</client>
</application>
</system.runtime.remoting>
</configuration>
要注意type属性的设定:<namespace>.<class>, <assembly>
如果Client通过SoapSuds产生Remote Objects的元数据assembly,或者是Shared Assembly(如Interface或Abstract Class),这里<assembly>则为上述assembly的名称。
如果是通过SoapSuds产生Source code,则<assembly>为Client应用程序名(无exe后缀)。
同时,Client端application调用Remote Objects时,可以省掉:注册通道、Activator.GetObject()/RemotingConfiguration.RegisterActivatedServiceType()等代码,取而代之的代码为:
string filename = “clientApplication.exe.config”;
RemotingConfiguration.Configure(filename);
下面通过new来创建Remote Object实例。
3,标准的.Net Remoting Configuration配置文件
MSDN中有.Net Remoting Configuration file中全部元素/属性的完整的详细说明,需要的时候再查阅了。一般情况下,知道下面这些属性就够用了。
<configuration>
<system.runtime.remoting>
<application>
<lifetime /> ―― 配置Remote Objects生存期的信息
<channels /> ―― 配置与远程对象进行通信的信道
<service />
<client />
</application>
</system.runtime.remoting>
</configuration>
简单说明:
(1)<service> ―― 仅在Server端配置
<service>
<wellknown /> ―― 配置要发布的SAO(已知)对象的信息
<activated /> ―― 配置要发布的CAO客户端激活对象的信息
</service>
(2)<client> ―― 仅在Client端配置,与Server端<service>对应
<client>
<wellknown />
<activated />
</client>
When using CAOs, the <client> property has to specify the URI to the server for all underlying <activated> entries.
Note:When using CAOs from more than one server, you have to create several <client> properties in your configuration file.
当调用CAO远程对象时,必须设定<client>的url属性。如果CAO来自不同的Server,则需要在配置文件中定义多个<client>。如下所示:
<client url="http://localhost/MyServer>
<activated type="Server.MyRemote, Client" />
</client>
4,定制Client/Server Channel元素
(1)Client Side
<channel ref="http">
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
其中,formatter ref=”binary” or “soap”。formatter ref指要在通道上发送的消息格式,在此示例中为二进制,以增强性能。
(2)Server Side
<channel ref="http">
<serverProviders>
<provider ref="wsdl" />
<formatter ref="binary" typeFileterLevel="Full" />
<formatter ref="soap" typeFileterLevel="Full" />
</serverProviders>
</channels>
typeFilterLevel表示当前自动反序列化级别,支持的值包括 Low(默认值)和 Full。
1. Ingo Rammer, Advanced .Net Remoting.
2. MSDN
Reference:
比如clientApplication.config就可以用了!前段时间也遇到这个问题。 回复
*
为什么要对同一应用程序同时配置<service />和<client />?有什么特殊的需求吗?
<service> ―― 仅在Server端配置
<client> ―― 仅在Client端配置,与Server端<service>对应 回复
*
对Server端而言,当然需要指定端口了,否则Client端无法访问.
(1)当Remote Objects部署在Console/Windows Form、Windows Services下时(上面的配置文件channel需要设置port属性)。
<channels>
<channel ref="http" port="8083">
<serverProviders>
<provider ref="wsdl" />
<formatter ref="soap" typeFilterLevel="Full" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
(2)当由IIS承载Remote Objects时,port就是由IIS设定的,默认为80。
那当有多个服务的时候,
看看client端<client>元素配置内容就知道了,如下:
<client>
<wellknown type="ComponentHost.CustomerManager, RemotingTest"
url=" http://localhost/ComponentHost/CustomerManager.soap" />
</client>
url属性中包括http(channel),port(示例为80),Application Name(ComponentHost),服务对象标识(CustomerManager.soap)
**全有了,一个都不能少。
回复
<service>
<wellknown mode="Singleton" type="ComponentHost.CustomerManager, ComponentHost"
objectUri="CustomerManager.soap" />
</service>
<service>
<wellknown mode="Singleton" type="ComponentHost.CustomerManagerOne, ComponentHost"
objectUri="CustomerManagerOne.soap" />
</service>
<service>
<wellknown mode="Singleton" type="ComponentHost.CustomerManagerTwo, ComponentHost"
objectUri="CustomerManagerTwo.soap" />
</service>
<channels>
<channel ref="http" port="8081">
<serverProviders>
<provider ref="wsdl" />
<formatter ref="soap" typeFilterLevel="Full" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
<channel ref="http" port="8082">
<serverProviders>
<provider ref="wsdl" />
<formatter ref="soap" typeFilterLevel="Full" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
<channel ref="http" port="8083">
<serverProviders>
<provider ref="wsdl" />
<formatter ref="soap" typeFilterLevel="Full" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
这样写之后,在配置的时候就出现错误了。我不知道当配置多个服务的时候,在什么位置写每个服务对应的port 回复
<service>
<wellknown mode="Singleton" type="ComponentHost.CustomerManager, ComponentHost"
objectUri="CustomerManager.soap" />
<wellknown mode="Singleton" type="ComponentHost.CustomerManagerOne, ComponentHost"
objectUri="CustomerManagerOne.soap" />
......
</service>
应该这样配置多个Remote Objects对象。 回复
***
objectUri="CustomerManager.soap" 只是一个标识而已,不必是某个存在的类,如上面的可以改为objectUri="ILOVEMM.soap"
回复
上面关于IIS承载多个不同的Remote Objects,配置文件的用法是没有问题的,实际的项目就是这样配置的。
http://localhost/AppName/EndPoint.soap?wsdl
通过访问上面的URL来验证Remote Objects是否配置成功。
失败的原因很多:(1)Remote Objects本身存在问题;(2)配置文件存在问题等等。需要具体情况具体分析。
***
同一个Web Site不可以设置不同的port,因此不同服务Remote Objects在同一Web Site必须使用相同的端口。
你可以在IIS中创建多个Web Site,并指定不同的port,就可以实现你的要求了。 回复
的话,好象即使把obj的dll的copy到了客户端也不能用new来产生,只能用Activator.GetObject。是不是只有client actived的object才能用new 来产生proxy的? 回复
Q: 如果remoting object是Singleton 的话,好象即使把obj的dll的copy到了客户端也不能用new来产生,只能用Activator.GetObject。
A: Client可以使用new来创建Remote Objects实例,注意需要在项目中引用该DLL,并且添加相应的namespace.
Q: 是不是只有client actived的object才能用new 来产生proxy的?
A: 不是,CAO and SAO都可以. 回复
可否請教您一個問題﹕
我在用.Net Remoting配置文件的時候老是出現如下錯誤﹐請問是什么問題﹐謝謝﹕
類型 'System.Runtime.Remoting.RemotingException' 的未處理例外狀況發生於 mscorlib.dll
其他資訊: 遠端組態已經失敗,發生例外狀況 System.IO.FileNotFoundException: 找不到檔案或組件名稱 'System.Runtime.Remoting' 或其相依性的其中之一。
檔案名稱: "System.Runtime.Remoting"
at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Boolean isStringized, Evidence assemblySecurity, Boolean throwOnFileNotFound, Assembly locationHint, StackCrawlMark& stackMark)
at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Boolean stringized, Evidence assemblySecurity, StackCrawlMark& stackMark)
at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark)
at System.Reflection.Assembly.Load(String assemblyString)
at System.Runtime.Remoting.RemotingConfigInfo.LoadType(String typeName, String assemblyName)
at System.Runtime.Remoting.RemotingConfigHandler.CreateChannelFromConfigEntry(ChannelEntry entry)
at System.Runtime.Remoting.RemotingConfigHandler.ConfigureChannels(RemotingXmlConfigFileData configData)
at System.Runtime.Remoting.RemotingConfigHandler.ConfigureRemoting(RemotingXmlConfigFileData configData)
=== Pre-bind state information ===
LOG: DisplayName = System.Runtime.Remoting
(Partial)
LOG: Appbase = K:/WebSite/Callback/Server/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: Found application configuration file (K:/WebSite/Callback/Server/bin/Debug/Server.exe.config).
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Post-policy reference: System.Runtime.Remoting
LOG: Attempting download of new URL file:///K:/WebSite/Callback/Server/bin/Debug/System.Runtime.Remoting.DLL.
LOG: Attempting download of new URL file:///K:/WebSite/Callback/Server/bin/Debug/System.Runtime.Remoting/System.Runtime.Remoting.DLL.
LOG: Attempting download of new URL file:///K:/WebSite/Callback/Server/bin/Debug/System.Runtime.Remoting.EXE.
LOG: Attempting download of new URL file:///K:/WebSite/Callback/Server/bin/Debug/System.Runtime.Remoting/System.Runtime.Remoting.EXE.
。
我的server 端部分代碼﹕
class Server
{
static void Main(string[] args)
{
RemotingConfiguration.Configure("Server.exe.config");
Console.WriteLine("Hit to exit");
Console.ReadLine();
}
}
server 的xml配置文件內容(該文件與Server.exe在相同目錄﹐且文件名為﹕Server.exe.config)﹕
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="Hello">
<service>
<wellknown mode="SingleCall" type="Wrox.ProfessionalCSharp.Hello, RemoteHello"
objectUri="Hi" />
</service>
<channels>
<channel type="System.Runtime.Remoting.Channels.Tcp.TcpChannel,
System.Runtime.Remoting" port="6791" />
<channel type="System.Runtime.Remoting.Channels.Http.HttpChannel,
System.Runtime.Remoting" port="6792" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
才學不久﹐如果您有時間幫忙看看是什么問題。
回复
我的例子是C#高級編程一書中的source code,
我將﹕
<channel type="System.Runtime.Remoting.Channels.Http.HttpChannel,
System.Runtime.Remoting" port="6792" />
</channels>
改成﹕
<channel ref="http" port="6792" />
</channels>
就可以了﹐該書中怎么會用上面那種寫法。
有那位用過上面那種寫法嗎?
回复
在ie中输入 http://localhost/SP/Coordinator.rem/RemoteActivationService.rem?wsdl
产生下面的代码。
System.Runtime.Remoting.RemotingException: .Config file D:/Visual Studio Projects/C#/SP/web.config can not be read successfully due to exception
System.Runtime.Remoting.RemotingException: Sink providers must have an element name of 'formatter' or 'provider'.
at System.Runtime.Remoting.Activation.RemotingXmlConfigFileParser.ReportError(String errorStr, RemotingXmlConfigFileData configData)
at System.Runtime.Remoting.Activation.RemotingXmlConfigFileParser.ProcessSinkProviderNode(ConfigNode node, RemotingXmlConfigFileData configData,
Boolean isTemplate, Boolean isServer)
at System.Runtime.Remoting.Activation.RemotingXmlConfigFileParser.ProcessSinkProviderNodes(ConfigNode node, ChannelEntry channelEntry,
RemotingXmlConfigFileData configData, Boolean isServer)
at System.Runtime.Remoting.Activation.RemotingXmlConfigFileParser.ProcessChannelsChannelNode(ConfigNode node, RemotingXmlConfigFileData
configData, Boolean isTemplate)
at System.Runtime.Remoting.Activation.RemotingXmlConfigFileParser.ProcessChannelsNode(ConfigNode node, RemotingXmlConfigFileData configData)
at System.Runtime.Remoting.Activation.RemotingXmlConfigFileParser.ProcessApplicationNode(ConfigNode node, RemotingXmlConfigFileData configData)
at System.Runtime.Remoting.Activation.RemotingXmlConfigFileParser.ParseConfigFile(String filename)
at System.Runtime.Remoting.RemotingConfigHandler.LoadConfigurationFromXmlFile(String filename).
at System.Runtime.Remoting.RemotingConfigHandler.LoadConfigurationFromXmlFile(String filename)
at System.Runtime.Remoting.RemotingConfiguration.Configure(String filename)
at System.Runtime.Remoting.Channels.Http.HttpRemotingHandler.InternalProcessRequest(HttpContext context)
而我的配置文件是这样的:SP/web.config
<?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <system.runtime.remoting>
- <application>
- <service>
<wellknown mode="Singleton" type="Coordinator,PaintCoordinator" objectUri="Coordinator.rem" />
</service>
- <channels>
- <channel ref="http">
- <serverProviders>
<provider ref="wsdl" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
- <clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
我的dll路径
SP/bin/PaintCoordinator.dll
如果我运行客户端的话就会提示说
BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 1835627630.1699884645.
到底是怎么回事呢,老大,帮忙指点指点啊~~~ 回复
# re: .Net Remoting配置文件的用法 2004-11-04 10:40 Casablanca
评论