如何使用微软的Dynamics AX类?

时间:2013-08-08 15:33:45

标签: c# web-services dynamic reporting-services ax

我是报道和.net的新手。我使用 SSRS 完成了报告,但现在我想将条形码添加到报告中。我已经有一个生成条形码图像的Web服务对我来说,但现在只有数据矩阵格式我想在该Web服务中编写方法,这将获得我搜索过的条形码图像或字符串,并且遇到了

Microsoft.Dynamics.AX

课程,但我不知道如何使用它们msdn也没有提供任何帮助,所以我的问题是

1) Is it possible to use the Microsoft.Dynamics.AX classes in to the web service? 
2) If possible how to add references any links will be helpful ?
3) If not then any other way for it ? any links will be helpful ?

我知道有很多第三方工具,但我想知道是否可以通过自编码来完成它?感谢您提前获得帮助。

1 个答案:

答案 0 :(得分:2)

1)是的 2)您需要这些来访问AX内的信息。

using Microsoft.Dynamics.AX.Framework.Linq.Data;
using U23 = Microsoft.Dynamics.AX.ManagedInterop;
using U22 = Microsoft.Dynamics.AX.Framework.Linq.Data;
using Microsoft.Dynamics.Portal.Application.Proxy;

最后,这是我在C#中的访问层的基本布局。

private U23.Session _axSession = new U23.Session();
public U23.Session Connection
{
    get
    {
        return this._axSession;
    }
}
/// <summary>
/// Checks to see if the AX session is connected. If it's null we return false. 
/// Then we check to see if it's logged in, then return true. Otherwise it's not logged in.
/// </summary>
public bool Connected
{
    get
    {
        if (this._axSession == null)
        {
            return false;
        }
        else if (this._axSession.isLoggedOn())
        {
            return true;
        }
        return false;
    }
}

/// <summary>
/// This connects to the AX session. If it's already connected then we don't need to connect
/// again, so we return true. Otherwise we'll try to initiate the session. 
/// </summary>
/// <returns>
/// True: Connection openned successfully, or was already open.
/// False: Connection failed.
/// </returns>
public bool OpenConnection()
{
    if (this.Connected)
    {
        return true;
    }
    else
    {
        try
        {
            _axSession.Logon(null, null, null, null);
            return true;
        }
        catch
        {
            return false;
        }
    }
}

/// <summary>
/// If the session is logged on we will try to close it.
/// </summary>
/// <returns>
/// True: Connection closed successfully
/// False: Problem closing the connection
/// </returns>
public bool CloseConnection()
{
    bool retVal = false;
    if (this.Connection.isLoggedOn())
    {
        try
        {
            _axSession.Logoff();
            retVal = true;
        }
        catch
        {
            retVal = false;
        }
    }
    else
    {
        retVal = true;
    }
    this.Connection.Dispose();
    return retVal;
}

然后,要访问AX中的某些功能,您只需:

public Somethings GetSomethingsById(int id)
    {
        Somethings retVal = new Somethings();
        U22.QueryProvider provider = new U22.AXQueryProvider(null);
        U22.QueryCollection<Somethings> somethings = new U22.QueryCollection<Somethings>(provider);
        var somethings2 = somethings.Where(x => x.SomethingId == id);
        retVal = somethings2.FirstOrDefault();
        return retVal;
    }

最后,如果你想更新一些东西:

public bool UpdateSomething(Something something)
        {
            U23.RuntimeContext.Current.TTSBegin();
            try
            {
                if (something.Count == 0)
                {
                    something.Delete();
                }
                something.Update();
                U23.RuntimeContext.Current.TTSCommit();
                return true;
            }
            catch
            {
                U23.RuntimeContext.Current.TTSAbort();
                return false;
            }
        }

但请确保在计划更新时使用.ForUpdate()选择Something

您还必须将您计划使用的任何对象添加到应用程序代理(例如EPApplicationProxies)并从项目中引用它。