views:

24

answers:

1

Just trying to do a (I thought) simple callout to read a number from a text file stored on the CRM server, use it as one of the values on the CRM form, and increment the number and then rewrite it to the text file. When I load the form, the callout ostensibly does nothing and Event Viewer on the server gives me this unhelpful invalid cast error message. I've gone over the code and changed various things to no avail, but I'm brand new to both CRM callouts and C#, so I'm probably missing something dumb. Here's the code:

using System;
using System.IO;
using Microsoft.Crm.Callout;

namespace IncrementCompetitorNumber
{
    public class Increment
    {
        public string IncrementNumber()
        {
            string ProjectAutoNumber = "D:\\CRM_Misc\\incrementers\\new_competitornumber.txt";
            string AutoNumber = "0"; 
            string ReturnThis = "0";
            int i = 0;

            lock(this)

            {

            TextReader tr = new StreamReader(ProjectAutoNumber);

            AutoNumber = tr.ReadLine();

            tr.Close();

            ReturnThis = AutoNumber;

            i = Convert.ToInt32(AutoNumber);

            i++;

            AutoNumber = i.ToString();

            TextWriter tw = new StreamWriter(ProjectAutoNumber);

            tw.WriteLine(AutoNumber);

            tw.Close();

            }

            return ReturnThis;
        }
    }
}

So... anyone know what I'm doing wrong?

A: 

There is nothing in the codde you have posted the would cause an invalid cast exception, what line does the exception occur on?

One thing to mention is that the code you have posted is not a CRM 3.0 callout.

A CRM 3.0 callout class has to inherit from CrmCalloutBase and you then have overide one of the various event methods like PostUpdate. Have you done this else where and are calling this class from there?

OK from your second comment i know what you are doing wrong. You have not setup your class corectly. I assume you want to do something with the returned string at some point but i have ignored that for now and the value will just be discarded.

Change it as follows:

using System;
using System.IO;
using Microsoft.Crm.Callout;

namespace IncrementCompetitorNumber 
{
    public class Increment : CrmCalloutBase
    {
        public override void PostCreate(CalloutUserContext userContext, CalloutEntityContext entityContext, string postImageEntityXml)
        {
            IncrementNumber();
        }

        private string IncrementNumber()
        {
            string ProjectAutoNumber = "D:\\CRM_Misc\\incrementers\\new_competitornumber.txt";
            string AutoNumber = "0"; 
            string ReturnThis = "0";
            int i = 0;

            lock(this)

            {

            TextReader tr = new StreamReader(ProjectAutoNumber);

            AutoNumber = tr.ReadLine();

            tr.Close();

            ReturnThis = AutoNumber;

            i = Convert.ToInt32(AutoNumber);

            i++;

            AutoNumber = i.ToString();

            TextWriter tw = new StreamWriter(ProjectAutoNumber);

            tw.WriteLine(AutoNumber);

            tw.Close();

            }

            return ReturnThis;
        }
    }
}
Ben Robinson
Unfortunately, I have no idea what line the error occurs on. The only place I'm getting an error is in EventViewer, and this is all it says: Error: ISV code threw exception: assembly: IncrementCompetitorNumber.dll; class: IncrementCompetitorNumber.Increment; entity: new_localcompetitor, event: postcreate, exception: System.InvalidCastException: Specified cast is not valid. at Microsoft.Crm.Callout.CalloutHost.PostCreate(CalloutUserContext userContext, CalloutEntityContext entityContext). As for use: I'm building it into a .dll and putting it in the \bin\assembly
extarbags
...in the CRM directory alongside Microsoft.Crm.Platform.Callout.Base.dll. Here's the relevant part of my callout.config.xml file: <callout entity="new_localcompetitor" event="PostCreate"> <subscription assembly="IncrementCompetitorNumber.dll" class="IncrementCompetitorNumber.Increment"> <postvalue>new_competitornumber</postvalue> </subscription>
extarbags
Can you post your code that calls the IncrementNumber method. The most likely cause is whatever you are are doing with the return value.
Ben Robinson
All the code I have is what's posted... I was under the impression that it just ran whatever was in the class specified in callout.config.xml whenever the callout runs, is that not the case?
extarbags
Ah, ok, I did not know that I had to do it that way. I've moved on to a different error now, which is a good sign I suppose. I'll work at it from here. Thanks!
extarbags