Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null reference error #3

Open
RoarkDude opened this issue Apr 4, 2017 · 12 comments
Open

Null reference error #3

RoarkDude opened this issue Apr 4, 2017 · 12 comments

Comments

@RoarkDude
Copy link

Hi,

Creating a service requires additional parameters not specified in the documentation. I guessed and used:

            int productThreadsLimit = 100;
            int sessionLifeTimeMs = 10000;
            bool isLogRawMessages = true;

Also, left Soap parameters as empty strings.

            var servicesFactory = new MagentoFactory();
            var magentoService = servicesFactory.CreateService(new MagentoAuthenticatedUserCredentials(accessToken, accessTokenSecret, storeURL, consumerSecret, consumerKey, "", "", productThreadsLimit,sessionLifeTimeMs,isLogRawMessages), new MagentoConfig() { EditionByDefault = "ce", VersionByDefault = "2.0.2.0" });

var margentoOrders = magentoService.GetOrdersAsync().Result;

Should return the 2 orders in the test webstore. The API call works using Postman with the same credentials. This throws and exception (same for GetProductsAsync()):

System.AggregateException occurred
HResult=0x80131500
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result()
at MagentoClient.Program.Main(String[] args) in C:\Users\Peter\magentoAccess-master\src\MagentoClient\Program.cs:line 29

Inner Exception 1:
MagentoCommonException: GetOrdersAsync:{MethodName:GetOrdersAsync, ConnectionInfo:{"StoreVersion":"2.0.2.0","ApiUser":"","ApiKey":"","Store":null,"BaseMagentoUrl":"http://localhost/Magento","TokenSecret":null,"LogRawMessages":true,"GetStockItemsWithoutSkuImplementedWithPages":true,"GetOrderByIdForFullInformation":true,"GetOrdersUsesEntityInsteadOfIncrementId":true}, MethodParameters:, Mark:"826af4bc-2bd5-4998-9404-4fb5e35e7af4", {}}

Inner Exception 2:
NullReferenceException: Object reference not set to an instance of an object.

@MaximKitsenko
Copy link
Contributor

MaximKitsenko commented Apr 4, 2017

Hi @RoarkDude,
What version of magento do you use?
Looks like you use incorrect credentials. I have updated documentation try to use parameters like in documentation.
Does it work now?

@RoarkDude
Copy link
Author

Hi,
I'm using 2.1.5. I'm still getting the same error. I'm trying to use rest like this:

            var servicesFactory = new MagentoFactory();
            var magentoService = servicesFactory.CreateService(new MagentoAuthenticatedUserCredentials(accessToken, accessTokenSecret, storeURL, consumerSecret, consumerKey, "", "", productThreadsLimit,sessionLifeTimeMs,isLogRawMessages), new MagentoConfig() { EditionByDefault = "ce", VersionByDefault = "2.1.5.0", Protocol=MagentoDefaultProtocol.RestOnly});

My credentials are ok. I know because they work when using Postman

@MaximKitsenko
Copy link
Contributor

MaximKitsenko commented Apr 4, 2017

@RoarkDude yes, your credentials can be ok, but you don't pass them to the function. I mean you use incorrect arguments.
Look to the documentation for correct example:
var magentoService = servicesFactory.CreateService(new MagentoAuthenticatedUserCredentials( "n/a", "n/a", "https://www.youstore.com", "n/a", "n/a", "User", "Password", 4, 1800000, false ), new MagentoConfig() { EditionByDefault = "ce", VersionByDefault = "2.1.0.0", Protocol = MagentoDefaultProtocol.SoapOnly } );

but insteadof "User", "Password" you use "" and "":
var magentoService = servicesFactory.CreateService(new MagentoAuthenticatedUserCredentials(accessToken, accessTokenSecret, storeURL, consumerSecret, consumerKey, "", "", productThreadsLimit,sessionLifeTimeMs,isLogRawMessages), new MagentoConfig() { EditionByDefault = "ce", VersionByDefault = "2.1.5.0", Protocol=MagentoDefaultProtocol.RestOnly});

@RoarkDude
Copy link
Author

Right, I don't want to use soap, I want to use rest. I tried putting "n/a" for soap user and password but still same error. Maybe you can document an an example using

Protocol=MagentoDefaultProtocol.RestOnly
You get the rest API credentials from the Magento Admin screen. System-Integrations. Add an Integration and you'll get the 4 keys: accessToken, accessTokenSecret, consumerSecret, consumerKey

Thanks.

@MaximKitsenko
Copy link
Contributor

MaximKitsenko commented Apr 4, 2017

@RoarkDude in general 4 keys from magento store : accessToken, accessTokenSecret, consumerSecret, consumerKey doesn't make sense, because you can always accesss your store API just with store simple user credentials. At this moment only this way supported, later i will added support for these 4 keys.

So, to start working, you should create user (system->all users), add correct permissons for that user.

Use your user credentials as i wrote above:
var magentoService = servicesFactory.CreateService(new MagentoAuthenticatedUserCredentials( "n/a", "n/a", "https://www.youstore.com", "n/a", "n/a", "User", "Password", 4, 1800000, false ), new MagentoConfig() { EditionByDefault = "ce", VersionByDefault = "2.1.0.0", Protocol = MagentoDefaultProtocol.SoapOnly } );

"User" = your store user
"Password" = your store user password

