[英]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中工作
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);
It is possible call them dinamically? 可以给他们打电话吗? In one only method in order to just change the urls?
在一种仅更改URL的方法中?
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
更改为所有客户端都遵循的更通用的接口或类。
Based on your updated question you will need to do one of two things 根据您更新的问题,您将需要执行以下两项操作之一
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.