SQL Compact 只允许一个 WCF 客户端
我写了一个小聊天应用程序。为了保存用户名和密码等一些信息,我将数据存储在 SQL-Compact 3.5 SP1 数据库中。
一切工作正常,但如果另一个(同一台机器上的相同.exe)客户端想要访问该服务。它来自第二个客户端的 ServiceReference.Class.Open(),出现 EndpointNotFound 异常。
因此,我删除了 CE 数据访问代码,并且没有收到任何错误(带有 if (false))
问题出在哪里? 我用谷歌搜索了这个,但似乎没有人跟我得到同样的错误:(
解决方案
我使用了包装器 http://csharponphone.blogspot.com/2007/ 01/keeping-sqlceconnection-open-and-thread.html 为了威胁安全,现在它可以工作了:)
客户端代码:
public test()
{
var newCompositeType = new Client.ServiceReference1.CompositeType();
newCompositeType.StringValue = "Hallo" + DateTime.Now.ToLongTimeString();
newCompositeType.Save = (Console.ReadKey().Key == ConsoleKey.J);
ServiceReference1.Service1Client sc = new Client.ServiceReference1.Service1Client();
sc.Open();
Console.WriteLine("Save " + newCompositeType.StringValue);
sc.GetDataUsingDataContract(newCompositeType);
sc.Close();
}
服务器代码
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.Save)
{
SqlCeConnection con = new SqlCeConnection(Properties.Settings.Default.Con);
con.Open();
var com = con.CreateCommand();
com.CommandText = "SELECT * FROM TEST";
SqlCeResultSet result = com.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
var rec = result.CreateRecord();
rec["TextField"] = composite.StringValue;
result.Insert(rec);
result.Close();
result.Dispose();
com.Dispose();
con.Close();
con.Dispose();
}
return composite;
}
I write a little Chat Application. To save some infos like Username and Password I store the Data in an SQL-Compact 3.5 SP1 Database.
Everything working fine, but If another (the same .exe on the same machine) Client want to access the Service. It came an EndpointNotFound exception, from the ServiceReference.Class.Open() at the Second Client.
So i remove the CE Data Access Code and I get no Error (with an if (false))
Where is the Problem?
I googled for this, but no one seems the same error I get :(
SOLUTION
I used the wrapper in
http://csharponphone.blogspot.com/2007/01/keeping-sqlceconnection-open-and-thread.html
for threat safty, and now it works :)
Client Code:
public test()
{
var newCompositeType = new Client.ServiceReference1.CompositeType();
newCompositeType.StringValue = "Hallo" + DateTime.Now.ToLongTimeString();
newCompositeType.Save = (Console.ReadKey().Key == ConsoleKey.J);
ServiceReference1.Service1Client sc = new Client.ServiceReference1.Service1Client();
sc.Open();
Console.WriteLine("Save " + newCompositeType.StringValue);
sc.GetDataUsingDataContract(newCompositeType);
sc.Close();
}
Server Code
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.Save)
{
SqlCeConnection con = new SqlCeConnection(Properties.Settings.Default.Con);
con.Open();
var com = con.CreateCommand();
com.CommandText = "SELECT * FROM TEST";
SqlCeResultSet result = com.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
var rec = result.CreateRecord();
rec["TextField"] = composite.StringValue;
result.Insert(rec);
result.Close();
result.Dispose();
com.Dispose();
con.Close();
con.Dispose();
}
return composite;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在处置
con
对象之前,您不会关闭连接。尝试:
You're not closing the connection before disposing the
con
object.Try:
可能是第二个客户端连接时服务初始化期间发生异常。在运行第二个 exe 的同时调试服务,
在 VS 中设置异常行为,以在引发公共语言运行时异常以及未处理异常时中断,您将看到错误。
正如 tomlog 的回答所述 - 这可能是因为您没有正确关闭连接。
Could be an exception occuring during the service initialisation when the second client connects. Debug the service at the same time as running the second exe,
Set exception behaviour in VS to break when Common Language Runtime Exceptions are thrown as well as when they're unhandled and you'll see the error.
As tomlog's answer states - it could be because you're not closing the connection properly.
我在 http://csharponphone.blogspot 中使用了包装器。 com/2007/01/keeping-sqlceconnection-open-and-thread.html 用于威胁安全,现在它可以工作了:)
I used the wrapper in http://csharponphone.blogspot.com/2007/01/keeping-sqlceconnection-open-and-thread.html for threat safty, and now it works :)