Skip to Content
0
Former Member
Sep 07, 2011 at 05:30 PM

SAP .NET Connector 3.0 and web services

134 Views

Hi everyone!

I faced with the next problem: I can create sales orders in SAP R/3 from my .Net application successfully (MS Visual Studio 2010 and SAP .Net Connector 3.0). But when I use that code in web service and try to test it, I get error message like "The type initializer for 'SAP.Middleware.Connector.RfcDestinationManager' threw an exception.". But strangeness of that situation in the following: it works fine in debug time (I mean in Visual Studio) and get error when I publish web service in IIS 7. I can notice that other methods work fine.

Moreover, I developed 2 types of web service: SOAP and WCF version. In both cases I get an error "SAP.Middleware.Connector.RfcDestinationManager' threw an exception". In details of SOAP version I've found more info: "Event handlers can only be bound to HttpApplication events during IHttpModule initialization.". What it is, I don't know.

Here is the code:

        public string SAPCreateOrder(string AccountSAPCode, string OfferingSAPCode, int Quantity, float Price)
        {
            IDestinationConfiguration dest = new SAPConnectionManager();
            RfcDestinationManager.RegisterDestinationConfiguration(dest);
            RfcDestination SapRfcDestination = RfcDestinationManager.GetDestination("WDA");
            RfcRepository SapRfcRepository = SapRfcDestination.Repository;

            IRfcFunction BapiSalesOrder = SapRfcRepository.CreateFunction("BAPI_SALESORDER_CREATEFROMDAT2");
            IRfcFunction CommitFunction = SapRfcRepository.CreateFunction("BAPI_TRANSACTION_COMMIT");

            IRfcTable ORDER_PARTNERS = BapiSalesOrder.GetTable("ORDER_PARTNERS");
            ORDER_PARTNERS.Append();
            ORDER_PARTNERS.SetValue("PARTN_ROLE", "WE");
            //ORDER_PARTNERS.SetValue("PARTN_NUMB", "0060125914");
            ORDER_PARTNERS.SetValue("PARTN_NUMB", AccountSAPCode);
            ORDER_PARTNERS.Append();
            ORDER_PARTNERS.SetValue("PARTN_ROLE", "AG");
            //ORDER_PARTNERS.SetValue("PARTN_NUMB", "0060125914");
            ORDER_PARTNERS.SetValue("PARTN_NUMB", AccountSAPCode);

            IRfcStructure order_header_inx = BapiSalesOrder.GetStructure("ORDER_HEADER_INX");
            order_header_inx.SetValue("UPDATEFLAG", "I");
            order_header_inx.SetValue("DOC_TYPE", "X");
            order_header_inx.SetValue("SALES_ORG", "X");
            order_header_inx.SetValue("DISTR_CHAN", "X");
            order_header_inx.SetValue("DIVISION", "X");

            IRfcStructure order_header_in = BapiSalesOrder.GetStructure("ORDER_HEADER_IN");
            order_header_in.SetValue("DOC_TYPE", "TA");
            order_header_in.SetValue("SALES_ORG", "12");
            order_header_in.SetValue("DISTR_CHAN", "00");
            order_header_in.SetValue("DIVISION", "00");

            IRfcTable ORDER_ITEMS_INX = BapiSalesOrder.GetTable("ORDER_ITEMS_INX");
            IRfcTable ORDER_ITEMS_IN = BapiSalesOrder.GetTable("ORDER_ITEMS_IN");

            ORDER_ITEMS_INX.Append();
            ORDER_ITEMS_IN.Append();
            ORDER_ITEMS_INX.SetValue("UPDATEFLAG", "I");
            ORDER_ITEMS_INX.SetValue("ITM_NUMBER", "000001");
            ORDER_ITEMS_IN.SetValue("ITM_NUMBER", "000001");
            ORDER_ITEMS_INX.SetValue("MATERIAL", "X");
            //ORDER_ITEMS_IN.SetValue("MATERIAL", "000000000041000423");
            ORDER_ITEMS_IN.SetValue("MATERIAL", OfferingSAPCode);
            ORDER_ITEMS_INX.SetValue("TARGET_QTY", "X");
            //ORDER_ITEMS_IN.SetValue("TARGET_QTY", "2");
            ORDER_ITEMS_IN.SetValue("TARGET_QTY", Quantity.ToString());
            ORDER_ITEMS_INX.SetValue("PLANT", "X");
            ORDER_ITEMS_IN.SetValue("PLANT", "1201");

            IRfcTable ORDER_SCHEDULES_INX = BapiSalesOrder.GetTable("ORDER_SCHEDULES_INX");
            IRfcTable ORDER_SCHEDULES_IN = BapiSalesOrder.GetTable("ORDER_SCHEDULES_IN");
            ORDER_SCHEDULES_INX.Append();
            ORDER_SCHEDULES_IN.Append();
            ORDER_SCHEDULES_INX.SetValue("UPDATEFLAG", "I");
            ORDER_SCHEDULES_INX.SetValue("ITM_NUMBER", "000001");
            ORDER_SCHEDULES_IN.SetValue("ITM_NUMBER", "000001");
            ORDER_SCHEDULES_INX.SetValue("REQ_QTY", "X");
            //ORDER_SCHEDULES_IN.SetValue("REQ_QTY", "2");
            ORDER_SCHEDULES_IN.SetValue("REQ_QTY", Quantity.ToString());

            IRfcTable ORDER_CONDITIONS_INX = BapiSalesOrder.GetTable("ORDER_CONDITIONS_INX");
            IRfcTable ORDER_CONDITIONS_IN = BapiSalesOrder.GetTable("ORDER_CONDITIONS_IN");
            ORDER_CONDITIONS_INX.Append();
            ORDER_CONDITIONS_IN.Append();
            ORDER_CONDITIONS_INX.SetValue("UPDATEFLAG", "I");
            ORDER_CONDITIONS_INX.SetValue("ITM_NUMBER", "000001");
            ORDER_CONDITIONS_IN.SetValue("ITM_NUMBER", "000001");
            ORDER_CONDITIONS_INX.SetValue("COND_COUNT", "01");
            ORDER_CONDITIONS_IN.SetValue("COND_COUNT", "01");
            ORDER_CONDITIONS_INX.SetValue("COND_TYPE", "Z000");
            ORDER_CONDITIONS_IN.SetValue("COND_TYPE", "Z000");
            ORDER_CONDITIONS_INX.SetValue("COND_VALUE", "X");
            ORDER_CONDITIONS_IN.SetValue("COND_VALUE", Price.ToString());
            ORDER_CONDITIONS_INX.SetValue("CURRENCY", "X");
            ORDER_CONDITIONS_IN.SetValue("CURRENCY", "EUR");
            ORDER_CONDITIONS_INX.SetValue("COND_P_UNT", "X");
            ORDER_CONDITIONS_IN.SetValue("COND_P_UNT", "1");

            BapiSalesOrder.Invoke(SapRfcDestination);
            CommitFunction.Invoke(SapRfcDestination);

            string ResultMessage = "";
            IRfcTable res = BapiSalesOrder.GetTable("RETURN");
            for (int i = 0; i < res.Count; i++)
            {
                ResultMessage = ResultMessage + res<i>.GetString("MESSAGE");
            }
            RfcDestinationManager.UnregisterDestinationConfiguration(dest);
            return ResultMessage;
        }