WCF - 泛型

本文介绍了如何在WCF服务中使用泛型,并提供了一个具体的示例。虽然官方文档提到WCF不支持依赖于泛型参数的契约,但实际上可以通过提供封闭类型的构造参数来实现。文章还讨论了这种方式对客户端代理类的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 《Programming WCF Services》中有这么一段话 "You cannot define WCF contracts that rely on generic type parameters. Generics are specific to .NET, and using them would violate the service-oriented nature of WCF. ",意思是说 "我们不能定义依赖泛型参数的 WCF 契约,因为这违反了面向服务的基本原则……"。

其实我们是可以使用泛型的,只不过在添加宿主和端点时,需要提供封闭构造参数。看下面的例子。
[DataContract]
public class Data<T>
{
    [DataMember]
    public T X;
}

[ServiceContract]
public interface IMyService<T>
{
    [OperationContract]
    void Test(Data<T> d);
}

public class MyServie<T> : IMyService<T>
{
    public void Test(Data<T> d)
    {
        Console.WriteLine(d.X);
    }
}

public class WcfTest
{
    public static void Test()
    {
        ServiceHost host = new ServiceHost(typeof(MyServie<int>), 
            new Uri("http://localhost:8080/MyService"));
        host.AddServiceEndpoint(typeof(IMyService<int>), new BasicHttpBinding(), "");

        ServiceMetadataBehavior metadata = new ServiceMetadataBehavior();
        metadata.HttpGetEnabled = true;
        host.Description.Behaviors.Add(metadata);

        host.Open();
    }
}

可以是可以,不过生成的客户端代理类型名字有够怪的。 [sweat] (当然,我们可以使用 Name 强行指定一个 "好看" 的名字。)
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行库版本:2.0.50727.42
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace ConsoleApplication1.localhost
{
    [GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
    [DataContractAttribute(Namespace = "...")]
    [SerializableAttribute()]
    public partial class DataOfint : object, IExtensibleDataObject
    {
    }

    [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [ServiceContractAttribute(ConfigurationName = "ConsoleApplication1.localhost.IMyServiceOf_Int32")]
    public interface IMyServiceOf_Int32
    {
        [OperationContractAttribute(Action = "http://.../IMyServiceOf_Int32/Test", ReplyAction = "...")]
        void Test(DataOfint d);
    }

    [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public interface IMyServiceOf_Int32Channel : IMyServiceOf_Int32, IClientChannel
    {
    }

    [DebuggerStepThroughAttribute()]
    [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public partial class MyServiceOf_Int32Client : ClientBase<IMyServiceOf_Int32>, IMyServiceOf_Int32
    {
        public void Test(DataOfint d)
        {
            base.Channel.Test(d);
        }
    }
}

 不过,既然是开发 SOA,那么还是遵循标准为好。所以还是建议大家,不要在服务公开接口中使用泛型。至于内部实现该怎么着就怎么着好了。
其实你这样做已经不算是泛型了,因为在宿主提供服务的时候类型已经确定了。
 契约还是可以用泛型来提高代码复用的吗~~~~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值