Skip to Content
0
Sep 03, 2020 at 12:07 PM

B1 SQL Servicelayer connection using UI API

319 Views

In Short: Company.GetServiceLayerConnectionContext(BaseUri); throws Exception "General Failure"

SAP B1 10.00.120 PL02 for SQL

Service Layer is installed and can be reached via Postman

My Intention: Reading the Businesspartner Object from Servicelayer in a Desktop (C#) AddOnContext

I was following the Instructions in this article: https://blogs.sap.com/2016/07/01/sap-business-one-service-layer-sso-with-ui-api/

BaseUri is set to "https://qsc-mobile:50000/b1s/v1/"

Stacktrace:

Exception thrown: 'System.Runtime.InteropServices.COMException' in VisECommerceAddOn.dll
General Failure
   bei SAPbouiCOM.CompanyClass.GetServiceLayerConnectionContext(String inConnParam)
   bei VisECommerceAddOn.Logic.ServiceLayer.PrepareRequest(String method, String path, Object content) in D:\dev\VIS2\SAPAddOns\4.0\VisECommerceAddOn\VisECommerceAddOn\Logic\ServiceLayer.cs:Zeile 60.
   bei VisECommerceAddOn.Logic.ServiceLayer.GetBusinessPartner(String cardCode) in D:\dev\VIS2\SAPAddOns\4.0\VisECommerceAddOn\VisECommerceAddOn\Logic\ServiceLayer.cs:Zeile 32.
   bei VisECommerceAddOn.Logic.BPUpload.UploadShopBps(List`1 contactsToUpload, ShopModel shop) in D:\dev\VIS2\SAPAddOns\4.0\VisECommerceAddOn\VisECommerceAddOn\Logic\BPUpload.cs:Zeile 59.
   bei VisECommerceAddOn.Frontend.BPManagement.BPManagementForm.UploadBtn_Click(Object sender, EventArgs e) in D:\dev\VIS2\SAPAddOns\4.0\VisECommerceAddOn\VisECommerceAddOn\Frontend\BPManagement\BPManagementForm.cs:Zeile 497.
public class ServiceLayer
    {
        private Company Company;
        private string BaseUri;
        public ServiceLayer(string baseUri)
        {
            Company = SwissAddonFramework.B1Connector.GetB1Connector().Application.Company;
            BaseUri = baseUri;
        }

        public BusinessPartner GetBusinessPartner(string cardCode)
        {
            var path = $"BusinessPartners('{cardCode}')";
            var req = PrepareRequest("GET", path);
            var resp = req.GetResponse() as HttpWebResponse;
            var result = ReadResponse<BusinessPartner>(resp);
            return result;
        }

        private T ReadResponse<T>(HttpWebResponse response) where T : class
        {
            T result = null;
            using (var stream = response.GetResponseStream())
            {
                using (var streamReader = new StreamReader(stream))
                {
                    var content = streamReader.ReadToEnd();
                    if (response.ContentType != JsonContentType)
                    {
                        throw new WrongContentTypeException();
                    }
                    result = JsonConvert.DeserializeObject<T>(content);
                }
            }
            response.Close();
            return result;
        }
        private const string JsonContentType = "application/json";
        private HttpWebRequest PrepareRequest(string method, string path, object content = null)
        {
            var cookies = Company.GetServiceLayerConnectionContext(BaseUri);
            path = path.StartsWith("/") ? path.Substring(1) : path;
            var requestUri = BaseUri + path;
            var request = WebRequest.Create(requestUri) as HttpWebRequest;
            request.Method = method;
            if (content != null)
            {
                request.ContentType = JsonContentType;
                using var stream = request.GetRequestStream();
                using var streamWriter = new StreamWriter(stream);
                var json = JsonConvert.SerializeObject(content);
                streamWriter.Write(json);
            }
            request.CookieContainer = new CookieContainer();
            string[] cookieItems = cookies.Split(';');
            foreach (var cookieItem in cookieItems)
            {
                string[] parts = cookieItem.Split('=');
                if (parts.Length == 2)
                {
                    request.CookieContainer.Add(request.RequestUri, new Cookie(parts[0].Trim(), parts[1].Trim()));
                }
            }
            return request;
        }
    }

Is there a Logfile I could consult for more information about this bug/ behaviour?

Best Regards

Quentin Schär