这个问题与这个问题直接相关,但我认为因为主题不同,我会为当前问题提出一个新问题。我有一个 WCF 服务、一个服务和一个 GUI。GUI 将一个 int 传递给 WCF,WCF 应该将它存放到List<int> IntList
; 然后在我想访问列表的服务中。问题是,当我尝试添加到 WCF 服务中的列表时,我收到“检测到无法访问的代码”警告,并且当我通过它进行调试时,添加行被完全跳过。我怎样才能让这个列表“可达”?
下面是 WCF 代码、对 WCF 的 GUI 调用以及使用List<>
来自 WCF 的服务:
周转基金:
[ServiceContract(Namespace = "http://CalcRAService")]
public interface ICalculator
{
[OperationContract]
int Add(int n1, int n2);
[OperationContract]
List<int> GetAllNumbers();
}
// Implement the ICalculator service contract in a service class.
public class CalculatorService : ICalculator
{
public List<int> m_myValues = new List<int>();
// Implement the ICalculator methods.
public int Add(int n1,int n2)
{
int result = n1 + n2;
return result;
m_myValues.Add(result);
}
public List<int> GetAllNumbers()
{
return m_myValues;
}
}
图形用户界面:
private void button1_Click(object sender, EventArgs e)
{
using (ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/MyServiceAddress")))
{
ICalculator proxy = factory.CreateChannel();
int trouble = proxy.Add((int)NUD.Value,(int)NUD.Value);
}
}
服务:
protected override void OnStart(string[] args)
{
if (mHost != null)
{
mHost.Close();
}
mHost = new ServiceHost(typeof(CalculatorService), new Uri("net.pipe://localhost"));
mHost.AddServiceEndpoint(typeof(ICalculator), new NetNamedPipeBinding(), "MyServiceAddress");
mHost.Open();
using (ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/MyServiceAddress")))
{
ICalculator proxy = factory.CreateChannel();
BigList.AddRange(proxy.GetAllNumbers());
}
}