简体   繁体   English

通过依赖注入来消耗SOAP服务

[英]Consuming SOAP service by dependency injection

I've been developing silveright app which consumes some WCF services. 我一直在开发使用一些WCF服务的Silveright应用程序。 In my WCF, i have several different services that implement the same interface for example: 在WCF中,我有几种不同的服务实现了相同的接口,例如:

public class Service1 : IService<Type1> {...}
public class Service2 : IService<Type2> {...}

I've been thinking about acceesing the IService interface from the client app (consumer) to developer a generic class to maximize code re-use for consuming the WCF services for example: 我一直在考虑从客户端应用程序(消费者)访问IService接口,以开发通用类,以最大程度地重复使用代码以使用WCF服务,例如:

public class MyUnitedOPs<T>  {

      ServiceReference.IService<T> _object;
      public MyUnitedOPs (ServiceReference.IService<T> _object) {
          this._object = _object;
      }
}

But, apparently I can't directly access the IService interface and it is known as IServiceOf_Service1 for instance. 但是,显然我无法直接访问IService接口,例如,它被称为IServiceOf_Service1 Although this can be still cast but the methods that I get from this IServiceOf_Service1 are different from ones that are provided in the service that implements it for example I have BeginAdd and EndAdd while in the service that implements it, I have just an AddAsync method. 尽管仍然可以BeginAdd ,但是我从此IServiceOf_Service1获得的方法与实现它的服务中提供的方法不同,例如,在实现它的服务中我具有BeginAddEndAdd而我只有一个AddAsync方法。

So, out of curiosity, is it possible at all to do something like this ? 因此,出于好奇,是否有可能做这样的事情?

You can't make your IService<T> traverse to the client, as this is not supported by WSDL. 您不能使IService <T>遍历客户端,因为WSDL不支持此功能。

However, here are a few things that you can do: 但是,您可以执行以下操作:

  1. You can have a generic service contract on the server side. 您可以在服务器端拥有通用服务合同。
  2. You can also have a generic service implementation on the server side. 您还可以在服务器端具有通用服务实现。 (you didn't have to create Service1 and Service2, you could stick with Service<T>) (您不必创建Service1和Service2,可以坚持使用Service <T>)
  3. You can host multiple concrete instances of your generic service, each with different type. 您可以托管通用服务的多个具体实例,每个实例具有不同的类型。
  4. The client can reference one or more of your hosted services, and for each it will produce concrete proxies. 客户可以引用您的一项或多项托管服务,并且每一项都会产生具体的代理。
  5. The reason you're seeing the BeginAdd and EndAdd is just a technicality of the client side, it does not affect anything happening on the server. 您看到BeginAdd和EndAdd的原因仅仅是客户端的技术问题,它不会影响服务器上发生的任何事情。 From your post, i take it that your Add operation returns a Task, which is an efficient asynchronous implementation of a WCF service, starting from .net 4.5. 从您的帖子中,我认为您的Add操作返回一个Task,这是从.net 4.5开始的WCF服务的高效异步实现。 Even though your operation returns a Task, if you'll check the WSDL generated by the service, you'll see that your operation actually returns something else (an int, or string, or maybe some DataContract, depending on your implementation). 即使您的操作返回了Task,但如果您检查服务生成的WSDL,您也会看到您的操作实际上还返回了其他内容(一个int或一个字符串,或者某个DataContract,具体取决于您的实现)。 That's because asynchronicity, is just an implementation detail of the service, and it doesn't change the contract . 这是因为异步只是服务的实现细节,它不会更改合同 The client can call your service in three different ways: 客户可以通过三种不同的方式致电您的服务:

    a. 一种。 Just call it directly. 只需直接调用即可。 Meaning the your client will block the current thread. 这意味着您的客户端将阻止当前线程。

    b. b。 Use the APM async model, which utilizes methods like BeginXXX and EndXXX, which will initiate the actuall I/O request over the network, from a different thread, not blocking the calling thread like the previous option. 使用APM异步模型,该模型利用BeginXXX和EndXXX之类的方法,这些方法将从另一个线程通过网络发起实际的I / O请求, 不像以前的选项那样阻塞调用线程。

    c. C。 Use the TAP async model, which utilizes Tasks with methods like XXXAsync. 使用TAP异步模型,该模型将Tasks与XXXAsync等方法结合使用。

    You control these options (a - c), and many others (for example, what collection types to use for collections? Arrays? List? etc') from the "Add Service Reference" window in visual studio. 您可以从Visual Studio的“添加服务引用”窗口中控制这些选项(a-c)以及许多其他选项(例如,用于集合的哪种集合类型?数组?列表?等)。

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

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