Med*_*000 1 c# service wcf windows-services
这个问题与本问题直接相关,但我认为因为主题不同我会为当前问题开始一个新问题.我有一个WCF服务,一个服务和一个GUI.GUI将一个int传递给WCF,该WCF应该将其存入List<int> IntList
; 然后在服务中我想访问列表.问题是,当我尝试添加到WCF服务中的列表时,我收到"检测到无法访问的代码"警告,并且当我通过它进行调试时,添加行被完全跳过.如何让这个列表"可以访问"?
下面是WCF代码,对WCF的GUI调用以及使用List<>
from WCF 的服务:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
GUI:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
服务:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
所以你有了:
int result = n1 + n2;
return result; // <-- Return statement
m_myValues.Add(result); // <-- This code can never be reached!
Run Code Online (Sandbox Code Playgroud)
既然m_myValues.Add()
不result
以任何方式改变状态,为什么不翻转这些线:
int result = n1 + n2;
m_myValues.Add(result);
return result;
Run Code Online (Sandbox Code Playgroud)