【问题标题】:Delphi CrossTalk and C# Public Static classesDelphi CrossTalk 和 C# 公共静态类
【发布时间】:2020-05-18 07:17:16
【问题描述】:

我正在尝试将 Delphi 和 CrossTalk 与一组 C# 类库结合使用。

我收到一条错误消息:

System.NullReferenceException:对象引用未设置为对象的实例。

我似乎无法解决这个问题..

注意“动态”类定义和实例化的公共静态类型。

class RESTAdapterConfig 类中的代码用于从 .config 文件中读取一部分内容。

我可以通过“全 C#”应用程序方法很好地使用这些类。
我遇到问题的课程是:

using System;
using System.Configuration;
using System.Linq;

namespace HHHHHH.Adapters
{
    public class RESTAdapterConfig : ConfigurationSection
    {
        public static RESTAdapterConfig Instance { get; private set; }
        private static object instanceLock = new object();

        static RESTAdapterConfig()
        {
            if (Instance == null)
                lock (instanceLock)
                    if (Instance == null)
                        Instance = (RESTAdapterConfig)ConfigurationManager.GetSection("RESTAdapterConfig");
        }

        [ConfigurationCollection(typeof(HttpHeaders), AddItemName = "httpHeader")]
        public class HttpHeaders : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new HttpHeader();
            }

            protected override object GetElementKey(ConfigurationElement element)
            {
                if (element == null)
                    throw new ArgumentNullException("httpHeader");

                return ((HttpHeader)element).Key;
            }

            public new HttpHeader this[string key]
            {
                get { return (HttpHeader)base.BaseGet(key); }
            }
        }

还有一个 RestAdapter.cs 类包含

private static dynamic Submit(RESTRequest request, AuthType authType, object requestBody)
{
    HttpWebRequest webRequest = GetWebRequest(request, authType);
{etc}

private static HttpWebRequest GetWebRequest(RESTRequest request, AuthType authType)
{
    var uri = new Uri(GetServiceUrl(request));
{etc}

private static string GetServiceUrl(RESTRequest request)
{
    var baseURL = RESTAdapterConfig.Instance.baseURL.URL;

Delphi 代码如下:

var
    ARESTAdapter: RESTAdapter;
    GenerateHRF605StatusRequest: RESTRequest;
    //ARESTAdapterConfig : RESTAdapterConfig;
    HRF605Req: HRF605Request;
    PartialURL: string;
begin

    // Call Web service

    try
        ARESTAdapter := RESTAdapter.Create;
        try
            PartialURL := 'generateHRF605';
            GenerateHRF605StatusRequest := RESTRequest.Create;
            GenerateHRF605StatusRequest.PartyCode := DataModule.AJF2GetPartyId;
            GenerateHRF605StatusRequest.Method := 'POST';
            GenerateHRF605StatusRequest.ApiVersion := 1;
            GenerateHRF605StatusRequest.Path := PartialURL;
            HRF605Req := HRF605Request.Create(
                            FormatDateTime('yyyy-MM-dd', rsRangeSelector.DateFrom) ,
                            FormatDateTime('yyyy-MM-dd', rsRangeSelector.DateTo) ,
                                                        0,
                            '\\FILESERVER01\H2FileRepository\Dev\CommonDev\reports\');
            //HRF605Req := HRF605Request.Create;
            //HRF605Req.FromDate := FormatDateTime('yyyy-MM-dd', rsRangeSelector.DateFrom) ;
            //HRF605Req.ToDate := FormatDateTime('yyyy-MM-dd', rsRangeSelector.DateTo);
            //HRF605Req.MembershipId := 100;
            //HRF605Req.MembershipId := 100;
            //HRF605Req.FolderPath := null;
            //ShowMessage('Debug:  FromDate: ' +  HRF605Req.FromDate + ' ToDate: ' +  HRF605Req.ToDate + ' PartyID : ' + GenerateHRF605StatusRequest.PartyCode) ;

            try
                ARESTAdapter.HRF605RequestHelloWorld();
                //have tried both
                //ARESTAdapter.SubmitHRF605Request(GenerateHRF605StatusRequest,HRF605Req);
                ARESTAdapter.SubmitHRF605Request(GenerateHRF605StatusRequest,HRF605Req);

            finally
                HRF605Req.Free;
                GenerateHRF605StatusRequest.Free;
            end;
        finally
            ARESTAdapter.Free;
        end;
    except on E: Exception do
        raise EhslException.Create(StringReplace(E.Message, 'RESTException: ', '', [rfIgnoreCase]));
    end;

【问题讨论】:

    标签: c# delphi


    【解决方案1】:

    一旦我在正确的 .config 文件中有正确的部分,我就设法让它工作。

    正确的配置文件是与您的 .exe 文件匹配的配置文件。

    例如如果运行 ABC.exe,则在 ABC.exe.config 中查找配置。

    我还必须在配置文件顶部的区域中声明这些部分 .. 除了在最后一个标记上方的配置中包含配置部分。

    使用 Visual Studio“即时窗口”也有助于调试。 例如查看表达式的值; System.Configuration.ConfigurationManager.GetSection("RESTAdapterConfig")

    另一个调试技巧,将 Delphi 应用程序作为独立 exe 运行,但一旦运行,使用 Visual Studio 中的 Attach to Process 连接到您的 Delphi exe,然后在您希望在适配器中命中的代码上放置断点/dll https://docs.microsoft.com/en-us/visualstudio/debugger/attach-to-running-processes-with-the-visual-studio-debugger?view=vs-2019

    另一位开发人员建议更改为使用我按照此处所做的“静态实例(单例)”的代码模式..(但根据问题的上一个代码也可以在正确的文件中给出正确的配置。)

    public class RESTAdapterConfig : ConfigurationSection
    {
        private static readonly RESTAdapterConfig instance = (RESTAdapterConfig)ConfigurationManager.GetSection("RESTAdapterConfig");
        // Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
        // The laziness of type initializers is only guaranteed by .NET when the type isn't marked with a special flag called beforefieldinit.
        // The C# compiler marks all types which don't have a static constructor (i.e. a block which looks like a constructor but is marked static) as beforefieldinit.
        static RESTAdapterConfig()
        {
        }
    
        public static RESTAdapterConfig Instance { get { return instance; } }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 2012-02-11
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多