La creazione di pagina di pubblicazione in nel processo timer di SharePoint getta MissingMethodException

StackOverflow https://stackoverflow.com/questions/3761458

Domanda

che sto facendo un semplice importatore notizia per un sito di SharePoint utilizzando processo timer che viene eseguito ogni pochi minuti. Il seguente codice viene eseguito bene se l'eseguo in una parte web, ma getta MissingMethodException quando correva nel processo timer è metodo Execute.

L'eccezione completo:

System.MissingMethodException was unhandled by user code
  Message="No parameterless constructor defined for this object."
  Source="mscorlib"
  StackTrace:
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
       at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at System.Activator.CreateInstance(Type type)
       at Microsoft.SharePoint.WebPartPages.SPWebPartSerializer.get_DefaultControl()
       at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(PersonalizationScope scope)
       at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.get_Links()
       at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartToStore(WebPart webPart, Int32 viewId, String viewGuid)
       at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart superset, Boolean throwIfLocked)
       at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPartInternal(WebPart webPart, String zoneId, Int32 zoneIndex, Boolean throwIfLocked)
       at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPart(WebPart webPart, String zoneId, Int32 zoneIndex)
       at Microsoft.SharePoint.Publishing.PublishingPage.CopyAllWebParts(String destinationPageUrlServerRelative, SPWeb destinationPageWeb, String sourcePageUrlServerRelative, SPWeb sourcePageWeb, Boolean shouldOverwriteDestinationWebParts)
       at Microsoft.SharePoint.Publishing.PublishingPageCollection.<>c__DisplayClass5.<Add>b__0()
       at Microsoft.Office.Server.Diagnostics.FirstChanceHandler.ExceptionFilter(Boolean fRethrowException, TryBlock tryBlock, FilterBlock filter, CatchBlock catchBlock, FinallyBlock finallyBlock)
       at Microsoft.Office.Server.Diagnostics.ULS.SendWatsonOnExceptionTag(ULSTagID tagID, ULSCat categoryID, String output, Boolean fRethrowException, TryBlock tryBlock, CatchBlock catchBlock, FinallyBlock finallyBlock)
       at Microsoft.SharePoint.Publishing.PublishingPageCollection.Add(String name, PageLayout layout)
       at Virtua.SharePoint.Mercator.NewsImportJob.Execute(Guid contentDbId)
       at Microsoft.SharePoint.Administration.SPTimerJobInvoke.Invoke(TimerJobExecuteData& data, Int32& result)
  InnerException: 

Il codice:

var SPWebUri = new Uri("http://.../News/");
using (SPSite site = new SPSite(SPWebUri.AbsoluteUri))
{
    using (SPWeb web = site.OpenWeb(SPWebUri.AbsolutePath)) // tried 0x29A's anwser, but it didn't work
    // using (SPWeb web = site.OpenWeb())
    {
        web.AllowUnsafeUpdates = true;

        PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
        SPContentTypeId contentTypeId = new SPContentTypeId("...");

        PageLayout layout = publishingWeb.GetAvailablePageLayouts(contentTypeId).First(l => l.Name == "....aspx");
        PublishingPageCollection pages = publishingWeb.GetPublishingPages();

        var spFilename = "testing";

        // error line below
        var newPage = pages.Add(String.Format("{0}.aspx", spFilename), layout);

        // set newPage's fields

        // check-in and publish page
    }
}
È stato utile?

Soluzione 2

Il problema era una web part difettosa sulla mia layout di pagina. Stavo usando SPContext.Current... nel costruttore. Ora, a quanto pare, roba SPContext semplicemente non è lì quando l'esecuzione di codice processo timer (non così sorprendente o).

Dal momento che l'errore non è stata gestita, web part direttore ha cercato di sostituire la mia parte con una ErrorWebPart (quello normalmente dire all'utente qualcosa è andato storto). Per qualche ragione questo funziona bene quando si visualizza la pagina nel browser. Ma nel processo timer, SharePoint cerca di creare ErrorWebPart attraverso la riflessione utilizzando senza parametri costruttore che è non definita in ErrorWebPart e il tutto semplicemente crolla con MissingMethodException.

Spostare il codice incriminato per il metodo CreateChildControls (dove ho la gestione degli errori) risolto il problema.

Altri suggerimenti

Non sono sicuro, ma potrebbe essere che è necessario fornire il parametro URL per site.OpenWeb () Se il parametro url è vuoto, il sito di livello superiore deve essere aperto. Il che può spiegare perché funziona in una parte web, ma non dal processo timer. Vedere MSDN -. Site.OpenWeb Metodo per i dettagli

Saluti

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top