简体   繁体   中英

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. I'm working in 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)

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?


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.

If you mean dynamically like you only have a Type , you can do this:

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.

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. 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:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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