本文介绍了WCF,MySQL和交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Communication Foundation的服务器应用程序具有MySql.Data.MySqlClient.MySqlTransaction的一个问题。

I am having a problem in MySql.Data.MySqlClient.MySqlTransaction in a Windows Communication Foundation Server Application.

我使用的是实现了的ServiceContract接口的类的方法内的MySqlTransaction,当我尝试运行服务,它抛出这个错误:

I am using the MySqlTransaction inside the method of the class that implements the ServiceContract Interface and it throws this error when I try to run the service:

System.Runtime.Serialization.InvalidDataContractEx​​ception :输入'MySql.Data.MySqlClient.MySqlTransaction不能被序列化。考虑与DataContractAttribute属性将其标记和标记所有成员想要序列化的DataMemberAttribute属性。如果类型是一个集合,考虑与CollectionDataContractAttribute其标记。

我的code在WCF服务是simmilar这样:

My code in WCF service was simmilar to this:

[ServiceContract]
public interface IDALService
{
    MySqlCommand Command { [OperationContract] get; [OperationContract] set; }

    [OperationContract]
    void Execute(string cmdText);
}

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class DALService : IDALService
{
    public MySqlCommand Command { get; set; }

    public DALService()
    {
        Command = null;
    }

    public void Execute(string cmdText)
    {
        MySqlCommand SQLCom = this.Command??new MySqlCommand();
        SQLCom.CommandText = cmdText;

        if (this.Command == null)
        {
            SQLCom.CommandType = CommandType.Text;
            SQLCom.Connection = new MySqlConnection(/* ... */);
            SQLCom.Connection.Open();
            SQLCom.Transaction = SQLCom.Connection.BeginTransaction();
        }

        if (this.Command == null)
        {
            try
            {
                SQLCom.ExecuteScalar();
                SQLCom.Transaction.Commit();
            }
            catch { SQLCom.Transaction.Rollback(); throw; }
            finally { SQLCom.Connection.Close(); }
        }
        else SQLCom.ExecuteScalar();
    }
}

出现的错误只需键入的http://本地主机:1257 / DALService.svc 在我Web浏览器。

The error occur just by typing the http://localhost:1257/DALService.svc in my web browser.

我使用的框架4,code在C#中,在Microsoft Visual Studio 2010专业版。

I am using Framework 4, code in C#, in Microsoft Visual Studio 2010 Pro.

请帮忙。先谢谢了。

推荐答案

这不可能是code这是造成问题。你所得到的错误是由试图从服务返回或传递一个MySqlTransaction到/到来。这只是简单地行不通的。

This can't be the code that's causing an issue. The error you are getting is coming from an attempt to return or pass in a MySqlTransaction to/from the service. That's just simply not going to work.

另外,为什么在地球上,你通过公共财产暴露Command对象外面的世界?此外,它甚至不出现,你使用它...删除属性并保持你的命令,作用于使用它的方法。如果不这样做,你运行该服务作为一个单身,你会得到很多疯狂的虫子。

Also, why on earth are you exposing a Command object to the outside world via a public property? Furthermore, it doesn't even appear that you use it... Delete that property and keep your command scoped to the method that uses it. If you don't and you run this service as a singleton, you'll get lots of crazy bugs.

甚至还...这是一个非常危险的服务来公开。如果你有别人使用自己比那个以外,它提供了零封装。哎呀,你还不如干脆直接打开一个端口到SQL Server,一样傻乎乎的,听起来。

Even furthermore... This is an extremely dangerous service to expose. If you were to have someone use it other than yourself, it provides zero encapsulation. Heck, you might as well just open up a port directly to the SQL Server, as dumb as that sounds.

这篇关于WCF,MySQL和交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 14:17