zoukankan      html  css  js  c++  java
  • WCF客户端代理

    • 创建类库WCFServiceProxy
    • 添加System.ServiceModel、WCFService(见上篇文章)引用
    • 创建类:BookServiceClient
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Text;
    using System.Threading.Tasks;
    using WCFService;
    using WCFService.Models;
    
    namespace WCFServiceProxy
    {
        public class BookServiceClient : ClientBase<IBookService>, IBookService
        {
            public BookServiceClient() : base() { }
            public BookServiceClient(string endpointConfigurationName) : base(endpointConfigurationName) { }
            public BookServiceClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
            public BookServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
            public BookServiceClient(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress) { }
            public bool Add(string name, double price)
            {
                return base.Channel.Add(name, price);
            }
    
            public List<Book> GetList()
            {
                return base.Channel.GetList();
            }
        }
    }

    创建类BookServiceProxy

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.Text;
    using System.Threading.Tasks;
    using WCFService.Models;
    
    namespace WCFServiceProxy
    {
        public static class BookServiceProxy
        {
            private static string _clientEndpointName = "bookInfo";
            static List<Book> list = new List<Book>();
            public static bool Add(string name, double price)
            {
                BookServiceClient client = null;
                try
                {
                    client = new BookServiceClient(_clientEndpointName);
                    client.Add(name, price);
                    client.Close();
                    return true;
                }
                catch (Exception ex)
                {
                    if (client != null && client.State != CommunicationState.Closed)
                    {
                        client.Abort();
                        client = null;
                    }
                    return false;
                }
                finally
                {
                    client = null;
                }
            }
    
            public static List<Book> GetList()
            {
                BookServiceClient client = null;
                try
                {
                    client = new BookServiceClient(_clientEndpointName);
                    list = client.GetList();
                    client.Close();
                    return list;
                }
                catch (Exception ex)
                {
                    if (client != null && client.State != CommunicationState.Closed)
                    {
                        client.Abort();
                        client = null;
                    }
                    return null;
                }
                finally
                {
                    client = null;
                }
            }
        }
    }
  • 相关阅读:
    干掉 LaTeX !用BookDown写本书
    Java面试指北!13个认证授权常见面试题/知识点总结!| JavaGuide
    写了个简洁的Typora+Markdown简历模板
    有哪些可以提高代码质量的书籍推荐?
    京东数科面试真题:常见的 IO 模型有哪些?Java 中的 BIO、NIO、AIO 有啥区别?
    国内有哪些顶级技术团队的博客值得推荐?
    两万字长文总结,梳理 Java 入门进阶那些事
    藏在栈里的金丝雀
    surging 如何使用流媒体服务
    低代码平台--基于surging开发微服务编排流程引擎构思
  • 原文地址:https://www.cnblogs.com/wzq806341010/p/3603651.html
Copyright © 2011-2022 走看看