WCF Rest 服务中的协方差实现

发布于 2024-10-15 08:32:08 字数 189 浏览 2 评论 0原文

协方差概念可以在 WCF Rest 服务中实现吗?

即,我有类 A 和 B 继承自 It。

WCF 操作合约具有输入参数 A。我应该也能够将 B 传递给此操作。

我有一个 JSON 客户端,可以访问我的 EXF 休息服务。

我是否可以实现协方差概念。我应该如何在服务器和客户端中执行此操作。请帮助。

Can covariance concept be implemented in WCF rest service,

i.e, i have class A and B inherits from It.

WCF Operation contract has input parameter A. I Should be able to pass B also to this peration.

I have a JSON client which accesses my EXF rest service.

Is it possible i imeplement covariance concept . How should i do this in server and client. Pls Help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗼ふ静 2024-10-22 08:32:08

当然!要实现此功能,唯一需要做的就是将 B 类添加到服务应该了解的类型列表中,使用 ServiceKnownType 属性。

下面是我整理的一个快速示例来演示这一点,假设这是您的服务契约:

using System.Runtime.Serialization;
using System.ServiceModel;

namespace WcfCovariance
{
    [ServiceKnownType(typeof(Employee))]
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        Person GetPerson();

        [OperationContract]
        Person PutPerson(Person person);
    }

    [DataContract]
    public class Person
    {
        [DataMember]
        public string Name { get; set; }
    }

    [DataContract]
    public class Employee : Person
    {
        [DataMember]
        public double Salary { get; set; }
    }
}

以及实现:

namespace WcfCovariance
{
    public class Service1 : IService1
    {
        static Person Singleton = new Person { Name = "Me" };

        public Person GetPerson()
        {
            return Singleton;
        }

        public Person PutPerson(Person person)
        {
            Singleton = person;

            return Singleton;
        }
    }
}

因为您已使用 ServiceKnownType 属性告诉 WCF 关于类型 Employee 的信息,当遇到它时(在输入参数和响应中),它将能够序列化/反序列化它,无论是否使用 JSON。

这是一个简单的客户端:

using System;
using WcfCovarianceTestClient.CovarianceService;

namespace WcfCovarianceTestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new Service1Client("WSHttpBinding_IService1");

            // test get person
            var person = client.GetPerson();

            var employee = new Employee { Name = "You", Salary = 40 };
            client.PutPerson(employee);

            var person2 = client.GetPerson();

            // Employee, if you add breakpoint here, you'd be able to see that it has all the correct information
            Console.WriteLine(person2.GetType()); 

            Console.ReadKey();
        }
    }
}

在 WCF 服务之间传递子类型是很常见的,但您唯一不能做的就是在契约中指定一个接口作为响应。

希望这有帮助。

Of course! The only thing that's required for this to work is for you to add class B to the list of types that the service should know about using the ServiceKnownType attribute.

Here's a quick example I put together to demonstrate this, imagine this is your service contract:

using System.Runtime.Serialization;
using System.ServiceModel;

namespace WcfCovariance
{
    [ServiceKnownType(typeof(Employee))]
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        Person GetPerson();

        [OperationContract]
        Person PutPerson(Person person);
    }

    [DataContract]
    public class Person
    {
        [DataMember]
        public string Name { get; set; }
    }

    [DataContract]
    public class Employee : Person
    {
        [DataMember]
        public double Salary { get; set; }
    }
}

And the implementation:

namespace WcfCovariance
{
    public class Service1 : IService1
    {
        static Person Singleton = new Person { Name = "Me" };

        public Person GetPerson()
        {
            return Singleton;
        }

        public Person PutPerson(Person person)
        {
            Singleton = person;

            return Singleton;
        }
    }
}

Because you've told WCF about the type Employee using the ServiceKnownType attribute, when it's encountered (both in the input parameters and in response) it'll be able to serialize/deserialize it, be it using JSON or not.

Here's a simple client:

using System;
using WcfCovarianceTestClient.CovarianceService;

namespace WcfCovarianceTestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new Service1Client("WSHttpBinding_IService1");

            // test get person
            var person = client.GetPerson();

            var employee = new Employee { Name = "You", Salary = 40 };
            client.PutPerson(employee);

            var person2 = client.GetPerson();

            // Employee, if you add breakpoint here, you'd be able to see that it has all the correct information
            Console.WriteLine(person2.GetType()); 

            Console.ReadKey();
        }
    }
}

It's very common to pass subtypes to and from a WCF service, the only thing you won't be able to do though is to specify an interface as the response in your contract.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文