简体   繁体   English

如何在.NET中使用相同的代码调用两个或多个WCF服务?

[英]How call two or more WCF Services with same code in .NET?

I'm coming from Web Api so maybe this question will be a little weird. 我来自Web Api,所以也许这个问题有点奇怪。 I'm working in Visual Studio 2012 我在Visual Studio 2012中工作

Scenario: 场景:

I've two WCF services right now (but in the future will be more) that accept the same object (suppose datacontract A with same datamembers) 我现在有两个WCF services (但将来会更多),它们接受相同的对象(假设具有相同数据成员的datacontract A)

I'm calling them in this way: 我这样称呼他们:

string url1 = "myURL1";
BasicHttpBinding binding1 = new BasicHttpBinding();
EndpointAddress address1 = new EndpointAddress(url1);
ServiceClient1 serviceClient1 = new ServiceClient1(binding1, address1);

string url2 = "myURL2";
BasicHttpBinding binding2 = new BasicHttpBinding();
EndpointAddress address2 = new EndpointAddress(url2);
ServiceClient2 serviceClient2 = new ServiceClient2(binding2, address2);

Question: 题:

It is possible call them dinamically? 可以给他们打电话吗? In one only method in order to just change the urls? 在一种仅更改URL的方法中?


Update 更新

Is it possible call them without any reference of the type? 可以在没有任何类型引用的情况下调用它们吗? Because I need use a common method in both and that method receives the same object and retrieves the same object. 因为我都需要在两者中使用一个通用方法,所以该方法接收相同的对象并检索相同的对象。

T CreateServiceClient<T>(string url)
{
    var binding = new BasicHttpBinding();
    var endpointAddress = new EndpointAddress(url);
    return (T) Activator.CreateInstance(typeof(T), new object[] {binding, endpointAddress});
}

Should be able to use it like so: 应该能够像这样使用它:

var client1 = new CreateServiceClient<ServiceClient1>("http://...");
var client2 = new CreateServiceClient<ServiceClient2>("http://...");

You can look into generics and try to constrain T to be more specific as this doesn't give you any type safety. 您可以研究泛型并尝试将T限制为更具体,因为这不会给您任何类型安全性。

If you mean dynamically like you only have a Type , you can do this: 如果您动态地表示自己只有Type ,则可以执行以下操作:

object CreateServiceClient(Type serviceClientType, string url)
{
    var binding = new BasicHttpBinding();
    var endpointAddress = new EndpointAddress(url);
    return Activator.CreateInstance(serviceClientType, new object[] {binding, endpointAddress});
}

Just change object to be a more generic interface or class that all of your clients adhere to if you have one defined. 如果定义了object ,只需将object更改为所有客户端都遵循的更通用的接口或类。

EDIT 编辑

Based on your updated question you will need to do one of two things 根据您更新的问题,您将需要执行以下两项操作之一

  1. Implement an interface or a base class for your common services with the common methods you are needing to invoke. 使用您需要调用的通用方法为通用服务实现接口或基类。 The above code will still work, just need to cast your new common type. 上面的代码仍然可以使用,只需要强制转换您的新普通类型。
  2. Use reflection to dynamically invoke the common method you are needing. 使用反射来动态调用所需的常用方法。 This won't give you any compile time checks or errors and any issues will only be found during runtime. 这不会给您任何编译时检查或错误,并且只会在运行时发现任何问题。

I highly recommend #1, but if for some reason that is not possible, you can do #2. 我强烈建议#1,但是如果由于某些原因无法执行,则可以执行#2。 If you need to do #2, you could meet halfway between the two and implement a wrapper class that tries to union some of this functionality: 如果您需要做#2,则可以在两者之间碰到一半,并实现一个包装器类,该包装器类试图结合一些此功能:

public class MyServiceWrapper
{
    Type _serviceType;
    public MyServiceWrapper(Type serviceType)
    {
        _serviceType = serviceType;
    }

    public object CreateInstance()
    {
        ... code from above ...
    }

    public YourObject InvokeServiceMethod() 
    {
        var instance = CreateInstance();
        var methodInfo = _serviceType.GetMethod("MethodName");
        return (YourObject) methodInfo.Invoke(instance, anyArguments);
    }
}

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

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