是否可以使用客户端的通用DataContract? [英] Is it possible to use generic DataContract's from the client end?

查看:110
本文介绍了是否可以使用客户端的通用DataContract?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当你创建一个服务时,你可以创建一个通用的DataContract:

  [DataContract(Name =Get {0 } Request)
public sealed class GetItemRequest< T>
其中T:class,new(){...}

[DataContract(Name =Get {0} Response)
public sealed class GetItemResponse< T>
其中T:class,new(){...}

[ServiceContract]
public void MyService:IMyService
{
[OperationContract]
GetItemResponse< Foo> GetItem(GetItemRequest< Foo>请求);
}

这会生成一个 GetFooRequest GetFooResponse 定义我的WSDL。现在,我很好奇的是,如果有可能走向另一个方向吗?

是否可以创建一个使用通用DataContracts并通过这些到服务器作为具体的对象?我在添加Service Reference之后尝试了这一点,但实际上效果并不好。所以,这更多的是我想知道是否有任何方法(即使这意味着不添加服务引用)来做到这一点?

解决方案

<最终,WCF将会看看合同类。如果是从WSDL / MEX生成的,它不会有这个(因为这不是它在元数据中的表达方式) - 但是如果你的客户端有上面的代码,那么确定它应该可以正常工作。



如果您从客户端向您的DTO dll添加一个引用(即dll /项目引用),并确保WCF启用了共享程序集,应该管用。如果它仍然阻止,那么作弊:使用服务引用来获取配置数据。然后删除服务引用,但保留配置(否则这些配置文件是一种痛苦)。然后它应该从库中找到类型。


I know when you create a service you can create a generic DataContract:

[DataContract(Name = "Get{0}Request")
public sealed class GetItemRequest<T>
    where T : class, new() { ... }

[DataContract(Name = "Get{0}Response")
public sealed class GetItemResponse<T>
    where T : class, new() { ... }

[ServiceContract]
public void MyService : IMyService
{
    [OperationContract]
    GetItemResponse<Foo> GetItem(GetItemRequest<Foo> request);
}

This generates a GetFooRequest and GetFooResponse definition for my WSDL. Now, what I'm curious about is if it is possible to go in the other direction?

Is it possible to create a client that uses the Generic DataContracts and pass those to the server as a concrete object? I attempted this after adding a Service Reference and it didn't really work out so well. So this is more of me wondering if there is any way (even if it means not adding a Service Reference) to do this?

解决方案

Ultimately, WCF is going to look at the contract class. If that is generated from WSDL/MEX it won't have this (since this isn't how it is expressed in the metadata) - but if your client has the code as above, then sure it should work fine.

If you add a library reference (i.e. a dll / project reference) to your DTO dll from the client, and ensure WCF has shared-assemblies enabled, it should work. If it still baulks, then cheat: use a service reference just to get the config data. Then delete the service reference but keep the configuration (those config files are a pain otherwise). Then it should locate the type from the library.

这篇关于是否可以使用客户端的通用DataContract?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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