最近学remoting开发,宿主为windows服务,代码写好测试时就是启动不了windows服务,手动启动马上又停止了,我知道写的代码有问题。于是改为控制台应用程序,这样的话服务端运行没有错误,但是客户端ITest iTest = (ITest)(Activator.GetObject(typeof(ITest), "tcp://localhost:8999/Tst"));
Response.Write(iTest.Hello());运行到这句提示错误:找不到请求的服务。请高手帮忙看下代码!
我将创建appdomain和assembly放在AssemblyLoader类库里,下面是代码AppDomainLoader类:
public void CreateAppDomain(string dllName)
{
domains = new Hashtable();
AppDomainSetup setup = new AppDomainSetup();
setup.ShadowCopyFiles = "true";
domain = AppDomain.CreateDomain(dllName, null, setup);
domains.Add(dllName, domain);
object[] parms = { dllName };
BindingFlags bindings = BindingFlags.CreateInstance |
BindingFlags.Instance | BindingFlags.Public;
try
{
_remoteLoader = (RemoteLoader)domain.CreateInstanceFromAndUnwrap(
"Explorer.Framework.AssemblyLoader.dll", "Explorer.Framework.AssemblyLoader.RemoteLoader", true, bindings,
null, parms, null, null, null);
}
catch (Exception ex)
{
throw ex;
}
}
public void LoadAssembly(string dllUrl)
{
CreateAppDomain(dllUrl);
_remoteLoader = new RemoteLoader();
_remoteLoader.LoadAssembly(dllUrl);
}
RemoteLoader类:
public class RemoteLoader : MarshalByRefObject, IDisposable
{
private Assembly _assembly;
public RemoteLoader()
{
}
public RemoteLoader(string dllUrl)
{
if (null == _assembly)
{
_assembly = Assembly.LoadFile(dllUrl);
}
}
public void LoadAssembly(string dllurl)
{
_assembly = Assembly.LoadFile(dllurl);
Type[] type = _assembly.GetTypes();
foreach (Type t1 in type)
{
string name = t1.Name;
RemotingConfiguration.RegisterWellKnownServiceType(t1,name, WellKnownObjectMode.SingleCall);
LifetimeServices.LeaseTime = TimeSpan.Zero;
}
}
}
service服务端代码:
private void RemoteStart()
{
_remoteManager = new RemoteManager.RemoteManager();
DataTable dt = _remoteManager.GetAssemblyList();
for (int i = 0; i < dt.Rows.Count; i++)
{
if ("Remoting" == dt.Rows[i]["Mode"].ToString())
{
BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
int port = int.Parse(dt.Rows[i]["Port"].ToString());
TcpChannel tc = new TcpChannel(port);
ChannelServices.RegisterChannel(tc, false);
_appDomainLoader = new AppDomainLoader();
DataTable dTable = _remoteManager.RemoteList();
for (int j = 0; j < dTable.Rows.Count; j++)
{
string url = dTable.Rows[j]["Key"].ToString();
_appDomainLoader.LoadAssembly(url);
}
}
}
}
请高手帮忙找出错误所在,小弟刚学着三个技术,花了两三天也没找出问题,弄得头大死了。
你看下
TcpChannel tc = new TcpChannel(port);
ChannelServices.RegisterChannel(tc, false);
是否注册成功。
可以试着调试下