0
votes

We've created a custom webservice in Umbraco to add (async) files and upload them. After upload the service is called with node and file-information to add a new node to the content tree.

At first our main problem was that the service was running outside of the Umbraco context, giving strange errors with get_currentuser.

Now, we inherit the umbraco BaseWebService from the umbraco.webservices dll and we've set all acces information in the settings file; we authenticatie before doing anything else using (correct and ugly-hardcoded) administrator.

When we now execute the webservice (from the browser or anything else) we get:

at umbraco.DataLayer.SqlHelper`1.ExecuteReader(String commandText, IParameter[] parameters)
   at umbraco.cms.businesslogic.CMSNode.setupNode()
   at umbraco.cms.businesslogic.web.Document.setupNode()
   at umbraco.cms.businesslogic.CMSNode..ctor(Int32 Id)
   at umbraco.cms.businesslogic.Content..ctor(Int32 id)
   at umbraco.cms.businesslogic.web.Document..ctor(Int32 id)
   at FileUpload.AddDocument(String ProjectID, String NodeID, String FileName)*

Where AddDocument is our method. The node (filename w/o extension) does not exist in the tree (not anywhere, it's a new filename/node). We've cleared the recycle bin, so it's not in there either.

Are we missing something vital, does anyone has a solution?

Below is the source for the webservice;

using umbraco.cms.businesslogic.web;
using umbraco.BusinessLogic;
using umbraco.presentation.nodeFactory;
using umbraco.cms.businesslogic.member;
using umbraco.cms;

/// <summary>
/// Summary description for FileUpload
/// </summary>
[WebService(Namespace = "http://umbraco.org/webservices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class FileUpload : umbraco.webservices.BaseWebService //System.Web.Services.WebService
{

    private string GetMimeType(string fileName)
    {
        string mimeType = "application/unknown";
        string ext = System.IO.Path.GetExtension(fileName).ToLower();
        Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
        if (regKey != null && regKey.GetValue("Content Type") != null)
            mimeType = regKey.GetValue("Content Type").ToString();
        return mimeType;
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }


    [WebMethod]
    public void AddDocument(string ProjectID, string NodeID, string FileName)
    {
        Authenticate("***", "***");
        string MimeType = GetMimeType(FileName); //"application/unknown";

        // Create node
        int nodeId = 1197; 
        string fileName = System.IO.Path.GetFileNameWithoutExtension(@"*****\Upload\" + FileName);

        string secGroups = "";

//EDIT DUE TO COMMENT: Behavior remains the same though
        Document node = umbraco.cms.businesslogic.web.Document.MakeNew(fileName.Replace(".", ""), new DocumentType(1049), umbraco.BusinessLogic.User.GetUser(0), nodeId);


        secGroups = "Intern";

        StreamWriter sw = null;
        try
        {
//EXCEPTION IS THROWN SOMEWHERE HERE
            Document doc = NodeLevel.CreateNode(fileName, "Bestand", nodeId);
            doc.getProperty("bestandsNaam").Value = fileName;
            byte[] buffer = System.IO.File.ReadAllBytes(@"****\Upload\" + FileName);

            int projectId = 0;
            int tempid = nodeId;
//EXCEPTION IS THROWN TO THIS POINT (SEE BELOW)


            try
            {
                Access.ProtectPage(false, doc.Id, 1103, 1103);
                Access.AddMembershipRoleToDocument(doc.Id, secGroups);
            }
            catch (Exception ex)
            {
        // write to file
            }

            try
            {
                doc.Publish(umbraco.BusinessLogic.User.GetUser(0));
                umbraco.library.UpdateDocumentCache(doc.Id);

                umbraco.content.Instance.RefreshContentFromDatabaseAsync();
            }
            catch (Exception ex)
            {
        // write to file
            }
            System.IO.File.Delete(FileName);

        }
        catch (Exception ex)
        {
            // THIS EXCEPTION IS CAUGHT!!
        }
    }
    public override umbraco.webservices.BaseWebService.Services Service
    {
        get { return umbraco.webservices.BaseWebService.Services.DocumentService; }
    }
}

If anyone has a solution, pointer, hint or whatever; help is appreciated!!

TIA, riffnl

1
The eception seems to occur at the row "Document node = new Document(nodeId);" (according to the callstack), where nodeId = 1197. Is there a node with id 1197 in your content tree? Can you use SQL Server Profiler to check executed SQL? Does it execute without errors (and finds a document?) - Andreas Paulsson
what exception is thrown? looks like only the stack trace is there... - Jon
@Andreas; you were right. The document 1197 did exist, although it's supposed to be it's parent. We've edited the code, the behavior remains the same though - riffnl
@Jon this is just about the only thing we can see besides request timed out. - riffnl
Do you get a timeout from the database? Could you get a SQL profile trace to find what SQL that is timing out? We need more information about the exception. As @Jon says, you only show us the call stack above, not the exception itself. - Andreas Paulsson

1 Answers

0
votes

We've rewritten the whole procedure (dumped all code and restart) and we've got it working now.

I think we've been messing around with the old code so much in trying to get it to work we were missing some key issues, because it functions.

Thanks for thinking along anyway!