LoginSignup
1
5

More than 5 years have passed since last update.

C#でSOAP呼び出しを行う

Last updated at Posted at 2017-01-08

始めに

C#でSOAP通信を行い、サーバーから取得した値をコンソールに出力するアプリケーションを作ってみます

実装するモジュールの概略

クライアント

Webサーバーから取得した値をコンソールに出力する単純なアプリケーションをC#で作ってみます

Webアプリケーション

アプリケーションサーバーにGlassFish、アプリケーションはJava1.8で作成します
また、GlassFishにDB接続用のデータソース設定を行います

DBサーバー

Oracle12cを使っています

実装するモジュールの詳細について

サーバーアプリの実装

SimpleWeb.java
package simple;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
@WebService
public class SimpleWeb {

      @WebMethod
      public String sayHello(String name) throws Exception{
          return _sayHello(name);
      }

      private String _sayHello(String name)throws Exception{
          try{
              Context ctx = new InitialContext();
              DataSource ds = (DataSource)ctx.lookup("jdbc/oracle12c");

              return ((new QueryRunner())
              .query(ds.getConnection(), //コネクションの取得
                    "select EMP_ID,EMP_NAME from EMP",                 //実行するSQL  zzzz
                    new ArrayListHandler())
              .stream()
              .filter(array->array[0].equals("001"))
              .map(array -> String.format("従業員ID:%s:従業員名:%s",array[0],array[1]))
              .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
              .toString());
          }catch(Exception e){
              e.printStackTrace();
              return name;
          }

      }
}

クライアント(C#)のサービス参照の追加方法について

1.VisualStudioのソリューションエクスプローラーから「Service Reference」を選択し、「サービス参照の追加」を選択する
image

2.「アドレス」に参照先のURLを指定し、移動ボタンを押す
image

3.「サービス」の欄に目的のWebサービスが表示されていることを確認する。
サービス名を変更したい場合は「名前空間」の値を変更する、
image

クライアントの実装

ConsoleApplication4.cs
using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            MyWebService.SimpleWebClient client = new MyWebService.SimpleWebClient();
            Console.WriteLine(client.sayHello("test"));

        }
    }
}

Webアプリケーションから参照されるテーブル

image

クライアントからサーバーアプリを呼び出した実行結果

image

1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5