简体   繁体   English

WCF客户-最佳实践

[英]WCF Client - Best Practise

I just wanted to hear your Opinion about a WCF Client implementation. 我只是想听听您对WCF客户端实现的看法。

I have an Server that provides several services like SecurityManager. 我有一台提供多种服务的服务器,例如SecurityManager。 This service is defined in the Interface ISecurityManager and implemented in the Class SecurityManager. 该服务在接口ISecurityManager中定义,并在Class SecurityManager中实现。

So far everything is fine. 到目前为止,一切都很好。 On the Client side I want to implement the services calls via a seperated Class. 在客户端,我想通过一个单独的类实现服务调用。 My question is whether I do this also in an SecurityManager Class which implements the same ISecurityManager Interface? 我的问题是我是否也在实现相同ISecurityManager接口的SecurityManager类中这样做?

What is here the best practise? 最好的做法是什么?

I would suggest to use a generic Wrapper make WCF calls. 我建议使用通用包装器进行WCF调用。 So everytime you need to make a WCF call you can do it like this: 因此,每当您需要进行WCF调用时,都可以这样进行:

var invoker = new ServiceInvoker();
        var result = invoker.InvokeService<ISecurityManager, MyObjectReturnType>(
            proxy => proxy.DoSomething(myParameters));
        return result;

I use ServiceInvoker to create the channel and manage any exceptions that would occur inside. 我使用ServiceInvoker创建通道并管理内部发生的任何异常。 It creates a channel with the ISecurityManager contract, with return type of MyObjectReturnType and with the action DoSomething (method from ISecurityManager contract). 它使用ISecurityManager合同,返回类型为MyObjectReturnType和操作DoSomething(来自ISecurityManager合同的方法)创建一个通道。 You can use this solution with all your interfaces, without any extra class implementation! 您可以在所有接口上使用此解决方案,而无需任何额外的类实现!

InvokeService would be something like this: InvokeService将如下所示:

public TResult InvokeService<TServiceContract, TResult>(Func<TServiceContract, TResult> invokeHandler) where TServiceContract : class
    {
        ICommunicationObject communicationObject;
        var arg = CreateCommunicationObject<TServiceContract>(out communicationObject);
        var result = default(TResult);
        try
        {
            result = invokeHandler(arg);
        }
        catch (Exception ex)
        {
            Logger.Log(ex);
            throw;
        }

        finally
        {
            try
            {
                if (communicationObject.State != CommunicationState.Faulted)
                    communicationObject.Close();
            }
            catch
            {
                communicationObject.Abort();
            }
        }
        return result;
    }

private TServiceContract CreateCommunicationObject<TServiceContract>(out ICommunicationObject communicationObject)
        where TServiceContract : class
    {
        //Create the Channel
        // ICommunicationObject is an out parameter for disposing purposes 
        return channel;
    }

Visual Studio Generator Visual Studio生成器

You can ask Visual Studio to build a client for you, right-clicking your client project and adding a Service Reference . 您可以要求Visual Studio为您构建一个客户端,右键单击您的客户端项目并添加Service Reference There's a dialog where you can either type your service url or discover it from within the solution. 有一个对话框,您可以在其中键入服务URL或从解决方案中发现它。

Creating a Client 创建一个客户

You can build the client class inheriting from ClientBase<ISecurityManager>, ISecurityManager . 您可以构建从ClientBase<ISecurityManager>, ISecurityManager继承的客户端类。 Being an operation example on this client class: 作为此客户端类上的操作示例:

public void ExampleMethod(int id)
{
   Channel.ExampleMethod(id);
}

Like a real man does 就像一个真正的男人一样

Or without any client class, just calling it: 或没有任何客户端类,只需调用它即可:

ServiceInvokerinvoker invoker = new ServiceInvoker(); 
var result = invoker.InvokeService<ISecurityManager, ReturnType>(     proxy => proxy.ExampleMethod(1) ); 

Last two options assuming you already have configured the ISecurityManager client: 假设您已经配置了ISecurityManager客户端,则最后两个选项是:

<client>     
<endpoint name="ServiceName" 
address="http://ServiceName.test/Service" 
binding="basicHttpBinding"   
contract="ISecurityManager" /> 
</client> 

To implement the service calls "via a separate class" you can just generate a service reference using svcutil.exe or visual studio from a running instance of your service. 要实现“通过单独的类”进行的服务调用,您只需使用svcutil.exe或Visual Studio从正在运行的服务实例中生成服务引用即可。

This will generate a set of types off your service contract which will be completely "separate" from the assemblies containing your service contract and implementation. 这将根据您的服务合同生成一组类型,这些类型将与包含您的服务合同和实现的程序集完全“分离”。

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

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