Hi Thomas!
I'm not sure if this is what you are looking for?
The key is on the Service* (Take a look the If case)
Steps:
1.- Created the DataContexts (2) (Assuming the two tables on the two databases are identical structure)
Refer: Add a Database Context
2.- Create the Model
/////////////// CustomersModel.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SnapObjects.Data;
using DWNet.Data;
namespace restapi.Models
{
[DataWindow("d_customers", DwStyle.Grid)]
[Table("customer")]
#region DwSelectAttribute
[DwSelect("PBSELECT( VERSION(400) TABLE(NAME=\"customer\" ) @(_COLUMNS_PLACEHOLDER_) )")]
#endregion
[DwKeyModificationStrategy(UpdateSqlStrategy.DeleteThenInsert)]
[UpdateWhereStrategy(UpdateWhereStrategy.KeyAndConcurrencyCheckColumns)]
public class CustomersModel
{
[Key]
[DwColumn("customer", "id")]
public int Id { get; set; }
[ConcurrencyCheck]
[StringLength(15)]
[DwColumn("customer", "fname")]
public string Fname { get; set; }
[ConcurrencyCheck]
[StringLength(20)]
[DwColumn("customer", "lname")]
public string Lname { get; set; }
[ConcurrencyCheck]
[StringLength(20)]
[DwColumn("customer", "city")]
public string City { get; set; }
[ConcurrencyCheck]
[StringLength(35)]
[DwColumn("customer", "company_name")]
public string Company_Name { get; set; }
}
}
3.- Scaffolding (Service and Controller from Model SqlModelMapper)
4.- Change code on the Service and on the Interface
///////////Service*
public class CustomersService : ICustomersService
{
private readonly AppeonSampleDataContext _dataContext;
private readonly AppeonSampleADataContext _dataContextA;
public CustomersService(AppeonSampleDataContext dataContext, AppeonSampleADataContext adataContext)
{
_dataContext = dataContext;
_dataContextA = adataContext;
}
public IList<CustomersModel> Retrieve(int clientID)
{
if(clientID == 0)
return _dat aContextA.SqlModelMapper.Load<CustomersModel>().ToList();
else
return _dataContext.SqlModelMapper.Load<CustomersModel>().ToList();
}
/////////////////////Interfase
public interface ICustomersService
{
IList<CustomersModel> Retrieve(int clientID);
}
///////////////// Controller
// In the controller added {clientId} on the HttpGet Method
// URL: "http://localhost:5000/api/Customers/Retrieve/1"
//GET api/Customers/Retrieve/clientId
[HttpGet("{clientId}")]
[ProducesResponseType(typeof(IList<CustomersModel>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public ActionResult<IList<CustomersModel>> Retrieve(int clientId)
{
try {
var result = _icustomersservice.Retrieve(clientId);
Return Ok(result);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
6. My final startup.cs
///////////////////////////////// Startup.cs
// Startup.cs with the 2 DataContexts added
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(m =>
{
m.UseCoreIntegrated();
m.UsePowerBuilderIntegrated();
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDataContext<AppeonSampleADataContext>(m => m.UseSqlServer(this.Configuration, "AppeonSample"));
services.AddDataContext<AppeonSampleDataContext>(m => m.UseSqlServer(this.Configuration, "AppeonSample"));
services.AddScoped<ICustomersService, CustomersService>();
services.AddGzipCompression(CompressionLevel.Fastest);
7. Create a client on PowerBuilder
////////////////Client ///////////////////
// Power Builder as Rest Client /
RestClient lrc_Client
String ls_Url, ls_Method
Long ll_rtn
//test 1:
ls_Url = "http://localhost:5000/api/Customers/Retrieve/1"
//test 2:
// ls_Url = "http://localhost:5000/api/Customers/Retrieve/0"
dw_customers.clear( )
lrc_Client = Create RestClient
ls_Method = "GET"
lrc_Client.SetRequestHeader ("Content-Type", "application/json")
lrc_Client.SetRequestHeader("Accept-Encoding", "gzip")
ll_rtn = lrc_Client.Retrieve( dw_customers,ls_Url )
If ll_rtn >= 0 And lrc_Client.GetResponseStatusCode() = 200 Then
MessageBox( "Retrieve Success","Rows:" + String ( ll_rtn ))
Else
MessageBox(
"Retrieve Failed","Rows:" + String ( ll_rtn ))
End If
If IsValid (lrc_Client) Then Destroy ( lrc_Client )
////////////////////////////////////////////////////
Hope it works for you!
So I get the ID from the datapacker in the controller, pass that value to a service and connect to the db at the beginning of the service. How would you code the service to make a call to the CreateDataContext() method (i.e. the one in your DataContextFactory class)?
Thanks, Tom
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public ActionResult<IDataPacker> Foo(IDataUnpacker unpacker)
{
var packer = new DataPacker();
var tenantID = unpacker.GetValue<string>("tenant");
try
{
//pass tenant code to service and connect to db there
var rc = _service.FooMethod(tenantID);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
return packer;
}
You can also get data from request body. Please refer to the following code:
var body = _httpAccessor.HttpContext.Request.Body;
string data;
using (StreamReader reader = new StreamReader(body,Encoding.UTF8,true,1024,true) )
{
data = reader.ReadToEnd();
}
var packer = new DataPacker(data, DataFormat.Json); // Then you can get value from this object
Regards,
Logan