WCF返回的客户端中的访问对象

时间:2016-04-14 10:52:57

标签: c# wcf

我有一个返回类对象的WCF服务。如何在客户端应用程序中访问这些返回值。

服务代码:

public ET_ITAM_RequestDetails GetAssociateFreewareRequestDetails(ET_ITAM_RequestDetails objET_ITAM_RequestDetails)
{
    SqlDataReader rdr = null;
    connect.Open();
    SqlCommand cmd = new SqlCommand("ET_ITAM_GetAssociateFreewareRequestDetails", connect);
    cmd.CommandType = CommandType.StoredProcedure;

    while (rdr.Read())
    {
        objET_ITAM_RequestDetails.AssociateID = (string)rdr[0];
        objET_ITAM_RequestDetails.AssetID = (string)rdr[1];
        objET_ITAM_RequestDetails.ETRequestID = (int)rdr[2];
        objET_ITAM_RequestDetails.FreewareName = (string)rdr[3];
        objET_ITAM_RequestDetails.InstallationCommand = (string)rdr[4];
        objET_ITAM_RequestDetails.InstallationArguments = (string)rdr[5];
        objET_ITAM_RequestDetails.VerificationType = (bool)rdr[6];
        objET_ITAM_RequestDetails.VerificationPath = (string)rdr[7];
    }

    return objET_ITAM_RequestDetails;
}

在客户端:

ServiceReference1.ET_ITAM_RequestDetails objItam = new 

ServiceReference1.ET_ITAM_RequestDetails();

// need to get return value.
// if i create another object it not work as expected
obj_service.GetAssociateFreewareRequestDetails(objItam);

1 个答案:

答案 0 :(得分:1)

根据ServiceReference1来判断,您已成功将WCF引用添加到客户端解决方案中。但是,据我所知,你错过了WCF客户端。

您没有提供足够的信息来了解您的客户端会被调用,但您的代码应该类似于:

ServiceReference1.ET_ITAM_RequestDetails objItam = new 
    ServiceReference1.ET_ITAM_RequestDetails();

// the service generation will create a WCF client for you, thought I'm not sure what your client's name would be.
objItam = obj_service.GetAssociateFreewareRequestDetails(objItam);

再次查看您的代码,我想我看到了您的问题。你做了新的wcf客户端,但没有提供代码。

obj_service.GetAssociateFreewareRequestDetails(objItam);

您只是调用该函数,但不会将其值返回给您的对象。

objItam = obj_service.GetAssociateFreewareRequestDetails(objItam);

我不清楚你为什么要新建一个空对象,将它传递给你的函数,然后返回它。为什么不把它新起来并在函数中返回呢?

public ET_ITAM_RequestDetails GetAssociateFreewareRequestDetails()
{
    ET_ITAM_RequestDetails objET_ITAM_RequestDetails = new ET_ITAM_RequestDetails();

    SqlDataReader rdr = null;
    connect.Open();
    SqlCommand cmd = new SqlCommand("ET_ITAM_GetAssociateFreewareRequestDetails", connect);
    cmd.CommandType = CommandType.StoredProcedure;

    while (rdr.Read())
    {
        objET_ITAM_RequestDetails.AssociateID = (string)rdr[0];
        objET_ITAM_RequestDetails.AssetID = (string)rdr[1];
        objET_ITAM_RequestDetails.ETRequestID = (int)rdr[2];
        objET_ITAM_RequestDetails.FreewareName = (string)rdr[3];
        objET_ITAM_RequestDetails.InstallationCommand = (string)rdr[4];
        objET_ITAM_RequestDetails.InstallationArguments = (string)rdr[5];
        objET_ITAM_RequestDetails.VerificationType = (bool)rdr[6];
        objET_ITAM_RequestDetails.VerificationPath = (string)rdr[7];
    }

    return objET_ITAM_RequestDetails;
}

如果你的读者有多行,那么上面(和你的原文)当然会有(可能)意想不到的结果 - 如果你不知道的话就打电话给你。