Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从c中的对象类型获取数据#_C#_Wcf - Fatal编程技术网

C# 从c中的对象类型获取数据#

C# 从c中的对象类型获取数据#,c#,wcf,C#,Wcf,我在服务器端以对象类型存储了一个类对象,并将其发送给客户端。如何从客户端的对象类型检索类对象的数据成员的数据?我正在使用WCF进行通信。我对这个概念还不熟悉 Service Contract [ServiceContract] public interface IRCommService { [OperationContract] result sendMessage(string command, object data); } [DataContrac

我在服务器端以对象类型存储了一个类对象,并将其发送给客户端。如何从客户端的对象类型检索类对象的数据成员的数据?我正在使用WCF进行通信。我对这个概念还不熟悉

Service Contract

  [ServiceContract]
  public interface IRCommService
  {
    [OperationContract]
    result sendMessage(string command, object data);
  }

  [DataContract]
  public class result
  {
    [DataMember]
    public List<string> results { get; set; }
  }


[service behavior]
 public result sendMessage(string command, object data)
    {
      List<string> l = new List<string>();
      Console.WriteLine("Received");
      return new result { results = l };
    }
服务合同
[服务合同]
公共接口IRCommService
{
[经营合同]
结果发送消息(字符串命令、对象数据);
}
[数据合同]
公开课成绩
{
[数据成员]
公共列表结果{get;set;}
}
[服务行为]
公共结果发送消息(字符串命令、对象数据)
{
列表l=新列表();
控制台。写入线(“已接收”);
返回新结果{results=l};
}

如果你真的需要这么大的灵活性,可以考虑使用字典。也就是说,拥有10个具有不同签名的服务方法可能比在一个服务方法中处理10个不同的输入要好


此外,通过自定义类也是常见的做法。如果你的物体有10种不同的东西,只需创建一个包含10个属性的类,并设置您需要的属性。

最好使用自定义请求对象,而不是使用对象数据类型。该请求对象类对于客户端和服务器都应该是通用的。然后在客户端中,您只需填写请求并从服务器检索所需结果即可

您的解决方案层次结构最好如下所示

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; }
    }
}

需要更多信息,它是以XML还是JSON的形式返回给客户端?发布到目前为止您所有的代码,这将极大地帮助有人为您的问题提供解决方案。我正在制作这个类的对象(a=10)。我想将这个类对象发送到客户端并得到这个值10。我正在将类对象复制到对象类型中。如果这不是正确的方法,请建议我如何执行此操作。这里有一些介绍如何在“hello world”级别创建WCF服务器和客户端的演练。请记住,WCF服务器可以使用各种传输方式中的任何一种—命名为管道、tcp或http,并且可以托管在windows服务或IIS中。如何实现客户机取决于您在服务器端选择的托管方式和传输方式。如果将WCF服务添加到IIS网站,将serviceMetadata添加到serviceBehaviors,并且服务正在运行,则只需在.NET客户端项目中添加服务引用,即可构建客户端访问。
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);
        }