zoukankan      html  css  js  c++  java
  • WCF .NET REST调用方式

    1通过WebClient或者Request实现,此方法略去

    2通过WCF Client调用

      a通过WebChannelFactory建Channel

      b通过Channel就可以直接调用服务端方法

    3代码

    WCF服务端和客户端都引用Contact库

     [ServiceContract]
        public interface IUserService : IBaseWCFService
        {
            [WebGet(UriTemplate = "userID={userID}")]
            [Description("根据UserID获取用户信息")]
            UserInfo GetUserInfo(string userID);
        }

    Client调用方式

    using (WebChannelFactory<IUserService> wcf = new WebChannelFactory<IUserService>(new Uri("http://ipv4.fiddler:30760/Services/UserService")))
                    {  
                        var channel = wcf.CreateChannel();
                        IClientChannel clientchanel = channel as IClientChannel;                   
                        channel.GetUserInfo(new Random().Next(1, 1000).ToString());
                    }

    WCF请求头设置,ContentType,Accept,这个的设置很让人头痛,查询了很多资料才找到实现方式

    通过OperationContextScope来实现

    直接代码

     using (WebChannelFactory<IUserService> wcf = new WebChannelFactory<IUserService>(endpoint))
                    {
                        var channel = wcf.CreateChannel();
                        IClientChannel clientchanel = channel as IClientChannel;
                        using (new OperationContextScope(channel as IContextChannel))
                        {
                            WebOperationContext.Current.OutgoingRequest.Headers.Add("APIKey", Guid.NewGuid().ToString());
                            WebOperationContext.Current.OutgoingRequest.ContentType = "json"; 

             WebOperationContext.Current.OutgoingRequest.Accept                      
                            channel.GetUserInfo(new Random().Next(1, 1000).ToString());
                        }                   
                    }

  • 相关阅读:
    SpringMVC的拦截器
    artDialog双击会关闭对话框的修改
    artDialog弹出框使用
    解决从本地文件系统上传到HDFS时的权限问题
    JAVA中写时复制(Copy-On-Write)Map实现
    数据结构--堆的实现(下)
    二叉树的创建算法
    Lamport Logical Clock 学习
    动态规划的思想来求解字符串分割问题
    数据结构--图 的JAVA实现(下)
  • 原文地址:https://www.cnblogs.com/rosanshao/p/2450734.html
Copyright © 2011-2022 走看看