It doesn't metter do you use REST or SOAP, you should always use your user credentials like in example above.

@RoarkDude also don't forget to Init service after creation. Does it work for you now?

@RoarkDude
Copy link
Author

RoarkDude commented Apr 5, 2017

Init service? That's not in the documentation at all. I added it and I'm still getting same errors with

            string storeURL = "http://localhost/Magento";

            var servicesFactory = new MagentoFactory();

            var magentoService = servicesFactory.CreateService(new MagentoAuthenticatedUserCredentials("n/a", "n/a", storeURL, "n/a", "n/a", soapUser, soapPassword, productThreadsLimit, sessionLifeTimeMs, isLogRawMessages), new MagentoConfig() { EditionByDefault = "ce", VersionByDefault = "2.1.5.0", Protocol = MagentoDefaultProtocol.SoapOnly });

            bool isInitialized = magentoService.InitAsync().Result;

            if (isInitialized)
            {
                var margentoOrders = magentoService.GetOrdersAsync().Result;
            }

I tried both admin user and password and a user I created with and admin role and password.

@MaximKitsenko
Copy link
Contributor

MaximKitsenko commented Apr 5, 2017

@RoarkDude can ypu add complete description of these "some" errors that you receive?
Also, feel free to add pull request with line that contains InitAsync into the documentation

@RoarkDude
Copy link
Author

RoarkDude commented Apr 5, 2017

Here's the code:

            int productThreadsLimit = 4;
            int sessionLifeTimeMs = 180000;
            bool isLogRawMessages = false;
            string storeURL = "http://localhost/Magento";
            string soapUser = "admin";
            string soapPassword = "********";

            var servicesFactory = new MagentoFactory();
            var magentoService = servicesFactory.CreateService(new MagentoAuthenticatedUserCredentials("n/a", "n/a", storeURL, "n/a", "n/a", soapUser, soapPassword, productThreadsLimit, sessionLifeTimeMs, isLogRawMessages), new MagentoConfig() { EditionByDefault = "ce", VersionByDefault = "2.1.5.0", Protocol = MagentoDefaultProtocol.SoapOnly });

            bool isInitialized = magentoService.InitAsync().Result;

            if (isInitialized)
            {
                var margentoOrders = magentoService.GetOrdersAsync().Result;
            }

Here's the error. I think "Store":null is the is the null reference that's causing the exception:

`

  •   InnerException	{"GetOrdersAsync:{MethodName:GetOrdersAsync, ConnectionInfo:{\"StoreVersion\":\"2.1.5.0\",\"ApiUser\":\"admin\",\"ApiKey\":\"********\",\"Store\":null,\"BaseMagentoUrl\":\"http://localhost/Magento\",\"TokenSecret\":null,\"LogRawMessages\":false,\"GetStockItemsWithoutSkuImplementedWithPages\":true,\"GetOrderByIdForFullInformation\":true,\"GetOrdersUsesEntityInsteadOfIncrementId\":true}, MethodParameters:, Mark:\"a8c3a5e4-daf4-4fe8-a4dd-1733dadd4024\", {}}"}	System.Exception {MagentoAccess.MagentoCommonException}
    
      Message	"Object reference not set to an instance of an object."	string
    
      StackTrace	"   at MagentoAccess.MagentoService.<GetOrdersAsync>d__44.MoveNext() in C:\\work\\SkuVault\\repo\\Integration\\MagentoAccess\\src\\MagentoAccess\\MagentoService.cs:line 497"	string
    

`

Stack Trace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task1.get_Result() at MagentoClient.Program.Main(String[] args) in C:\Users\Peter\Documents\Visual Studio 2017\Learning\MagentoClient\MagentoClient\Program.cs:line 36

@MaximKitsenko
Copy link
Contributor

MaximKitsenko commented Apr 6, 2017

@RoarkDude
I suggest you to use another 2 methods:
public async Task< IEnumerable< Order > > GetOrdersAsync( DateTime dateFrom, DateTime dateTo, Mark mark = null )
and
public async Task< IEnumerable< Order > > GetOrdersAsync( IEnumerable< string > orderIds )
(try to use SOAP if REST will not work)

Unfortunately, i have no time to fix it right now. If you will create a fix for GetOrdersAsync() create pull request please.

@ashishsuvarna
Copy link

Hi I have tried DetermineMagentoVersionAndSetupServiceAsync , GetOrdersAsync with and without dates but nothing seems to work.. Would be nice if you can update this.. This could be quite a useful library.

@MaximKitsenko
Copy link
Contributor

MaximKitsenko commented May 23, 2017

@ashishsuvarna, DetermineMagentoVersionAndSetupServiceAsync , GetOrdersAsync work, i tested it on dozens of production stores via SOAP (and magentoAccess work with dozens of production stores at this moment). I think your problem related to incorrect parameters. Look to the Tests there are a lot of examples there.

@ashishsuvarna Also, can you provide your code which causes that exception?

@Malfonde
Copy link

Malfonde commented Sep 3, 2017

Hey,
I am using Magento version 1.9.3.2 , ( I know that officially it has not been tested with MagentoAccess.),
and I get the same error as the above.
I will mention that MagentoService.GetOrdersAsync(DateTime dateFrom ,DateTime dateTo) works just fine with the same request parameters that I have created the service with.

I hope it will help you to find the issue, and maybe solve it for this version too :).
I'll gladly answer any question,
thank you for your good job!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants