zoukankan      html  css  js  c++  java
  • C#无需IIS构建XmlRpc服务器

    准备

    我们使用CookComputing.XmlRpcServerV2 3.0.0来构建XmlRpc服务器。

    新建一个控制台项目,在项目中添加对CookComputing.XmlRpcServerV2 3.0.0的引用,可以使用nuget来安装。

    1
    2
    
    PM> Install-Package xmlrpcnet
    PM> Install-Package xmlrpcnet-server
    

    编写服务

    我这里写了个非常简单的服务,代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    
    public class SimpleService : XmlRpcListenerService
    {
      [XmlRpcMethod]
      public int Add(int a, int b)
      {
        return a + b;
      }
    }
    

    编写Service Host相关代码,也就是XmlRpc服务代码

    这里我们通过HttpListener类处理XmlRpc客户端的请求,HttpListener使用的是异步处理,代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
    class Program
    {
      private static XmlRpcListenerService _svc = new SimpleService();
    
      static void Main(string[] args)
      {
          HttpListener listener = new HttpListener();
          listener.Prefixes.Add("http://127.0.0.1:11000/");
          listener.Start();
          listener.BeginGetContext(new AsyncCallback(ProcessRequest), listener);
          Console.ReadLine();
      }
    
      static void ProcessRequest(IAsyncResult result)
      {
          HttpListener listener = result.AsyncState as HttpListener;
          // 结束异步操作
          HttpListenerContext context = listener.EndGetContext(result);
          // 重新启动异步请求处理
          listener.BeginGetContext(new AsyncCallback(ProcessRequest), listener);
          try
          {
              Console.WriteLine("From: " + context.Request.UserHostAddress);
              // 处理请求
              _svc.ProcessRequest(context);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
      }
    }
    

    启动程序后,打开浏览器访问:http://127.0.0.1:11000/就可以看到如下的页面,现在就可以调用XmlRpc服务了。

    XmlRpc服务

  • 相关阅读:
    docker 相关
    mongo 连接方式
    Redis 面试题
    Ubuntu如何挂载U盘
    python try异常处理之traceback准确定位哪一行出问题
    Opencv 基础用法
    CentOS 7 安装MongoDB 4.0(yum方式) 简单方便
    linux中pthread_join()与pthread_detach()详解
    C语言线程池 第三方库
    XML文件删除掉注释
  • 原文地址:https://www.cnblogs.com/jasondan/p/3499195.html
Copyright © 2011-2022 走看看