开发者

SQL Compact allow only one WCF Client

开发者 https://www.devze.com 2022-12-16 06:50 出处:网络
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.

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 Cl开发者_Python百科ient.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;
}


You're not closing the connection before disposing the con object.

Try:

con.Close();
con.Dispose();


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.


I used the wrapper in http://csharponphone.blogspot.com/2007/01/keeping-sqlceconnection-open-and-thread.html for threat safty, and now it works :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号