更好地使用自定義請求對象,而不是使用Object數據類型。請求對象類應該在客戶端和客戶端都是公用的,然後您可以填充請求並從中獲取所需的結果服務器。
您的解決方案層次結構最好如下。在普通的裝配
在普通的裝配
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;
}
}
}
Request類
namespace Common
{
using System.Runtime.Serialization;
[DataContract]
public class CustomRequest
{
[DataMember]
public int MyValue { get; set; }
}
}
Response類
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);
}
需要更多信息,它是以XML還是JSON形式返回給客戶端?發佈迄今爲止所有的代碼,對於提供解決方案的人來說,這會大有幫助。 –
我有一個數據成員int a的簡單類。我正在創建這個類的對象(a = 10)。我想發送這個類對象到客戶端,並得到這個值10.我將類對象複製到一個對象類型。如果這不是正確的方法,請告訴我如何做到這一點。 – user2276352
發佈您的WCF代碼。 –