Thursday, June 9, 2011

Avoiding the "Destination configuration already initialized" error in SAP .NET Connector 3

SAP's documentation suggests using a line similar to the following to create a connection to SAP:

RfcDestinationManager.RegisterDestinationConfiguration(new Config());

where Config() is a class that contains connection settings.

This works fine, except, you occasionally get the error, "Destination configuration already initialized", because the connection hasn't timed out. SAP's documentation that I've seen (the .chm help file and the "Overview" document) doesn't address this issue at all, and their sample code doesn't contain any examples of how to close a connection.

I've found that the following code works:

Config c = new Config();
RfcDestinationManager.RegisterDestinationConfiguration(c);
...
(processing code)
...
RfcDestinationManager.UnregisterDestinationConfiguration(c);

However, if you've just tried to connect without using "unregister", you'll have to wait until the old connection times out itself before trying the code above (the sample config code SAP provides keeps the connection open for 10 minutes).

Overall, I've found the new SAP Connector 3 to be a HUGE step up from v2, though it is quite a pain having to re-write all of my v2 applications. But I easily prefer a few months of re-writing over being tied to VS2003. Thanks, SAP, for updating this - I thought you had forgotten about us .NET developers.


11 comments:

Anonymous said...

Do you perhaps have a working sample on SAP .Net Connector 3? If so can you please post it.

Dave Gruska said...

Here's the config.cs class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SAP.Middleware.Connector;

namespace DocScans_SAP
{
public class Config : IDestinationConfiguration
{
public RfcConfigParameters GetParameters(String destinationName)
{
if ("prod".Equals(destinationName))
{
RfcConfigParameters parms = new RfcConfigParameters();
parms.Add(RfcConfigParameters.AppServerHost, "your.site.com");
parms.Add(RfcConfigParameters.SystemID, "SP1");
parms.Add(RfcConfigParameters.SystemNumber, "02");
parms.Add(RfcConfigParameters.User, "UserName");
parms.Add(RfcConfigParameters.Password, "Password");
parms.Add(RfcConfigParameters.Client, "400");
parms.Add(RfcConfigParameters.Language, "EN");
parms.Add(RfcConfigParameters.PoolSize, "5");
parms.Add(RfcConfigParameters.MaxPoolSize, "10");
parms.Add(RfcConfigParameters.IdleTimeout, "600");

return parms;
}
else return null;
}

public bool ChangeEventsSupported(){
return false;
}

public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
}
}

Dave Gruska said...

and here's the codebehind of a page that uses it:

RfcDestinationManager.RegisterDestinationConfiguration(new Config());
RfcDestination prd = RfcDestinationManager.GetDestination("prod");

...

RfcRepository repo = prd.Repository;
IRfcFunction zBapi = repo.CreateFunction("ZBAPI_GET_DOC_DETAILS");
zBapi.SetValue("BELNR", accountingDoc.AccountingDocNumber);
zBapi.SetValue("BUKRS", accountingDoc.CompanyCode);
zBapi.Invoke(prd);

String docDate = zBapi.GetString("BLDAT");

---

That should get you started. Let me know if you have any questions about it. I can spend some time later and put together a full working sample if you really need it. Let me know.

Anonymous said...

Thank you so much....I really need it and will really appreciate it if you can provide a working example.

Again thanx a lot.

Anonymous said...

Please provide me with a full working example.

Dave Gruska said...

I'm in the middle of a huge project at work right now - it's going to be a few days at the very least.

Why don't you try using the code I provided, and just shoot me a question if you get stuck.

Anonymous said...

Thanx for quick respond. I tried, but do not seem to be succeeding; i should mention that i am a newbie.

The code that u said should be used on the page, i have put it on the Button click; however I dont get any errors or respond on Button click.

I can wait for such days if you are busy. Again thnx a lot.

Dave Gruska said...

Hmm - if you can send me your code, that would probably be the easiest way for me to help you. gruskada @ gmx DOT com

Guille said...

This is an old post... but...

To avoid the "Destination configuration already initialized" error I write the next code:

RfcDestination prd = RfcDestinationManager.TryGetDestination(ConfigurationManager.AppSettings["DestinationName"]);

if (prd == null)
{
RfcDestinationManager.RegisterDestinationConfiguration(new DestinationConfiguration());
prd = RfcDestinationManager.GetDestination(ConfigurationManager.AppSettings["DestinationName"]);
}

Do you think it's ok?

Dave Gruska said...

@Guille - seems reasonable, though I don't see any documentation for TryGetDestination. TryGet... methods usually pass in a value by reference (out), but maybe this one works differently.

Charly said...

Hi,

I have been developing a WCF Service that uses the SAP NCO 3.0.

When the WCF Service is used by one user everything is ok, but, when many users use the service they get the exception "destination configuration already initialized".

Have you tried to execute your class in parallel mode?