WCF 返回带有对象类型的自定义结果 [英] WCF Return a custom result with an object type

查看:33
本文介绍了WCF 返回带有对象类型的自定义结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 WCF 函数调用返回一个 object,如果我在我的 ServiceModel 中指定了类型,则结果发送成功.

I'm trying return an object from my WCF function call, and if I specify the type in my ServiceModel, the result is sent successfully.

但是如果我发送一个对象显示我一条消息:

But if I send an object displayme a message like:

wcf 测试客户端不支持此操作,因为它使用类型

This operation is not supported in the wcf test client because it uses type

代码:

[DataContract]
public class ServiceModel
{
    [DataMember]
    public int status { get; set; }

    [DataMember]
    public string message { get; set; }

    [DataMember]
    public requestdVM result { get; set; }
    //public object result { get; set; }
}

问题类似于:WCF 服务:返回自定义对象 但作为解决方案他们改变了类型

The problem is similar to: WCF service: Returning custom objects but as solution they change the type

推荐答案

一般来说,在 WCF 服务中使用基本公共对象是不可能的.

In common there is no such possibility as using base common objects in WCF services.

以下是 WSDL 类型声明的示例:

Here is the example of WSDL type declaration:

   <complexType name="TimePeriod">
      <all>
          <element name="StartTime" type="xsd:timeInstant"/>
          <element name="EndTime" type="xsd:timeInstant"/>
      </all>
   </complexType>

这个模式对客户端是可见的,然后.NET可以理解它可以使用他自己的对象并传输它.在 .NET 中,这将是类似于

This schema is visible for the clients and then .NET can understand that it can use his own object and transfer it. In .NET this will be similar to

[DataContract]
public class TimePeriod
{
   [DataMember]
   public DateTime StartTime {get;set;}
   [DataMember]
   public DateTime EndTime {get;set;}
}

Java 可以理解 WSDL 并使用自己的类和对象;Python 可以,PHP 可以,希望你有 SOA 的想法.

Java can understand WSDL and use own class and objects; Python can do it, PHP can do it, hope you got the SOA idea.

现在你想发送可以是任何东西的对象.用合同是不可能的.

Now you want to send object that can be anything. It is not possible to do with contracts.

但是,如果您可以保证在您的项目中只使用 .NET,那么就有一个解决方案.
这个想法是以二进制格式序列化 .NET 对象并给出对象的类型名称.然后在客户端反序列化它.当然,您不能简单地将对象从 .NET 发送到 Python 等.

However if you can promise that only .NET is using in your project then there is a solution.
The idea is to serialize .NET object in binary format and give a type name of the object. Then on the client side deserialize it. Of course you cannot simply do it with sending object from .NET to, say, Python.

1) 以字符串 XML 格式发送的小对象

1) Small object sending in string XML format

[Serializable]
public class Data
{
  public string TypeName {get;set;} // typeof(string), typeof(int), etc.
  public string Xml {get;set;} // serialized object via XmlSerializer
}

然后在客户端序列化和接收对象后,就可以反序列化

Then after serialization and receiving object in the client it can be deserialized

    Data objectData;// we got it from service
    var serializer = new XmlSerializer(typeof(objectData.TypeName));
    object result;

    using (TextReader reader = new StringReader(objectData.Xml))
    {
        result = serializer.Deserialize(reader);
    }

也可以用二进制序列化代替XML,那么就会有byte[]属性.

Also binary serialization can be used instead of XML, then there will be byte[] property.

2) 第二种解决方案将使用二进制流并在 消息头,方法与第一点类似.

2) Second solution will be using binary stream and write type name in the header of message, the approach is similar to the first point.

一切都取决于情况,但我推荐它用于大文件(> 1 MB).

All depends on situation, but I recommend it for big files (> 1 MB).

这篇关于WCF 返回带有对象类型的自定义结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