调用WCF服务时,内部变量为NULL

本文关键字:变量 NULL 内部 WCF 服务 调用 | 更新日期: 2023-09-27 18:10:00

我创建了一个运行在网络上的WCF服务。当客户端试图调用它的一个函数时(我目前正在调试和检查调用进入时的一步一步),WCF服务中定义的所有变量都为NULL(例如,在服务创建时定义的logger类为NULL)。

我试着搜索这样的术语,但它最终是空的。有人有这方面的经验吗?WCF服务创建在与服务器应用程序不同的线程上。

编辑:

客户机代码:

[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"); }
    }
客户机代码:

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

调用WCF服务时,内部变量为NULL

WCF服务应该尽可能是无状态的。我的意思是,如果您想创建Logger类,我建议您创建一个静态变量。除此之外,您定义的任何变量都应该严格局限于方法。话虽如此,我可能误解了问题,但代码片段将有助于更好地理解问题。如果你想在类中初始化和拥有成员,你可能想看看这个:会话,实例化和并发。