次のコードを使用しています。
private WSHttpBinding ws;
private EndpointAddress Srv_Login_EndPoint;
private ChannelFactory<Srv_Login.Srv_ILogin> Srv_LoginChannelFactory;
private Srv_Login.Srv_ILogin LoginService;
ログインは私のコンストラクタです:
public Login()
{
InitializeComponent();
ws = new WSHttpBinding();
Srv_Login_EndPoint = new EndpointAddress("http://localhost:2687/Srv_Login.svc");
Srv_LoginChannelFactory = new ChannelFactory<Srv_Login.Srv_ILogin>(ws, Srv_Login_EndPoint);
}
そして、私はこのようにサービスを使用しています:
private void btnEnter_Click(object sender, EventArgs e)
{
try
{
LoginService = Srv_LoginChannelFactory.CreateChannel();
Srv_Login.LoginResult res = new Srv_Login.LoginResult();
res = LoginService.IsAuthenticated(txtUserName.Text.Trim(), txtPassword.Text.Trim());
if (res.Status == true)
{
int Id = int.Parse(res.Result.ToString());
}
else
{
lblMessage.Text = "Not Enter";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Srv_LoginChannelFactory.Close();
}
}
ユーザーが有効なユーザー名とパスワードを入力すると、すべて問題ありません。ユーザーが間違ったユーザー名とパスワードを入力すると、最初の試行では「Not Enter」メッセージが正しく表示されますが、2 回目の試行では次のメッセージが表示されます。
{System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.ServiceModel.ChannelFactory`1[Test_Poosesh.Srv_Login.Srv_ILogin]'.
at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposed()
at System.ServiceModel.ChannelFactory.EnsureOpened()
at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
at System.ServiceModel.ChannelFactory`1.CreateChannel()
このエラーが発生しないようにコードを修正するにはどうすればよいですか?