尝试在Droid项目中调用WCF服务时收到以下异常:
"由于对象的当前状态"
,操作无效
我不确定我做错了什么。
我在执行RPC(远程过程调用)之前等待我的Open事件。
我等到RPC完成后再尝试关闭连接。
代码:
public class EmailClient : IEmailClient
{
FasadeClient _client = null;
public void Send(string title, string fromAddress, string toAddress, string content)
{
Activate();
var message = new Messaging.Message()
{
Subject = title,
From = fromAddress,
To = toAddress,
Body = content
};
_client.OpenCompleted += (se, ev) => _client.SendEmailAsync(message, true);
_client.SendEmailCompleted += (se, ev) => Deactivate();
}
void Activate()
{
var binding = CreateBasicHttp();
var endpoint = new EndpointAddress(@"http://www.some_domain.com/some_service.svc");
_client = new FasadeClient(binding, endpoint);
_client.OpenAsync();
}
void Deactivate() => _client.Close();
BasicHttpBinding CreateBasicHttp()
{
BasicHttpBinding binding = new BasicHttpBinding
{
Name = "basicHttpBinding",
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
TimeSpan timeout = new TimeSpan(0, 0, 30);
binding.SendTimeout = timeout;
binding.OpenTimeout = timeout;
binding.ReceiveTimeout = timeout;
return binding;
}
}