简体   繁体   English

在不生成程序集的情况下调用WCF服务

[英]Calling a WCF service without generating an Assembly

I am trying to write some code in C# that will call a WCF service on the fly by importing the WSDL, examining it and then making calls to it dynamically. 我试图在C#中编写一些代码,通过导入WSDL,检查它然后动态调用它来动态调用WCF服务。

The service I am calling can change from time to time - so if it does I want my client to know about new methods and new input parameters and output parameters to the calls, without rebuilding my client. 我正在调用的服务可能会不时更改 - 所以如果它确实如此,我希望我的客户端知道新方法和新输入参数以及调用的输出参数,而无需重建我的客户端。

One possible solution to this is to import and compile a service reference on the fly. 一种可能的解决方案是动态导入和编译服务引用。

Outlined here: Creating an assembly on the fly from a WSDL 这里概述: 从WSDL动态创建程序集

I would like to avoid the generation of an assembly and then reflecting over it if possible. 我想避免生成一个组件,然后尽可能地反射它。

I looked into the code of the dynamic proxy in the link and they use a framework class to do the import. 我查看了链接中动态代理的代码,他们使用框架类来进行导入。 This class is the WsdlImporter . 这个类是WsdlImporter So I had thought great - I can use that and examine the WSDL schema and determine what calls are present and what inputs and outputs are available. 所以我认为很好 - 我可以使用它并检查WSDL模式并确定存在哪些调用以及可用的输入和输出。

The problem is that the type information is missing in the MessagePartDescription objects that the WsdlImporter creates. 问题是WsdlImporter创建的MessagePartDescription对象中缺少类型信息。 Apparently this is missing because it cannot find the types yet - see the response to the question from Brian. 显然这是因为它无法找到类型而丢失- 请参阅Brian对问题的回答。

So any advice on how I should proceed? 那么关于我应该如何进行的任何建议? Am I completely on the wrong track here? 我在这里走错了路吗?

This is probably not an answer but I will post it as one to fully describe my opinion. 这可能不是一个答案,但我会将其作为一个完整描述我的意见。

Dynamic proxy: 动态代理:
IMO this is example of wrong usage of technology. IMO这是错误使用技术的例子。 It is elementary behavior of WSDL - if it changes you have to change client or you have to make good WSDL versioning and create new client. 它是WSDL的基本行为 - 如果它发生变化,您必须更改客户端,或者您必须进行良好的WSDL版本控制并创建新客户端。

You still have to somehow say your client to get WSDL - does it mean that you will parse WSDL before each call? 您仍然必须以某种方式说您的客户端获取WSDL - 它是否意味着您将在每次调用之前解析WSDL? Doesn't seem like a good idea. 似乎不是一个好主意。

Information about types is really not part of WSDL because by default WSDL is generated as interoperable. 有关类型的信息实际上不是WSDL的一部分,因为默认情况下WSDL是生成可互操作的。 CLR types are not operation needed for interoperability. CLR类型不是互操作性所需的操作。 When you create service proxy by Add service reference or Svcutil it will generate code for types defined in WSDL. 当您通过添加服务引用或Svcutil创建服务代理时,它将为WSDL中定义的类型生成代码。 That code then need to be compiled. 然后需要编译该代码。

You can try to use NetDataContractSerializer instead of default DataContractSerializer . 您可以尝试使用NetDataContractSerializer而不是默认的DataContractSerializer NetDataContractSerializer adds CLR type information into WSDL but I still expect that new types must be known to your clients - it means deploying new assembly with types and use it by clients. NetDataContractSerializer将CLR类型信息添加到WSDL中,但我仍然希望客户端必须知道新类型 - 这意味着部署带有类型的新程序集并由客户端使用它。 This almost sounds like same approach when simply deploying assembly with new static client proxy. 当使用新的静态客户端代理部署程序集时,这几乎听起来像是相同的方法。

Dynamic WF client 动态WF客户端
I also don't see too much usage of this architecture - you still need to change client to reflect new WF steps, don't you? 我也没有看到太多使用这种架构 - 您仍然需要更改客户端以反映新的WF步骤,不是吗?

Changing the WF 改变WF
Are we talking about Windows Workflow foundation? 我们在谈论Windows Workflow基础吗? I can hardly imagine scenario where you create WF, expose it as a service and then change it. 我很难想象您创建WF的场景,将其作为服务公开然后更改它。 When you expose WF as service you are probably defining long running WF. 当您将WF公开为服务时,您可能正在定义长时间运行的WF。 Long running WFs use persistance which is based on serialization (at least in WF 3.5 but I believe it is same in WF 4). 长时间运行的WF使用基于序列化的持久性(至少在WF 3.5中,但我相信它在WF 4中是相同的)。 When you change WF definition, all persisted WFs are most probably doomed because they will never ever deserialize. 当您更改WF定义时,所有持久化的WF很可能注定失败,因为它们永远不会反序列化。 This situation is usually solved by parallel deployment of new and old version where old version is only used to finish incomplete WFs. 这种情况通常通过并行部署新旧版本来解决,其中旧版本仅用于完成不完整的WF。 Again it means new clients. 它再次意味着新客户。

If you look at the problem from a different angle. 如果从不同的角度看问题。 Do you need to regenerate the proxy each time or do you need a contract that continues to work when things change? 您是否每次都需要重新生成代理,还是需要在事情发生变化时继续工作的合同?

WCF has a mechanism for this IExtensibleDataContracts see: http://msdn.microsoft.com/en-us/library/ms731083%28v=VS.100%29.aspx WCF有一个这种IExtensibleDataContracts的机制,请参阅: http//msdn.microsoft.com/en-us/library/ms731083%28v=VS.100%29.aspx

Best practices for versioning of contracts can be found here 可以在此处找到合同版本化的最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM