错误信息:
Service cannot be started. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidCastException: [A]HouseInfo.Common.Request cannot be cast to [B]HouseInfo.Common.Request. Type A originates from 'HouseInfo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'D:HouseInfo应用Debug/HouseInfo.dll'. Type B originates from 'HouseInfo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'D:HouseInfo应用DebugHouseInfo.dll'. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterRequest.Write5
代码:
string FullName = "HouseInfo.StartUp"; //Assembly assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "/HouseInfo.dll"); Assembly assembly = Assembly.Load("HouseInfo"); Type type = assembly.GetType(FullName); //创建该对象的实例,object类型,参数(名称空间+类) object instance = assembly.CreateInstance(FullName); System.Reflection.MethodInfo method = type.GetMethod("Start"); method.Invoke(instance, null);
使用直接加载dll的就会报上面的那种错误(不需要添加引用,只要dll跟调用的应用在一起就可以了)
原因是系统将反射调用的放到了另外的(LoadNeither)环境里面,而不是默认的(Default)环境,跟调用方不在一个环境里面
Window服务注意:
1.window服务时间间隔,如果时间间隔设置成1秒,那么调用的操作大于1秒,就会停止
2.替换window服务反射调用的dll,只要停止掉服务就可以了
3.修改编译Window服务源码,不需要卸载window服务,只要停止就可以
比如:
window服务调用一个配置信息,修改配置信息,发现window服务调用的还是原来的,暂停以后再次启动,就会调用新的配置信息