0

I'm using .Net Core 2.0 and VS 2017 v15.3.1. I'm trying to test making a Microsoft WCF Web Service Reference test by logging into a Salesforce service using SOAP. I successfully created the service reference using a WSDL. I've done this using .Net and have successfully logged in using a Service Reference (SoapClient) and a Web Service (SforceService). The interface created in .Net Core 2.0 is all asynchronous versus synchronous in .Net. I know little about async. I created a simple command line program to test the interface but I don't know async. I've done numerous versions of it. This is the latest attempt. I noted the error using a comment VS is giving me (red squiggly).

Any help is appreciated.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    using SFDC;

    namespace SfSoapCore
    {
        class Program
        {
            static string userName = "";
            static string password = "";
            static string securityToken = "";
            static SoapClient sc;
            static loginResponse lresp;
            static LoginResult lres;
            static LoginScopeHeader lsr = null;

            static void Main(string[] args)
            {
                sc = new SoapClient();
                Run();
            }

            static async void Run()
            {
                Task<loginResponse> loginTask = new Task<loginResponse>(SfLogin);
                loginTask.Start();
                lresp = await loginTask;
                lres = lresp.result;
                string serverUrl = lres.serverUrl;
                string sessionId = lres.sessionId;
                Console.WriteLine("Break");
            }

            static async loginResponse SfLogin()  //ERROR 'return type of an async method must be void, Task or Task<T>'
            {
                loginResponse lr = await sc.loginAsync(null, userName, password + securityToken);
                return lr;
            }

        }
    }
1
  • 1
    Your question seems to be more about c# and less about SFDC..... It's likely that you need help from a different forum. Aug 25, 2017 at 15:39

2 Answers 2

0
static async loginResponse SfLogin()  //ERROR 'return type of an async method must be void, Task or Task<T>'

Since you're calling the code asynchronously, you need to return a Task. So, you'd want to change your code as follows:

    static async Task<loginResponse> SfLogin()
    {
        Task<loginResponse> lr = sc.loginAsync(null, userName, password + securityToken);
        return await lr;
    }

This is kind of silly, though, since you could just have called loginAsync from the other method and avoid the overhead of an additional promise chain.

0
0

Here is the corrected code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    using SFDC;

    namespace SfSoapCore
    {
        class Program
        {
            static string userName = "";
            static string password = "";
            static string securityToken = "";
            static SoapClient sc;
            static loginResponse lresp;
            static LoginResult lres;
            static LoginScopeHeader lsr = null;
            static string serverUrl;
            static string sessionId;

            static void Main(string[] args)
            {
                sc = new SoapClient();
                Run().Wait();
            }

            static async System.Threading.Tasks.Task Run()
            {
                lresp = await SfLogin();
                lres = lresp.result;
                serverUrl = lres.serverUrl;
                sessionId = lres.sessionId;
                Console.WriteLine("Break");

            }

            static async Task<loginResponse> SfLogin()
            {
                loginResponse lr = await sc.loginAsync(null, userName, password + securityToken);
                return lr;
            }

        }
    }

Not the answer you're looking for? Browse other questions tagged .