• [WCF]WCF起航


    解决方案概览:

    Client:windows 控制台应用程序。
    WcfService1: windows 服务应用程序。
    WCFWebTest:asp.net 空web应用程序。

    变量程序命名、结构可能不是那么规范,重点在与说明问题。


    1. 建立WCF服务项目。

    在解决方案上面点右键->添加->新建项目->wcf服务应用程序(图标是齿轮带3个小箭头)。

    删掉自动生成的服务和协议。

    1. 添加服务。

    在wcf服务项目上点右键,点添加->新建项->wcf服务。本例中建立了两个服务,User.svc 和Animal.scv。建立服务后,会自动生成对应的协议(接口),IUser、IAnimal.

    1. 测试方法创建。

        在IUser中,定义一个方法接口。ShowName,用来接收一个字符串,返回一个字符串。

        [ServiceContract]
        public interface IUser
        {
            [OperationContract]
            string ShowName(string name);
        }
    IUser

        在IAnimal中,定义一个方法接口。DoWork,也用来接收一个字符串,返回一个字符串。

    [ServiceContract]
        public interface IAnimal
        {
            [OperationContract]
            string DoWork(string a);
        }
    IAnimal

        记得在方法前面增加

    [OperationContract]

        在Animal.scv 和User.svc 中分别实现接口方法。

        public class User : IUser
        {
            #region IUser 成员
    
            public string ShowName(string name)
            {
                return string.Format("Test:{0}", name);
            }
    
            #endregion
        }
    User
    public class Animal : IAnimal
        {
            public string DoWork(string a)
            {
                return string.Format("Animal Test:{0}", a);
            }
        }
    Animal
    1. 测试实例调用。
     1  static void Main(string[] args)
     2         {
     3             string a = Console.ReadLine();
     4              
     5             //根据建议,也这么实例化一个
     6             SRUserWSDL.UserClient b = new SRUserWSDL.UserClient();
     7 
     8             //调用方法
     9             a = b.ShowName(a);
    10             //关闭连接 
    11             b.Close();
    12             //将返回的结果展示在屏幕上 
    13             Console.WriteLine(a);
    14 
    15             a = Console.ReadLine();
    16 
    17             //实例化一个参数更改的方式
    18             //Service在本地建立的时候,可能是localhost,但是发布的时候,该ip地址可能会发生变化。
    19             //BasicHttpBinding_IAnimal 这个字段来自 客户端(调用端) 的config里面。而不是WCF服务的服务段的config里面
    20             AnimalService.AnimalClient c = new AnimalService.AnimalClient("BasicHttpBinding_IAnimal", "http://192.168.159.142/WcfService1/Animal.svc");
    21 
    22             a = c.DoWork(a);
    23 
    24             c.Close();
    25 
    26             Console.WriteLine(a);
    27 
    28             a = Console.ReadLine();
    29 
    30             //另一种连接地址变化的方法
    31             AnimalService.AnimalClient d = new AnimalService.AnimalClient();
    32 
    33             d.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://192.168.159.142/WcfService1/Animal.svc"); 
    34 
    35             a = d.DoWork(a);
    36             d.Close();
    37 
    38             Console.WriteLine(a);
    39 
    40             Console.ReadKey();
    41         }
    winform端调用
    1  protected void Page_Load(object sender, EventArgs e)
    2         {
    3             AnimalService.AnimalClient c = new AnimalService.AnimalClient("BasicHttpBinding_IAnimal", "http://192.168.159.142/WcfService1/Animal.svc");
    4 
    5             Response.Write(c.DoWork("webtest"));
    6             Response.Flush();
    7         }
    web端调用
    1. 调用结果

    winform端:

    web端:


    后续如果还有进一步发现或者问题修改还会继续补充。

  • 相关阅读:
    公号文章模板
    css 网格线
    刷题笔记-图-图的存储
    PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]
    PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
    PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]
    PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]
    堆排序
    PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
    PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
  • 原文地址:https://www.cnblogs.com/Xuhaiyang/p/4250093.html
Copyright © 2020-2023  润新知