user1782815
user1782815

Reputation: 195

Call to a WCF service, internal variables are NULL

I've created a WCF service running on a network. When a client tries to call one of its function (I'm currently debugging and checking step-by-step when the call comes in), all of the variables defined in the WCF service are NULL (e.g the logger class which is defined on service creation is NULL).

I've tried to search for terms like these, but it ends up empty. Does anyone have experience regarding this issue? The WCF service is created on a different thread than the server application.

Edit:

Client code:

[ServiceContract]
public interface IInterface
{
    [OperationContract]
    string Ping();
}

public class Interface : IInterface
{
    public string Ping()
    {
        Logger.Debug("Interface Received Ping through WCF"); //Exception, logger is NULL
        return "Pong";
    }

    public void OpenHost()
    {
        try
        {
            Logger.Info("Interface OpenHost - WCF thread is initializing with ID:" + Thread.CurrentThread.ManagedThreadId.ToString());

            string MachineAddress = string.Format("{0}.{1}", System.Net.Dns.GetHostName(),System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName);
            baseAddress = new Uri(String.Format("http://{0}:8732/WCF/",MachineAddress) + "Interface_" + System.Diagnostics.Process.GetCurrentProcess().SessionId.ToString() + "_" + System.Diagnostics.Process.GetCurrentProcess().Id.ToString() + "/");

            Logger.Info("Interface OpenHost - WCF host will be started with baseAddress " + baseAddress.ToString());

            serviceHost = new ServiceHost(typeof(Interface), baseAddress);
            serviceHost.AddServiceEndpoint(typeof(IInterface), new BasicHttpBinding(), baseAddress);


            serviceHost.Open();
            Logger.Info("Interface OpenHost - WCF host ready and listening for incomming requests");
        }
        catch (Exception ex) { HandleException(ex, "OpenHost"); }
    }

Client code:

                string sWCFUrl = WCFUrl + "?wsdl";
            //Create object of the Binding
            System.ServiceModel.Channels.Binding binding = new System.ServiceModel.BasicHttpBinding();

            //Create endpointAddress of the Service
            System.ServiceModel.EndpointAddress endpointAddress = new System.ServiceModel.EndpointAddress(sWCFUrl);
            //Create Client of the Service
            InterfaceService.InterfaceClient cc = new InterfaceService.InterfaceClient(binding, endpointAddress);

            //Call Service method using ServiceClient
            string ss = cc.Ping();

Upvotes: 1

Views: 860

Answers (1)

Aditya Patil
Aditya Patil

Reputation: 558

WCF service should be as stateless as possible. What I mean is if you want to create Logger class, I suggest you create a static variable. Other than that, any variables that you define should be strictly local to the method. Having said that, I could be misinterpreting the problem, a code snippet will help better understand the issue. If you do want to initialize and have members in the class you may want to take a look at this: Sessions, Instancing and Concurrency.

Upvotes: 1

Related Questions