为什么在我的 Web 服务中调用此方法时出现错误“无法将类型 'void' 隐式转换为 'string'?
Why am I getting the error "cannot implicitly convert type 'void' to 'string' when calling this method in my Web Service?
所以我遇到的问题是,当我在我的 WCF 服务引用中调用方法时,我得到了带红色下划线的代码 "Cannot implicitly convert type 'void' to 'string'"。最初,我假设我不小心将 webservice 方法设置为 public void GetData()
但是经过进一步审查,我知道事实并非如此。我尝试调用的 WCF 方法是:
public string GetData()
{
return "StringRecieved";
}
Operation合约定义如下:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData();
}
而我调用该方法的地方在不同的应用程序中。
Service1Client.EndpointConfiguration endpointConfiguration = new Service1Client.EndpointConfiguration();
Service1Client service1Client;
service1Client = new Service1Client(endpointConfiguration);
string Data = service1Client.GetDataAsync(); //This is where I'm getting my error.
为什么会出现该错误?该方法被设置为一个字符串,我正在尝试将其分配给一个字符串。
对于异步,您需要添加事件 GetDataCompleted 和方法。
service1Client.GetDataCompleted += service1Client_GetDataCompleted;
public void service1Client_GetDataCompleted(object sender, wsService1.GetDataCompletedEventArgs e)
{
string Data = e.Result.ToString();
}
所以我遇到的问题是,当我在我的 WCF 服务引用中调用方法时,我得到了带红色下划线的代码 "Cannot implicitly convert type 'void' to 'string'"。最初,我假设我不小心将 webservice 方法设置为 public void GetData()
但是经过进一步审查,我知道事实并非如此。我尝试调用的 WCF 方法是:
public string GetData()
{
return "StringRecieved";
}
Operation合约定义如下:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData();
}
而我调用该方法的地方在不同的应用程序中。
Service1Client.EndpointConfiguration endpointConfiguration = new Service1Client.EndpointConfiguration();
Service1Client service1Client;
service1Client = new Service1Client(endpointConfiguration);
string Data = service1Client.GetDataAsync(); //This is where I'm getting my error.
为什么会出现该错误?该方法被设置为一个字符串,我正在尝试将其分配给一个字符串。
对于异步,您需要添加事件 GetDataCompleted 和方法。
service1Client.GetDataCompleted += service1Client_GetDataCompleted;
public void service1Client_GetDataCompleted(object sender, wsService1.GetDataCompletedEventArgs e)
{
string Data = e.Result.ToString();
}