我的WCF服务有一个OperationContract方法(getMyObject()),需要用到全局静态变量……为什么WCF操作总是报错说全局变量为null?
- 我已经使用单独的调试器单步执行了 WCF 服务主机,并且我知道全局变量不为空!- 但对客户来说,它似乎是空的!
请求客户:
namespace my_Server.stuffPage {
protected void Page_Load(object sender, EventArgs e) {
ChannelFactory<IGlobal_ServiceContract> pipeFactory = new
ChannelFactory<IGlobal_ServiceContract>(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://mylocalhost/myService"));
GlobalProxy = pipeFactory.CreateChannel();
ListMyObjects myObjects = GlobalProxy.getMyObjects();
}
}
服务主机:
namespace my_WindowsService {
public class myServiceHost {
public void startWCFService() {
try {
Uri baseAddress = new Uri("net.pipe://www.myDomain.com/myService");
serviceHost = new ServiceHost(Program.g, baseAddress);
serviceHost.Open();
} catch (Exception ex) {
Debug.WriteLine(DateTime.Now + " my_WindowsService.myServiceHost .startWCFService() failed. " + ex.Message);
throw ex;
}
}
}
}
ServiceContract 接口:
[ServiceContract(Namespace = "http://mylocalhost/myService")]
public interface IGlobal_ServiceContract {
[OperationContract]
List<MyObject> getMyObject();
}
ServiceContract 对象:
namespace my_WindowsService {
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
[DataContract]
public class Global : IGlobal_ServiceContract {
[DataMember]
public static List<MyObject> myObject { get; set; }
public List<myObject> getMyObject() {
return Global.myObject;
}
}
}
我的对象:
namespace my_WindowsService {
public class MyObject(){
public int x = 0;
}
}
谢谢!