是否可以从客户端使用通用 DataContract's?

编程入门 行业动态 更新时间:2024-10-26 16:22:46
本文介绍了是否可以从客户端使用通用 DataContract's?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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 生成一个 GetFooRequestGetFooResponse 定义.现在,我很好奇是否有可能朝另一个方向发展?

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&amp;#39;s?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 19:32:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   DataContract   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!