biz-Streamマニュアル Webサービスサーバ ガイド 第12章 サンプルプログラム(.NET) 12.11 リポジトリにディレクトリを作成しファイルを格納するサンプル(WCF)

12.11 リポジトリにディレクトリを作成しファイルを格納するサンプル(WCF)

概要

リポジトリにディレクトリを作成しファイルを格納するサンプルプログラムです。


環境準備

事前に『2.7 クライアントアプリケーションの環境構築』を確認してください。


サンプル構成

項目 ファイルパス
ソースサンプル <bswss-client_home>/sample/dotNET/wcf/WSS_WCF_Sample11.aspx.cs サンプル1


ソースサンプル



using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web;
using BizStreamWebApplication.CmsServiceReference;

namespace BizStreamWebApplication
{
    public partial class WSS_WCF_Sample11 : System.Web.UI.Page
    {
        // Webサービスサーバのホスト名
        const string WSS_HOST_NAME = "wsssrv";

        // Webサービスのエンドポイントプリフィックス
        const string WSS_SERVICE_LOCATION = "http://" + WSS_HOST_NAME + ":8080/axis2/services";

        // BASIC認証のユーザ名
        const string USER_NAME = "bizuser1";

        // BASIC認証のパスワード
        const string PASSWORD = "bizuser1";

        // 監査ロギング用のユーザ定義
        const string USER_DEF = "AAA";

        // リポジトリ内に作成するディレクトリ名
        const string MKDIR_NAME = "/Sample11_WCF";

        // 入力元ファイルパス
        const string INPUT_FILE_PATH = @"C:\\Temp\\WSS_WCF_Sample7.csv";

        // リポジトリへの出力先
        const string TARGET_FILE_PATH = "/Sample11_WCF/WSS_WCF_Sample11.csv";


        protected void Page_Load(object sender, EventArgs e)
        {
            // リポジトリにディレクトリを作成
            makeDir();

            // 作成したリポジトリにファイルを格納
            putData();

            // ブラウザに結果を表示
            Response.Clear();
            Response.ContentType = "text/html;charset=UTF-8";
            Response.Write("<HTML><HEAD><TITLE>WSS_WCF_Sample11</TITLE></HEAD><BODY>");
            Response.Write("正常終了しました。<BR>");
            Response.Write("リポジトリに作成されたディレクトリ: " + MKDIR_NAME + "<BR>");
            Response.Write("リポジトリに格納されたファイル: " + INPUT_FILE_PATH + " -> " + TARGET_FILE_PATH);
            Response.Write("</BODY></HTML>");
            Response.Flush();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

        private success makeDir()
        {
            // クライアントを生成
            cmsPortTypeClient client =
                new cmsPortTypeClient("CmsSOAP11port",
                    WSS_SERVICE_LOCATION + "/cms?UserDef=" + USER_DEF);

            // 認証の設定
            BindingElementCollection elements = client.Endpoint.Binding.CreateBindingElements();
            elements.Find<HttpTransportBindingElement>().AuthenticationScheme =
                System.Net.AuthenticationSchemes.Basic;
            client.ClientCredentials.UserName.UserName = USER_NAME;
            client.ClientCredentials.UserName.Password = PASSWORD;

            // MTOMを有効化
            elements.Remove<TextMessageEncodingBindingElement>();
            MtomMessageEncodingBindingElement mtom = new MtomMessageEncodingBindingElement();
            mtom.ReaderQuotas.MaxStringContentLength = 100000;
            elements.Insert(0, mtom);
            elements.Find<HttpTransportBindingElement>().KeepAliveEnabled = false;
            client.Endpoint.Binding = new CustomBinding(elements);

            // リクエストメッセージの作成
            mkdirRequest request = new mkdirRequest();
            request.Value = MKDIR_NAME;

            // リクエストを送信
            return client.mkdir(request);
        }

        private success putData()
        {
            // クライアントを生成
            cmsPortTypeClient client =
                new cmsPortTypeClient("CmsSOAP11port",
                    WSS_SERVICE_LOCATION + "/cms?UserDef=" + USER_DEF);

            // 認証の設定
            BindingElementCollection elements = client.Endpoint.Binding.CreateBindingElements();
            elements.Find<HttpTransportBindingElement>().AuthenticationScheme =
                System.Net.AuthenticationSchemes.Basic;
            client.ClientCredentials.UserName.UserName = USER_NAME;
            client.ClientCredentials.UserName.Password = PASSWORD;

            // MTOMを有効化
            elements.Remove<TextMessageEncodingBindingElement>();
            MtomMessageEncodingBindingElement mtom = new MtomMessageEncodingBindingElement();
            mtom.ReaderQuotas.MaxStringContentLength = 100000;
            elements.Insert(0, mtom);
            elements.Find<HttpTransportBindingElement>().KeepAliveEnabled = false;
            client.Endpoint.Binding = new CustomBinding(elements);

            // リクエストメッセージの作成
            fileData request = new fileData();
            request.name = TARGET_FILE_PATH;

            base64Binary inputData = new base64Binary();
            // ファイルの内容をバイト配列にすべて読み込む
            byte[] data = File.ReadAllBytes(INPUT_FILE_PATH);
            inputData.Value = data;
            request.contentData = inputData;

            // リクエストを送信
            return client.put(request);
        }
    }
}