zoukankan      html  css  js  c++  java
  • 3 ways to create WCF Client(ChannelFactory)

    There are 3 basic ways to create a WCF client:

    1. Let Visual Studio generate your proxy. This auto generates code that connects to the service by reading the WSDL. If the service changes for any reason you have to regenerate it. The big advantage of this is that it is easy to set up - VS has a wizard and it's all automatic. The disadvantage is that you're relying on VS to do all the hard work for you, and so you lose control.

    2. Use ChannelFactory with a known interface. This relies on you having local interfaces that describe the service (the service contract). The big advantage is that can manage change much more easily - you still have to recompile and fix changes, but now you're not regenerating code, you're referencing the new interfaces. Commonly this is used when you control both server and client as both can be much more easily mocked for unit testing. However the interfaces can be written for any service, even REST ones - take a look at this Twitter API.

    3. Write your own proxy - this is fairly easy to do, especially for REST services, using theHttpClient or WebClient. This gives you the most fine grain control, but at the cost of lots of service API being in strings. For instance: var content = new HttpClient().Get("http://yoursite.com/resource/id").Content; - if the details of the API change you won't encounter an error until runtime.

    Personally I've never liked option 1 - relying on the auto generated code is messy and loses too much control. Plus it often creates serialisation issues - I end up with two identical classes (one in the server code, one auto generated) which can be tided up but is a pain.

    Option 2 should be perfect, but Channels are a little too limiting - for instance they completely lose the content of HTTP errors. That said having interfaces that describe the service is much easier to code with and maintain.

    Example for using ChannelFactory.  

    I find myself re-using this sort of snippet often, so I'm posting it here for all to enjoy. :)
     
    The issue here is: Using the built in proxy generator for referencing WCF services from client code is OK, but a neater way to do it is shown below - this ONLY works if the server and client code are both controlled by the author - its no good for publicly exposed web services where you expect the user to query the WSDL or service metadata as this will always end up with a SVCUTIL built proxy. 
     
    So what you do is, pull out your interface and your contract objects, to a 3rd assembly, and reference that from your service code and also your client. Then simply use the class shown below, from the client, plugging in the contract interface where the generic type is required. Add a config line pointing at the service endpoint. Voila!

    public class ServiceWrapper<T> : IDisposable where T: class
        {
            ChannelFactory<T> factory;
            private T channel;
     
            private readonly BasicHttpBinding binding;
            private readonly EndpointAddress endpoint;
     
            private readonly object lockObject = new object();
            private bool disposed;
     
            public ServiceWrapper(string configName)
            {
                if (ConfigurationManager.AppSettings[configName] == null)
                {
                    throw new ConfigurationErrorsException(configName + " is not present in the config file");
                }
     
                binding = new BasicHttpBinding();
                endpoint = new EndpointAddress(ConfigurationManager.AppSettings[configName]);
                disposed = false;
            }
     
            public T Channel
            {
                get
                {
                    if (disposed)
                    {
                        throw new ObjectDisposedException("Resource ServiceWrapper<"+typeof(T)+"> has been disposed");
                    }
     
                    lock (lockObject)
                    {
                        if (factory == null)
                        {
                            factory = new ChannelFactory<T>(binding, endpoint);
                            channel = factory.CreateChannel();
                        }
                    }
                    return channel;
                }
            }
     
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
     
    
            public void Dispose(bool disposing)
            {
                if (!disposed)
                {
                    if (disposing)
                    {
                        lock (lockObject)
                        {
                            if (channel != null)
                            {
                                ((IClientChannel) channel).Close();
                            }
                            if (factory != null)
                            {
                                factory.Close();
                            }
                        }
     
                        channel = null;
                        factory = null;
                        disposed = true;
                    }
                }
            }
        }

    Usage:
    1.Don't forget to wrap the service wrapper in a using statement so that the dispose gets called when it goes out of scope.

    2.Must use the same binding as the service binding.

    3.for convenience. you can share the service contract between service and client. which compiled service contract to a Dll. 

     
    private const string SERVICE_ENDPOINT = "ServiceEndpoint";
     
    ...
     
    using (var sw = new ServiceWrapper<IDataService>(SERVICE_ENDPOINT))
     {
        MyType t = sw.Channel.MyFunction(3);
    }

    config:

    <appSettings>
        <add key="ServiceEndpoint" value="http://localhost:55757/DataService.svc" /><!--this endpoint address could be the service base uri + endpoint address.-->
      </appSettings>

    See Also

    Loading the WCF configuration from different files on the client side
  • 相关阅读:
    160922、配置:spring通过profile或@profile配置不同的环境(测试、开发、生产)
    160921、React入门教程第一课--从零开始构建项目
    160920、springmvc上传图片不生成临时文件
    160919、使用AOP与注解记录Java日志
    160918、BigDecimal运算
    160914、ionic指令简单布局
    Oracle 修改文件所有者
    Oracle 新增删除账户
    Oralce 账户被锁后的解决办法
    Linux Oracle 转换编码格式
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2580789.html
Copyright © 2011-2022 走看看