命令行中的C#代码可以运行,但WinForms代码却无法正常工作。

5
我正在开发一个关于Adobe Echosign的C# winforms应用程序演示。我的代码是他们命令行代码的直接复制(做了一些修改),但是在传输数据后,我的代码会返回一个错误。
以下是EchoSign的命令行代码,它可以正常工作。
public static void sendDocument(string apiKey, string fileName, string formFieldLayerTemplateKey, string recipient)
{
    FileStream file = getTestPdfFile(fileName);
    secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1];
    fileInfos[0] = new secure.echosign.com.FileInfo(fileName, null, file);
    SenderInfo senderInfo = null;
    string[] recipients = new string[1];
    recipients[0] = recipient;
    DocumentCreationInfo documentInfo = new DocumentCreationInfo(
        recipients,
        testPrefix + Path.GetFileName(file.Name),
        testMessage,
        fileInfos,
        SignatureType.ESIGN,
        SignatureFlow.SENDER_SIGNATURE_NOT_REQUIRED
    );
    if (formFieldLayerTemplateKey != null)
    {
        secure.echosign.com.FileInfo[] formFieldLayerTemplates = new secure.echosign.com.FileInfo[1];
        formFieldLayerTemplates[0] = new secure.echosign.com.FileInfo(formFieldLayerTemplateKey);
        documentInfo.formFieldLayerTemplates = formFieldLayerTemplates;
    }
    DocumentKey[] documentKeys;
    documentKeys = ES.sendDocument(apiKey, senderInfo, documentInfo);
    Console.WriteLine("Document key is: " + documentKeys[0].documentKey);
}

这是我的代码块,它返回了他们系统的一个错误:
public static void sendDocument(string apiKey, string fileName, string formFieldLayerTemplateKey, string recipient)
{
    try
    {
        SenderInfo senderInfo = new SenderInfo();
        senderInfo = null;

        FileStream FileToSign = getTestPdfFile(fileName);
        byte[] bytes = System.IO.File.ReadAllBytes("C:\\PROJECTS\\TestFile.pdf");


        secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1];
        fileInfos[0] = new EchoSignTest.secure.echosign.com.FileInfo();
        fileInfos[0].fileName = fileName;
        fileInfos[0].mimeType = null;
        fileInfos[0].file = bytes;

        RecipientInfo[] docRecipient = new RecipientInfo[1];
        docRecipient[0] = new RecipientInfo();
        docRecipient[0].email = recipient;

        DocumentCreationInfo documentInfo = new DocumentCreationInfo();
        documentInfo.recipients = docRecipient;
        documentInfo.name = testPrefix + Path.GetFileName(FileToSign.Name);
        documentInfo.message = testMessage;
        documentInfo.fileInfos = fileInfos;
        documentInfo.signatureType = SignatureType.ESIGN;
        documentInfo.signatureFlow = SignatureFlow.SENDER_SIGNATURE_NOT_REQUIRED;

        if (formFieldLayerTemplateKey != null)
        {
            secure.echosign.com.FileInfo[] formFieldLayerTemplates = new secure.echosign.com.FileInfo[1];

            formFieldLayerTemplates[0] = new secure.echosign.com.FileInfo();
            formFieldLayerTemplates[0].formKey = formFieldLayerTemplateKey;
            documentInfo.formFieldLayerTemplates = formFieldLayerTemplates;
        }

        EchoSignDocumentService19PortTypeClient ES = new EchoSignDocumentService19PortTypeClient();
        DocumentKey[] documentKeys = new DocumentKey[1];

        documentKeys = ES.sendDocument(apiKey, senderInfo, documentInfo);
        Console.WriteLine("Document key is: " + documentKeys[0].documentKey);
    }
    catch (NullReferenceException ex)
    {
        string errMessage = ex.Message;
    }

    catch (Exception ex)
    {
        string errMessage = ex.Message;
    }
}  

两个代码块有什么不同?错误可能在FileInfo[]DocumentCreationInfo()块中。我可能没有按系统要求创建对象。
欢迎提出任何建议。

3
显而易见的区别是第二个代码块被包裹在了一个try/catch块中,这会压制错误并阻止你调试问题。 - Cody Gray
我浏览了代码,代码在没有任何错误的情况下到达了sendDocument行。 - user3929962
1
使用调试器在控制台应用程序中查看 apiKeysenderInfodocumentInfo 的值,然后将其与您在 WinForms 应用程序中获得的值进行比较。它们很可能是不同的。 - Elias
我刚刚完成了再次检查数值的工作... - user3929962
ES.sendDocument调用完成了吗?如果是这样,也许代码的“工作部分”没有任何问题...也许WinForms应用程序运行的上下文中没有有效的输出控制台。您是否尝试在表单版本中使用Debug.WriteLine而不是Console.WriteLine? - Zenilogix
显示剩余7条评论
1个回答

1
错误似乎在于你直接将读取的文档字节分配给fileInfos[0].file变量。在FileInfo文档中指出,文件参数必须是使用base64编码的原始文件内容,但你没有对原始文件内容进行编码。当构造函数以文件流的方式调用时(例如命令行示例中的第一个示例),构造函数似乎会自动处理这个问题。你可以尝试更改Winforms示例中的这些行:
    FileStream FileToSign = getTestPdfFile(fileName);
    byte[] bytes = System.IO.File.ReadAllBytes("C:\\PROJECTS\\TestFile.pdf");


    secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1];
    fileInfos[0] = new EchoSignTest.secure.echosign.com.FileInfo();
    fileInfos[0].fileName = fileName;
    fileInfos[0].mimeType = null;
    fileInfos[0].file = bytes;

将这段代码复制并尝试运行它:

into this code and try if this works:

    FileStream FileToSign = getTestPdfFile(fileName);


    secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1];
    fileInfos[0] = new secure.echosign.com.FileInfo(fileName, null, FileToSign);

你应该使用提供的构造函数而不是直接赋值,以确保所有变量/参数都被正确处理。
你在评论中提到的关于不带3个参数的构造函数的错误可能是由于在构造函数调用之前添加了前缀,因为这似乎是你自己程序的命名空间,而不是提供的API的正确命名空间。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接