无需使用Web.Config

时间:2019-03-27 11:19:42

标签: c# asp.net asp.net-mvc

我为南非的SagePay开发了一个付款网关插件,该插件将与nopCommerce(v3.90)电子商务平台一起使用。插件本身可以完美运行,但是,SagePay要求插件在设置和配置插件时验证商户ID和服务密钥。这是通过南非SagePay提供的Web服务完成的。

我正在使用Visual Studio2017。该插件是一个扩展nopCommerce功能的Web应用程序,它基于C#中编写的ASP.NET MVC 5。

在我的开发PC上将SagePay Web服务添加为“连接服务”没问题。当我测试插件时,验证过程可以完美地进行。但是,在测试服务器上部署插件时,使插件正常工作的唯一方法是,将端点配置设置(Web.Config)从插件文件夹复制到nopCommerce安装的根Web.Config文件中。

这是一个问题,因为我们不希望下载插件的客户必须手动编辑/修改根Web.Config文件。

所以我的问题是,如何通过在Controller代码中指定Endpoint设置来连接到Web服务,并以此绕过插件从Web.Config根文件获取Endpoint Configuration设置的需要? / p>

在测试服务器上安装插件后,插件文件夹中将存在一个web.config文件,其中包含该Web服务的Endpoint配置设置。正如我发现的那样,这不起作用,因为该插件正在这些设置的根Web.Config文件中查找。

如果我在根Web.Config中手动添加这些设置,则该插件可以正常工作,但是正如我所说,出于明显的原因,我们不希望用户手动编辑主Web.Config文件。

这是插件子文件夹中的Web.Config文件-我需要直接在Controller中添加它:

  <system.serviceModel>
      <bindings>
          <wsHttpBinding>
              <binding name="WSHttpBinding_INIWS_Partner">
                  <security mode="Transport">
                      <transport clientCredentialType="None" />
                  </security>
              </binding>
          </wsHttpBinding>
      </bindings>
      <client>
          <endpoint 
              address="https://ws.sagepay.co.za/NIWS/NIWS_Partner.svc"
              binding="wsHttpBinding" 
              bindingConfiguration="WSHttpBinding_INIWS_Partner"
              contract="NIWS.INIWS_Partner" 
              name="WSHttpBinding_INIWS_Partner" />
      </client>
  </system.serviceModel>  

================================================ ===

在id下面是需要上面的Web服务的两种方法:

using Nop.Plugin.Payments.SagePay.NIWS;

protected ServiceInfoList GetServiceInfo(string pServiceID, string pServiceKey)
{

        ServiceInfo serviceInfo = new ServiceInfo
    {
        ServiceId = pServiceID,
        ServiceKey = pServiceKey
    };

    ServiceInfoList serviceInfoList = new ServiceInfoList
    {
        serviceInfo
    };

    return serviceInfoList;
}

protected void ValidateServiceKey(string AccountNumber, string PayNowKey)
{

    //initialise all operations needed
    //---------------------------------------
    ServiceInfoList serviceInfoList = new ServiceInfoList();
    NIWS_PartnerClient client = new NIWS_PartnerClient();
    ValidateServiceKeyRequest validateServiceKeyRequest = new ValidateServiceKeyRequest();
    //---------------------------------------

    string SoftwareVendorKey = "******-****-****-****-************";

    //Populating request to validate
    //---------------------------------------

    //Add account number to MerchantAccount
    validateServiceKeyRequest.MerchantAccount = AccountNumber;

    //Add Vendor key issued by Sage Pay
    validateServiceKeyRequest.SoftwareVendorKey = SoftwareVendorKey;

    //checks if field was populated
    if (PayNowKey != null)
    {
        serviceInfoList = GetServiceInfo("14", PayNowKey);
    }

    //Add service info list to request
    validateServiceKeyRequest.ServiceInfoList = serviceInfoList;

    //---------------------------------------
    //Calling the ValidateServiceKey method validating account number with the service keys added
    var Request = client.ValidateServiceKey(validateServiceKeyRequest);

    //Do a check on the response for Account Status
    //001 = valid
    if (Request.AccountStatus == "001")
    {
        //do something, eg. output if account active
        //then add service info to list to check on each service status
        sageAccountStatus = Request.AccountStatus;

        ServiceInfoResponseList serviceInfoResponseList = new ServiceInfoResponseList();
        serviceInfoResponseList = Request.ServiceInfo;
        foreach (var s in serviceInfoResponseList)
        {
            string service = s.ServiceId;
            switch (service)
            {
                case "14": // Pay Now Service Status
                    sagePayNowStatus = s.ServiceStatus;
                    break;
                default:
                    break;
            }
        }
    }
    else
    {

        sageAccountStatus = Request.AccountStatus;
    }

    client.Close();
}

我想让插件直接从Controller中定义Web.Config文件中的设置,因此,不需要Web.Config文件。

0 个答案:

没有答案
相关问题