问题描述
限时送ChatGPT账号..我知道当你创建一个服务时,你可以创建一个通用的 DataContract:
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);
}
这会为我的 WSDL 生成一个 GetFooRequest
和 GetFooResponse
定义.现在,我很好奇是否有可能朝另一个方向发展?
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?
推荐答案
最终,WCF 将查看合同类.如果它是从 WSDL/MEX 生成的,则不会有这个(因为这不是它在元数据中的表达方式) - 但如果您的客户端具有上述代码,那么确保它应该可以正常工作.
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.
如果您从客户端向 DTO dll 添加 library 引用(即 dll/项目引用),并确保 WCF 已启用共享程序集,它应该可以工作.如果它仍然阻止,那么作弊:使用服务引用只是为了获取配置数据.然后删除服务引用但保留配置(否则那些配置文件会很痛苦).然后它应该从库中定位类型.
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&#39;s?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论