简体   繁体   English

WCF继承和动态CreateChannel

[英]Wcf inherit and dynamic CreateChannel

I have an issues on ihneritance on service when i called a dynamic ChannelFactory . 当我调用动态ChannelFactory时,我在服务意识上遇到问题。

The structure of my project : I have a class ServiceBase and an interface IServiceBase that is used in all WCF Service and Contract (2 separate projects). 我的项目的结构:我有一个ServiceBase类和一个IServiceBase接口,该接口在所有WCF服务和合同(2个独立的项目)中都使用。

IServiceBase

Imports System.ServiceModel

<ServiceContract()>
Public Interface IServiceBase

    ''' <summary>
    ''' Ping.
    ''' </summary>
    ''' <returns>Vrai.</returns>
    <OperationContract()>
    Function Ping() As Boolean
End Interface

ServiceBase

Public Class ServiceBase
    Implements IDisposable, IServiceBase

#Region "IServiceBase"

    ''' <summary>
    ''' Ping.
    ''' </summary>
    ''' <returns>
    ''' Vrai.
    ''' </returns>
    Public Function Ping() As Boolean Implements IServiceBase.Ping
        Return True
    End Function

#End Region
End Class

All IFoo inherit from IServiceBase . 所有IFoo都从IServiceBase继承。 All Foo ihnerit from ServiceBase and implement IFoo . 所有Foo都来自ServiceBase并实现IFoo


I have a TestController (client-side) that load all assembly of contracts to parse if it is alive. 我有一个TestController(客户端),它加载所有合同程序集以解析是否存在。

So I take assembly by the type of one of my contracts (working well) 所以我按照我的一份合同的类型进行组装(运作良好)

 Dim common As Assembly = Assembly.GetAssembly(GetType(Firma.Project.Services.Contracts.ICommon))

For each of them I determine if it's an interface and if it derived from IServiceBase (working well, but only with IsAssignableFrom , GetMethod don't return Ping from derived interface (works only with class)). 对于它们中的每一个,我都确定它是否是一个接口以及它是否从IServiceBase派生(运行良好,但仅与IsAssignableFrom配合IsAssignableFromGetMethod不会从派生的接口返回Ping (仅适用于类)。

For Each ty As Type In common.GetTypes
                If ty.IsInterface AndAlso GetType(IServiceBase).IsAssignableFrom(ty) Then 
                    Try
                        Dim myInterfaceType As Type = ty

                        Dim value As String = String.Empty
                        If Not _dicEndPoints.TryGetValue(ty.FullName, value) Then
                            Continue For
                        End If

From this, I create dynamically my channel (seems working, i see well ChannelFactory<IFoo>(endpointname) in debug step) 由此,我动态创建了我的频道(似乎可以正常工作,在调试步骤中我可以很好地看到ChannelFactory<IFoo>(endpointname)

                ' Type ChannelFactory<>.
                Dim factoryType = GetType(ChannelFactory(Of )).MakeGenericType(myInterfaceType)
                ' Constructeur ChannelFactory<IFoo>(endpointname)
                Dim factoryCtr = factoryType.GetConstructor(New Type() {GetType(String)})
                ' Invoke dynamic ChannelFactory<IFoo>(endpointname)
                Dim factory = factoryCtr.Invoke(New Object() {value})
                ' dynamic Create Channel 
                Dim channel = factory.CreateChannel()

But when I call the method Ping , it doesn't work 但是当我调用Ping方法时,它不起作用

Dim retour As Object = channel.Ping() 

I've got "public method Ping is not found". 我有“找不到公用方法Ping”。 Another method declared directly in Foo class are working and the method Ping is working well when I used Client Test WCF on IFoo Service. 当我在IFoo Service上使用客户端测试WCF时,直接在Foo类中声明的另一种方法正在工作,并且Ping方法运行良好。 So it mean that dynamic create of ChannelFactory is the issue, but I don't see how to make this. 因此,这意味着要动态创建ChannelFactory才是问题,但我看不出如何做到这一点。

Edit: answer can be translated in C# and response can be given in C# 编辑:答案可以在C#中翻译,响应可以在C#中给出

Thanks for your helps. 感谢您的帮助。

Try this. 尝试这个。 I'm assuming myInterfaceType is your interface implementing IServiceBase, and value is your endpoint name in your WCF config 我假设myInterfaceType是实现IServiceBase的接口,而value是WCF配置中的终结点名称

class Program
{
    static void Main(string[] args)
    {

        var factoryType = typeof(MyChannelFactory).GetMethod("CreateProxyChannel", BindingFlags.Static | BindingFlags.Public);

        var generic = factoryType.MakeGenericMethod(myInterfaceType);

        var result = generic.Invoke(null, new[] { value});

        var channel = (IServiceBase) result;

        var check = channel.Ping();
    }

}

class MyChannelFactory
{
    public static T CreateProxyChannel<T>(string endpointName)
    {
        ChannelFactory<T> factory = new ChannelFactory<T>(endpointName);

        return factory.CreateChannel();
    }
}

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

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