fc2ブログ

記事一覧

FTP接続時に Cannot access a disposed object. のエラーが発生する件 | FtpWebRequest


.Net Frameworkに含まれているFtpWebRequestを使用する際に以下のようなエラーが発生する場合がありますので、対応方法についてご紹介いたします。

Unhandled managed exception:
Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'. (System.ObjectDisposedException)




ftpwebrequest_error_01.png


前提条件
・Windows10 Pro 64Bit
・Visual Studio 2015 Community Update3
・.Net Framework 4.5
・VB.net / C#



1.原因

以下の例のようにFtpWebRequestを使いまわしているのに、FtpWebResponseをusing区で囲っていないメソッドを繰り返し使用すると発生します。

'FtpWebRequestの作成
Private _ftpReq As System.Net.FtpWebRequest = Nothing

Sub FtpMethod()

    'ダウンロードするファイルのURI
    Dim u As New Uri("ftp://localhost/test.txt")
    _ftpReq = DirectCast(System.Net.WebRequest.Create(u), _
                         System.Net.FtpWebRequest)

    '要求の完了後に接続を閉じない
    _ftpReq.KeepAlive = True
    'ディレクトリ情報を取得する
    _ftpReq.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails

    'FtpWebResponseを取得
    Dim ftpRes As System.Net.FtpWebResponse = _
                  DirectCast(_ftpReq.GetResponse(), _
                        System.Net.FtpWebResponse)

    ftpRes.Close();

End Sub



2.対応方法

必ずFtpWebResponseをUsing区で囲み、インスタンスを確実に破棄します。

'FtpWebRequestの作成
Private _ftpReq As System.Net.FtpWebRequest = Nothing

Sub FtpMethod()

    'ダウンロードするファイルのURI
    Dim u As New Uri("ftp://localhost/test.txt")
    _ftpReq = DirectCast(System.Net.WebRequest.Create(u), _
                         System.Net.FtpWebRequest)

    '要求の完了後に接続を閉じない
    _ftpReq.KeepAlive = True
    'ディレクトリ情報を取得する
    _ftpReq.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails

    'FtpWebResponseを取得
    Using ftpRes As System.Net.FtpWebResponse = _
                    DirectCast(_ftpReq.GetResponse(), _
                          System.Net.FtpWebResponse)

        ftpRes.Close();
    End Using

End Sub



FTPのサンプルコード例は以下のサイトを参考ください。
https://dobon.net/vb/dotnet/internet/ftpwebrequest.html

次回の記事でFtpWebRequestを使用する際の注意点をご紹介しております。





最後までお読みいただき、ありがとうございます。
他にも VB.net に関する記事 をご紹介しておりますのでご参考頂ければ幸いです。



関連記事

コメント

コメントの投稿

※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。

 入力されていないコメントには返信しませんのであらかじめご了承くださいませ。

※ニックネームでも良いので必ずご入力ください。

    

※必ずご入力ください。

    
    

※必ずご入力ください。

※技術的な質問には環境やエラーについて正確かつ詳細にお教えください。

・正確なエラーの内容

・Windowsのバージョン番号

・Visual Studioのバージョン

・機器の型番

・アプリやソフトのバージョン

    

カテゴリ別記事一覧

広告

プロフィール

石河 純


著者名 :石河 純
自己紹介:素人上がりのIT技術者。趣味は卓球・車・ボウリング

IT関連の知識はざっくりとこんな感じです。
【OS関連】
WindowsServer: 2012/2008R2/2003/2000/NT4
Windows: 10/8/7/XP/2000/me/NT4/98
Linux: CentOS RedHatLinux9
Mac: macOS Catalina 10.15 / Mojave 10.14 / High Sierra 10.13 / Sierra 10.12 / OSX Lion 10.7.5 / OSX Snow Leopard 10.6.8
【言語】
VB.net ASP.NET C#.net Java VBA
Xamarin.Forms
【データベース】
Oracle 10g/9i
SQLServer 2016/2008R2/2005/2000
SQLAnywhere 16/11/8
【BI/レポートツール】
Cognos ReportNet (IBM)
Microsoft PowerBI
ActiveReport (GrapeCity)
CrystalReport
【OCX関連】
GrapeCity InputMan SPREAD MultiRow GridView
【ネットワーク関連】
CCNP シスコ技術者認定
Cisco Catalyst シリーズ
Yamaha RTXシリーズ
FireWall関連
【WEB関連】
SEO SEM CSS jQuery IIS6/7 apache2

休みの日は卓球をやっています。
現在、卓球用品通販ショップは休業中です。