24

I am receiving this error when using the Dependency Service on a Xamarin.Forms PCL. I have seen answers to this error that involve iOS and the Linker. However, I am running this on Android and the Linker is off. Debug mode as well.

The constructor it is telling me it cannot find the default constructor for the Interface in the PCL.

I thought it may have been an issue with some renaming I did (I used refactor tool and made sure all necessary changes were being made) so I deleted those folders/files and remade them. Still nothing :(

I have been searching and debugging this for hours. What are some things that could be causing this error? I am pretty sure my DependencyService Implementations are correct so I feel like it is something different.

Here is my related code.

Interface:

namespace Enchantum.Functions
{
public interface PaymentProcessor
{

    void setUpCard(String cardNumber,
        int expirationMonth,
        int expirationYear,
        String CVC);

    Task cardTokenizer();

    void backendCardCharge();

}

Android:

[assembly: Xamarin.Forms.Dependency(typeof(PaymentProcessor_Android))]

namespace Enchantum.Droid.Functions_Android
{
class PaymentProcessor_Android : PaymentProcessor
{
public PaymentProcessor_Android() { }

    private Card mCard;
    private Token mToken;

    public void setUpCard(String cardNumber, int expirationMonth,
        int expirationYear, String cvcCode)
    {
        Card card = new Card
        {
            Number = cardNumber,
            ExpiryMonth = expirationMonth,
            ExpiryYear = expirationYear,
            CVC = cvcCode
        };

        mCard = card;
    }

    public async Task cardTokenizer()
    {
        mToken = await StripeClient.CreateToken(mCard);
    }

    /*TODO: 
     * */

    public void backendCardCharge()
    {
        throw new NotImplementedException();
    }

iOS:

[assembly: Xamarin.Forms.Dependency(typeof(PaymentProcessor_iOS))]

namespace Enchantum.iOS.Functions_iOS
{
class PaymentProcessor_iOS : PaymentProcessor
{
public PaymentProcessor_iOS() { }

    private Card mCard;
    private Token mToken;

    public void setUpCard(String cardNumber, int expirationMonth,
        int expirationYear, String cvcCode)
    {
        Card card = new Card
        {
            Number = cardNumber,
            ExpiryMonth = expirationMonth,
            ExpiryYear = expirationYear,
            CVC = cvcCode
        };

        mCard = card;

    }

    public async Task cardTokenizer()
    {
        mToken = await StripeClient.CreateToken(mCard);
    }

    /*TODO:*/
    public void backendCardCharge()
    {
        throw new NotImplementedException();
    }




}

Implementation in a Xamarin.Form Content Page:

DependencyService.Get<PaymentProcessor>().setUpCard(
                    cardNumber,
                    expirationMonth,
                    expirationYear,
                    CVC);

My error, again : "System.MissingMethodException: Default constructor not found for type Enchantum.Functions.PaymentProcessor"

What is going wrong?

P.S. Pardon my incorrect naming convention for the Interface as well some other mess.

coatless
  • 20,011
  • 13
  • 69
  • 84
eeastwood
  • 243
  • 1
  • 2
  • 6

4 Answers4

16

In my case, the problem was in the assembly exportation line.
The app was crashing as I used interface type instead of the class implementation:
[assembly: Xamarin.Forms.Dependency(typeof(IServiceType))]

But the correct way is to use Platform-Specific implementation of the interface:
[assembly: Xamarin.Forms.Dependency(typeof(ServiceImplementation_Android))]

Dr TJ
  • 3,241
  • 2
  • 35
  • 51
14

Maybe you can try making your interface implementation classes public, your constructors are visible, but the class itself might not be.

So Like:

[assembly: Xamarin.Forms.Dependency(typeof(PaymentProcessor_Android))]

namespace Enchantum.Droid.Functions_Android
{
public class PaymentProcessor_Android : PaymentProcessor //make the class public
{

 //your code here

 }
}
Ravi L
  • 1,142
  • 9
  • 17
  • 30
    For anyone else who comes across this and wonders why your code seems not quite right to this. The Dependency definition should include the current class, `PaymentProcessor_Android` not the interface, `PaymentProcessor`. – Brad Moore Oct 23 '16 at 11:40
  • 2
    As in the example above, my mistake was to write `[assembly: Xamarin.Forms.Dependency(typeof(PaymentProcessor))]` and I receive the same error. – Enrico Mar 20 '17 at 23:15
  • In my case, the constructor was protected, changing it to public did the trick! – chaosifier Jul 14 '18 at 17:00
  • I was scratching my head for a while but it is essential to have that `[assembly: Dependency(typeof([Your_Type))]` line. I don't know if all resolvers require this line so this might have slipped by me, but the Xamarin Forms one does. – Chucky Sep 07 '18 at 11:24
  • 1
    @BradMoore - Correct answer for me... Had the interface instead of the class. Once changed, it worked as expected. Thank you. – Thierry Dec 30 '18 at 14:07
  • So I had a combination of having the Interface name in the `assembly: Xamarin.Forms.Dependency` argument which you should have class name. Additionally having a constructor defined in the class `public PaymentProccessor_Android() {}` also cause an issue because the interface doesn't have that constructor defined in it – shreddish Aug 14 '20 at 19:42
5

I have the same issue for Linker , When I set Linker to None its working

Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
0

I was getting the same error when I defined a default constructor that initialized a readonly private property.

Seemed crazy, but it makes sense given Java doesnt have a concept of readonly, and the property probably transpiled to a constant.

MSC
  • 2,011
  • 1
  • 16
  • 22