Question

My WCF service has an OperationContract method (getMyObject()), which needs to use a global static variable... Why does the WCF operation always throw an error saying the global variable is null?
- I have stepped through the WCF service host with a separate debugger, and I know the global variable is not null! - but to the client, it appears null!

The requesting client:

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(); 
    }
}

The servicehost:

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;
            }
        }
    }
}

The ServiceContract interface:

[ServiceContract(Namespace = "http://mylocalhost/myService")]
public interface IGlobal_ServiceContract { 
    [OperationContract]
    List<MyObject> getMyObject();
}

The ServiceContract object:

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; 
        }            
    }
}

The MyObject:

namespace my_WindowsService {
    public class MyObject(){
        public int x = 0;
    }
}

Thanks!

Was it helpful?

Solution

It seems to me like you're trying to pass objects static property through service call? If so, than that's simply not possible. Statics don't get serialized, because serialization is instance based.

You could (probably) pass static by encapsulating it in a non-static property like this:

[DataMember]
public List<MyObject> myObjectProp
{
    get
    {
        return Global.myObject;
    }

    set
    {
        Global.myObject = value;
    }   
}

Having said that, whenever you encounter this kind of hack, it's probably a good idea to reconsider your design - does your field really need to be static? Maybe you need to transfer some other object and only put it's results in your singleton/static class?

For example, I'm guessing you're using myObject as a sort of cache - you could put the cache in a singleton class (this would make it transferrable via WCF), and on client initialize it with what you got from service side. There are of course other valid options but and I'm pretty sure that using static isn't a good choice here...

OTHER TIPS

Try to decorate MyObject class definition with [DataContract] attribute and its members with [DataMember] attribute

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top