我已经创建了在网络上运行的WCF服务。当客户端尝试调用其中一个函数时(我正在调试并在调用进入时逐步检查),WCF服务中定义的所有变量都是NULL(例如,记录器类是在服务创建上定义的是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();
答案 0 :(得分:1)
WCF服务应该尽可能无状态。我的意思是如果你想创建Logger类,我建议你创建一个静态变量。除此之外,您定义的任何变量都应该严格地位于方法的本地。话虽如此,我可能会误解问题,代码片段将有助于更好地理解问题。如果您确实想要在课程中初始化并拥有成员,您可能需要查看此内容:Sessions, Instancing and Concurrency。