最好使用自定义请求对象而不是使用对象数据类型。该请求对象类对于客户端和服务器应该是通用的。然后在客户端中,您可以填写请求并从服务器检索所需的结果。
您的解决方案层次结构最好如下。
namespace ServerProj
{
using System.ServiceModel;
using Common;
[ServiceContract]
public interface IRCommService
{
[OperationContract]
Result SendMessage(string command, CustomRequest data);
}
}
namespace ServerProj
{
using System.Collections.Generic;
using Common;
public class RCommService : IRCommService
{
public Result SendMessage(string command, CustomRequest data)
{ // You can get the value from here
int value = data.MyValue;
Result result = new Result();
List<string> list = new List<string>();
list.Add("Sample");
result.Rsults = list;
return result;
}
}
}
公共程序集中的请求类
namespace Common
{
using System.Runtime.Serialization;
[DataContract]
public class CustomRequest
{
[DataMember]
public int MyValue { get; set; }
}
}
通用程序集中的响应类
namespace Common
{
using System.Collections.Generic;
using System.Runtime.Serialization;
[DataContract]
public class Result
{
[DataMember]
public List<string> Rsults { get; set; }
}
}
然后只需在客户端添加为服务引用。
private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.RCommServiceClient service = new ServiceReference1.RCommServiceClient();
CustomRequest customRequest=new CustomRequest();
customRequest.MyValue = 10;
Result result = service.SendMessage("Test", customRequest);
}