你用反射创建的实例(只不过命名空间是远程的)是存储在本地的,代码也是运行在本地的,自然是客户端时间
你可以查一下Remoting资料,或者直接用wcf更省事
TcpChannel channel = new TcpChannel(端口号,序列化提供者);
ChannelServices.RegisterChannel(channel);
http://www.cnblogs.com/wayfarer/archive/2004/08/05/30437.html
你可以查一下Remoting资料,或者直接用wcf更省事
TcpChannel channel = new TcpChannel(端口号,序列化提供者);
ChannelServices.RegisterChannel(channel);
http://www.cnblogs.com/wayfarer/archive/2004/08/05/30437.html
呼叫 Proxy 来传送讯息至远程对象。 没有任何信息会经网络传送,直到在 Proxy 上呼叫某一方法。
应该比较单纯吧
WCF用的通道用接口,而接口不允许有属性,我用一个方法来返回一个对象,然后在本地调用这个对象的方法,还是执行的本地代码。服务端代码 [ServiceContract]
public interface IHello
{
[OperationContract]
string gettime(); [OperationContract]
c1 testc();
}
public class HelloWorld : IHello
{
public string gettime()
{
return DateTime.Now.ToString();
} public c1 testc()
{
return new c1();
}
} public class c1
{
public string gettime()
{
return DateTime.Now.ToString();
}
}
客户端代码 [ServiceContract]
public interface IHello
{
[OperationContract]
string gettime(); [OperationContract]
c1 testc();
} public class c1
{
public string gettime()
{
return "本地";
}
}
void main()
{
ServiceEndpoint httpEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IHello))
, new BasicHttpBinding(), new EndpointAddress(@"http://localhost:4002/HelloService/Svc")); using (ChannelFactory<IHello> factory = new ChannelFactory<IHello>(httpEndpoint))
{ IHello service = factory.CreateChannel();
MessageBox.Show(service.gettime());
MessageBox.Show(service.testc().gettime());}
public class c1 : MarshalByRefObject //<--
{ //获取本机时间
public string gettime()
{
return DateTime.Now.ToString();
}
public string Test()
{
return AppDomain.CurrentDomain.FriendlyName;
}
}void Test()
{
AppDomain domain = AppDomain.CreateDomain("hello new domain");
testRemoteServer proxy = (testRemoteServer)domain.CreateInstanceAndUnwrap(typeof(testRemoteServer).Assembly.FullName, typeof(testRemoteServer).FullName);
string s = proxy.TestC.gettime(); // <-- "hello new domain"
}
另外,如果c1不能继承自MarshalByRefObject,你也可以在testRemoteServer中添加一个方法,在该方法中调用return this.TestC.gettime();
-------------------------------------------------------------这个我试过了,依然执行的本地代码,并非远程的。而且我用WCF也试过,你看上面的WCF例子就是你说的这样的,也是执行本地代码。
客户端和服务端一句代码也不用改,服务端的MarshalByRefObject分配信道,提供远程对象,类库的每个类也都必须继承自MarshalByRefObject,如果不继承,tr.Testc.gettime()就在本地执行,继承了,tr.Testc.gettime()就在服务端执行。原来这么简单就解决了,困扰了我两天。gomoku 的一句提醒了我。谢谢。