Masstransit not consuming non MassTransit messages - asp.net-core

I have to publish a message using Python and consume it using MassTransit (.Net core app).
To test, I created a Console Application to produce the messages using the Azure.ServiceBuss library.
The consumer is a MassTransit implementation of the ServiceBus.
When I send the message to the queue (using my console application), MassTransit is not able to consume it.
I am aware of the Interoperability and all the headers we must send in order to MassTransit consume the messages, but I am not able to make the consumer work.
The producer:
namespace MassTransit.MultipleBus.ServiceBus.Models.Commands
{
class Program
{
const string ServiceBusConnectionString = "connstring";
const string QueueName = "topic";
static IQueueClient queueClient;
public static async Task Main(string[] args)
{
queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
// Send messages.
await SendMessagesAsync();
Console.WriteLine("ENVIOU");
Console.ReadKey();
await queueClient.CloseAsync();
}
private static async Task SendMessagesAsync()
{
try
{
while (true)
{
string messageBody = JsonConvert.SerializeObject(new EnviarExemploPocAzureServiceBusCommand(
NewId.NextGuid(),
NewId.NextGuid(),
new Proposta()
)
{
Headers = new Dictionary<string, object>() { },
Host = new HostInfo()
});
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
Console.WriteLine($"Enviando mensagem: {messageBody}");
await queueClient.SendAsync(message);
Console.WriteLine("Enviado. Enviar outro?");
Console.ReadKey();
}
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
}
}
}
}
The class I'm sending:
namespace MassTransit.MultipleBus.ServiceBus.Models.Commands
{
public class EnviarExemploPocAzureServiceBusCommand
{
public EnviarExemploPocAzureServiceBusCommand(Guid msgId, Guid convId, Proposta msg)
{
MessageId = msgId.ToString();
ConversationId = convId.ToString();
MessageType = new[]
{
"urn:message:MassTransit.MultipleBus.ServiceBus.Models.Commands:Proposta"
};
Message = msg;
SourceAddress = "";
DestinationAddress = "sb://organization.servicebus.windows.net/topic";
}
public string DestinationAddress { get; set; }
public string MessageId { get; set; }
public string ConversationId { get; set; }
public string SourceAddress { get; set; }
public HostInfo Host { get; set; }
public IDictionary<string, object> Headers { get; set; }
public Proposta Message { get; set; }
public string[] MessageType { get; set; }
}
public class Proposta
{
public Proposta()
{
Nome = "TESTE 2";
AcaoId = 2;
Mensagem = "TESTE 2";
}
public string Nome { get; set; }
public int AcaoId { get; set; }
public string Mensagem { get; set; }
}
public class HostInfo
{
public HostInfo()
{
MachineName = "NOTEXXXXX";
ProcessName = "iisexpress";
ProcessId = 34188;
Assembly = "MassTransit.MultipleBus";
AssemblyVersion = "1.0.0.0";
FrameworkVersion = "3.1.5";
MassTransitVersion = "7.0.2.0";
OperatingSystemVersion = "Microsoft Windows NT 10.0.19041.0";
}
public string MachineName { get; set; }
public string ProcessName { get; set; }
public int ProcessId { get; set; }
public string Assembly { get; set; }
public string AssemblyVersion { get; set; }
public string FrameworkVersion { get; set; }
public string MassTransitVersion { get; set; }
public string OperatingSystemVersion { get; set; }
}
}
The message queued in ServiceBus:
{
"DestinationAddress": "sb://organization.servicebus.windows.net/topic",
"MessageId": "00010000-0faa-0009-6939-08d854e183aa",
"ConversationId": "00010000-0faa-0009-69f0-08d854e183aa",
"SourceAddress": "",
"Host": {
"MachineName": "NOTEXXXXX",
"ProcessName": "iisexpress",
"ProcessId": 32608,
"Assembly": "MassTransit.MultipleBus",
"AssemblyVersion": "1.0.0.0",
"FrameworkVersion": "3.1.5",
"MassTransitVersion": "7.0.2.0",
"OperatingSystemVersion": "Microsoft Windows NT 10.0.19041.0"
},
"Headers": {
},
"Message": {
"nome": "TESTE 2",
"acaoId": 2,
"mensagem": "TESTE 2"
},
"MessageType": [
"urn:message:MassTransit.MultipleBus.ServiceBus.Models.Commands:Proposta"
]
}
The consumer startup:
services.AddMassTransit<IAzureServiceBusService>(x =>
{
x.AddConsumer<EnviarExemploPocAzureServiceBusCommandHandler>();
x.UsingAzureServiceBus((context, cfg) =>
{
cfg.UseConcurrencyLimit(serviceBusSettings.AzureServiceBusSettings.ExemploAplicacaoPocA.ConcurrencyLimit);
cfg.Host("connString", h =>
{
h.OperationTimeout = TimeSpan.FromSeconds(serviceBusSettings.AzureServiceBusSettings.ExemploAplicacaoPocA.OperationTimeout);
h.TransportType = Microsoft.Azure.ServiceBus.TransportType.AmqpWebSockets;
});
cfg.UseServiceBusMessageScheduler();
});
});
services.AddMassTransitHostedService();
services.AddSettings(configuration);
The handler:
namespace MassTransit.MultipleBus.ServiceBus.Handlers.CommandHandlers
{
public class EnviarExemploPocAzureServiceBusCommandHandler : IConsumer<Proposta>
{
public async Task Consume(ConsumeContext<Proposta> context)
{
await Task.CompletedTask;
}
}
public class EnviarExemploPocAzureServiceBusFaultCommandHandler : IConsumer<Fault<EnviarExemploPocAzureServiceBusCommand>>
{
public async Task Consume(ConsumeContext<Fault<EnviarExemploPocAzureServiceBusCommand>> context)
{
await Task.CompletedTask;
}
}
}
When I send the message using MassTransit as well, the consumer works and the message is queued like:
{
"messageId": "00010000-0faa-0009-6497-08d854edbfe1",
"conversationId": "00010000-0faa-0009-3b83-08d854edbfe5",
"sourceAddress": "sb://organization.servicebus.windows.net/XXXX",
"destinationAddress": "sb://organization.servicebus.windows.net/topic",
"messageType": [
"urn:message:MassTransit.MultipleBus.ServiceBus.Models.Commands:Proposta"
],
"message": {
"nome": "TESTE DOIS",
"acaoId": 2,
"mensagem": "TESTE DOIS"
},
"sentTime": "2020-09-09T18:25:37.6127127Z",
"headers": {
"MT-Activity-Id": "|1e87ff49-4beb56732bfd4887.3."
},
"host": {
"machineName": "NOTEXXXXXX",
"processName": "iisexpress",
"processId": 37092,
"assembly": "MassTransit.MultipleBus",
"assemblyVersion": "1.0.0.0",
"frameworkVersion": "3.1.5",
"massTransitVersion": "7.0.2.0",
"operatingSystemVersion": "Microsoft Windows NT 10.0.19041.0"
}
}
What am I doing wrong?
The namespace seems to be correct, I did everything I found about it and I can't make it work.
Can someone help me?

Related

System.ArgumentException: 'Id field is not correctly populated'

I am trying to implement Redis, I am sending a put request from Postman and getting error.
Controller:
[HttpPut]
[ProducesResponseType((int)HttpStatusCode.Created, Type = typeof(Brand))]
public async Task<IActionResult> Update([FromBody] Brand brand)
{
try
{
await _brandCollection.UpdateAsync(brand);
return Ok();
}
catch (Exception ex)
{
throw ex;
}
}
Error:
System.ArgumentException: 'Id field is not correctly populated'
From redis desktop manager I have confirmed that Id exists in record
Postman:
{
"Id": 1,
"Name": "Xiaomi",
"IsRegistered": "Yes",
"About": "Mobile Brand",
"Countries": [
"Pakistan",
"India",
"China"
]
}
Brand model class:
[Document(StorageType = StorageType.Json, Prefixes = new[] { "Brand" })]
public class Brand
{
[Indexed] public int Id { get; set; }
[Indexed] public string? Name { get; set; }
[Indexed] public string? IsRegistered { get; set; }
[Indexed] public string? About { get; set; }
[Indexed] public List<string>? Countries { get; set; }
}
I have injected HostedService as well in Program.cs:
builder.Services.AddHostedService<IndexCreationService>();
IndexCreationService:
public class IndexCreationService : IHostedService
{
private readonly RedisConnectionProvider _provider;
public IndexCreationService(RedisConnectionProvider provider)
{
_provider = provider;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await _provider.Connection.CreateIndexAsync(typeof(Person));
await _provider.Connection.CreateIndexAsync(typeof(Category));
await _provider.Connection.CreateIndexAsync(typeof(Brand));
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}

Injecting settings into service from appsettings.JSON

I have a service which read the data from a queue (Rabbit MQ - ProduceQueue), takes action on the message and publishes the status on a separate status queue (Rabbit MQ - StatusQueue).
The issue I am facing is I am not able to instantiate the Producer and Consumer with separate settings when I resolve the service instance.
JSON Settings:
"RabbitMQConnection": {
"UserName": "guest",
"Password": "guest",
"HostName": "localhost",
"QueueName": "TaskExecutorPublisher",
"ExchangeName": "TaskProducerExchange",
"Port": "9090"
},
"RabbitMQStatusConnection": {
"UserName": "guest",
"Password": "guest",
"HostName": "localhost",
"QueueName": "SequenceStatusPublisher",
"ExchangeName": "StatusExchange",
"Port": "9091"
}
Program.cs:
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(app => { app.SetBasePath(Directory.GetCurrentDirectory()); app.AddJsonFile("appsettings.json"); })
.ConfigureServices((context, services) =>
{
//services.AddMvcCore();
//services.AddMvc();
services.AddLogging();
services.AddSingleton<IMQConsumer, RabbitMQConsumer>()
.AddOptions<IMQConnectionConfiguration>().Configure(options =>
{
options.HostName = context.Configuration["RabbitMQConnection:HostName"];
options.Port = Convert.ToInt32( context.Configuration["RabbitMQConnection:Port"]);
options.UserName = context.Configuration["RabbitMQConnection:UserName"];
options.Password = context.Configuration["RabbitMQConnection:Password"];
options.ExtensionProperties = new Dictionary<string, string>() {
{ "QueueName", context.Configuration["RabbitMQConnection:QueueName"] },
{ "ExchangeName",context. Configuration["RabbitMQConnection:ExchangeName"] }};
});
services.AddSingleton<IMQProducer, RabbitMQProducer>()
.AddOptions<IMQConnectionConfiguration>().Configure(options =>
{
options.HostName = context.Configuration["RabbitMQStatusConnection:HostName"];
options.Port = Convert.ToInt32(context.Configuration["RabbitMQStatusConnection:Port"]);
options.UserName = context.Configuration["RabbitMQStatusConnection:UserName"];
options.Password = context.Configuration["RabbitMQStatusConnection:Password"];
options.ExtensionProperties = new Dictionary<string, string>() {
{ "QueueName", context.Configuration["RabbitMQStatusConnection:QueueName"] },
{ "ExchangeName",context. Configuration["RabbitMQStatusConnection:ExchangeName"] }};
});
services.AddHostedService<ExecutorServiceAPI>();
services.AddSingleton<IMQConnectionConfiguration, ConnectionConfiguration>(option =>
{
var connectionConfig = new ConnectionConfiguration();
connectionConfig.HostName = context.Configuration["RabbitMQConnection:HostName"];
connectionConfig.Port = Convert.ToInt32(context.Configuration["RabbitMQConnection:Port"]);
connectionConfig.UserName = context.Configuration["RabbitMQConnection:UserName"];
connectionConfig.Password = context.Configuration["RabbitMQConnection:Password"];
connectionConfig.ExtensionProperties = new Dictionary<string, string>() {
{ "QueueName", context.Configuration["RabbitMQConnection:QueueName"] },
{ "ExchangeName",context. Configuration["RabbitMQConnection:ExchangeName"] }};
return connectionConfig;
});
}).UseWindowsService()
ExecutorService code :
public class ExecutorServiceAPI : IHostedService
{
private readonly ILogger _logger;
private IMQConsumer _consumerConnection;
private IMQProducer _producerConnection;
public ExecutorServiceAPI(ILogger<ExecutorServiceAPI> logger, IMQConsumer consumerConnection, IMQProducer producerConnection)
{
if (consumerConnection == null)
throw new ArgumentNullException(nameof(consumerConnection));
_logger = logger;
_consumerConnection = consumerConnection;
_producerConnection = producerConnection;
}
}
In appsettings.json:
"Settings": {
"RabbitMQConnection": {
"UserName": "guest",
"Password": "guest",
"HostName": "localhost",
"QueueName": "TaskExecutorPublisher",
"ExchangeName": "TaskProducerExchange",
"Port": "9090"
},
"RabbitMQStatusConnection": {
"UserName": "guest",
"Password": "guest",
"HostName": "localhost",
"QueueName": "SequenceStatusPublisher",
"ExchangeName": "StatusExchange",
"Port": "9091"
}
}
Settings:
public class Settings
{
public SettingProperties RabbitMQConnection { get; set; }
public SettingProperties RabbitMQStatusConnection { get; set; }
}
SettingProperties:
public class SettingProperties
{
public string UserName { get; set; }
public string Password { get; set; }
public string HostName { get; set; }
public string QueueName { get; set; }
public string ExchangeName { get; set; }
public string Port { get; set; }
}
In Progarm.cs (.net 6) , add below code:
builder.Services.Configure<Settings>(builder.Configuration.GetSection("Settings"));
Then in HomeController:
public class HomeController : Controller
{
private readonly Settings _MysettingsM;
public HomeController(IOptions<Settings> APPsettings)
{
_MysettingsM = APPsettings.Value;
}
public IActionResult Index()
{
var R1= _MysettingsM.RabbitMQConnection;
var R2 = _MysettingsM.RabbitMQStatusConnection;
return View();
}
}
result:

How to properly deserialize asp.net core Model State errors to object in xamarin forms

I am having a silly problem trying to deserialize asp.net core model state errors to an object. My code is like this
For backend register method
[HttpPost("register-user")]
[ValidateModel]
public async Task<IActionResult> Index(RegisterDto registerDto)
{
try
{
Data.Models.User user = mapper.Map<RegisterDto, Data.Models.User>(registerDto);
user.LockoutEnd = DateTimeOffset.Now;
user.Warehouse = configuration["Config:Warehouse"];
user.SiteId = Convert.ToInt32(configuration["Config:SiteId"]);
IdentityResult result = await userManager.CreateAsync(user, registerDto.Password);
if (result.Succeeded)
{
AddLogInformation(logger, "User created a new account with password.");
string token = await userManager.GenerateEmailConfirmationTokenAsync(user);
token = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));
string confirmationUrl = Url.Action("Index", "EmailConfirmation",
new {userId = user.Id, code = token}, Request.Scheme);
emailSender.SendEmailAsync(registerDto.Email, "Confirm your email",
GetEmailConfirmationTemplate(registerDto.FirstName, registerDto.LastName,
confirmationUrl));
ApplicationRole retailPersonRole =
await roleManager.FindByNameAsync(RoleHelper.GetRetailUserRoleName());
if (retailPersonRole != null) await userManager.AddToRoleAsync(user, retailPersonRole.Name);
if (userManager.Options.SignIn.RequireConfirmedAccount)
{
AddLogInformation(logger, "Sent email confirmation email to user");
return Ok(SuccessResult(null));
}
//If confirm account is set to false
await signInManager.SignInAsync(user, false);
return Ok(SuccessResult(null));
}
// If we got this far, something failed, redisplay form
return Ok(FailedMessage(logger, "Cannot register user at this time. Please try again later."));
}
catch (Exception e)
{
return ServerErrorJsonResult(logger, "Error while trying to register user. Error message is: " + e.Message);
}
}
And I am catching the model state error in action filter and returning a response as below.
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
var modelState = context.ModelState;
if (!modelState.IsValid)
context.Result = new BadRequestObjectResult(new JsonResult(new {modelError = true, Errors = modelState}));
}
}
Now in the front-end (xamarin), I have a model to where the error should be deserialised so that I can display a proper error to user.
My register model in the front end is like this
public class RegisterDto: BaseDto
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
public AddressDto BillingAddress { get; set; }
public RegisterDto Errors { get; set; }
}
My Address Dto is like this
public class AddressDto
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string PostCode { get; set; }
public string State { get; set; }
}
I am creating the post request in xamarin like this.
public async Task<TResult> PostAsync<TResult>(string uri, TResult data, string token = "", string header = "")
{
try
{
HttpClient httpClient = CreateHttpClient(token);
if (!string.IsNullOrEmpty(header))
{
AddHeaderParameter(httpClient, header);
}
var content = new StringContent(JsonConvert.SerializeObject(data));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = await httpClient.PostAsync(uri, content);
await HandleResponse(response);
string serialized = await response.Content.ReadAsStringAsync();
TResult result = await Task.Run(() =>
JsonConvert.DeserializeObject<TResult>(serialized, serializerSettings));
return result;
}
catch (Exception e)
{
return default;
}
}
And finally, in the view model, I am doing this
--
userToBeRegistered is an instance of RegisterDto
await something.PostAsync(UrlHelper.RegisterUrl, userToBeRegistered);
The serialized string output is like this
{
"errors":{
"Email":[
"Email is required"
],
"LastName":[
"Last name is required"
],
"Password":[
"Password is required"
],
"FirstName":[
"First name is required"
],
"PhoneNumber":[
"Phone number is required"
],
"BillingAddress.City":[
"Suburb is required"
],
"BillingAddress.State":[
"State is required"
],
"BillingAddress.Address1":[
"Street address is required"
],
"BillingAddress.PostCode":[
"Postcode is required"
]
},
"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title":"One or more validation errors occurred.",
"status":400,
"traceId":"|3c2d7d70-49a6eceecbeedab8."
}
My question is how can I deserialise it to an errors object. If I keep running the above code, then I get this error.
"Unexpected character encountered while parsing value: [. Path 'errors.Email', line 1, position 20."
Can anyone help me with this?
Try the below method.
Create ErrorInfor class:
class ErrorInfor
{
public MyError errors { get; set; }
public string type { get; set; }
public string title { get; set; }
public int status { get; set; }
public string traceId { get; set; }
public class MyError
{
public List<string> FirstName { get; set; }
public List<string> LastName { get; set; }
public List<string> PhoneNumber { get; set; }
public List<string> Email { get; set; }
public List<string> Password { get; set; }
[JsonProperty("BillingAddress.City")]
public List<string> Citiy { get; set; }
[JsonProperty("BillingAddress.State")]
public List<string> State { get; set; }
[JsonProperty("BillingAddress.Address1")]
public List<string> Address1 { get; set; }
[JsonProperty("BillingAddress.PostCode")]
public List<string> PostCode { get; set; }
}
}
then you could get the data from your above json string.
ErrorInfor errorInfor = JsonConvert.DeserializeObject<ErrorInfo>(json);

ASP.NET core POST request fail

I have a model:
public class CoreGoal
{
[Key]
public long CoreGoalId { get; set; }
public string Title { get; set; }
public string Effect { get; set; }
public string Target_Audience { get; set; }
public string Infrastructure { get; set; }
public virtual ICollection<Benefit> Benefits { get; set; }
public virtual ICollection<Step> Steps { get; set; }
public virtual ICollection<Image> Images { get; set; }
public virtual ICollection<SubGoal> SubGoals { get; set; }
public CoreGoal()
{
}
}
And Image model is as following:
public class Image
{
[Key]
public long ImagelId { get; set; }
public byte[] Base64 { get; set; }
[ForeignKey("CoreGoalId")]
public long CoreGoalId { get; set; }
public Image()
{
}
}
My controller class:
[Route("api/[controller]")]
public class CoreGoalController : Controller
{
private readonly ICoreGoalRepository _coreGoalRepository;
//Controller
public CoreGoalController(ICoreGoalRepository coreGoalRepository) {
_coreGoalRepository = coreGoalRepository;
}
//Get methods
[HttpGet]
public IEnumerable<CoreGoal> GetAll()
{
return _coreGoalRepository.GetAllCoreGoals();
}
[HttpGet("{id}", Name = "GetCoreGoal")]
public IActionResult GetById(long id)
{
var item = _coreGoalRepository.Find(id);
if (item == null)
{
return NotFound();
}
return new ObjectResult(item);
}
//Create
[HttpPost]
public IActionResult Create([FromBody] CoreGoal item)
{
if (item == null)
{
return BadRequest();
}
_coreGoalRepository.CreateCoreGoal(item);
return CreatedAtRoute("GetCoreGoal", new { id = item.CoreGoalId }, item);
}
}
Repository:
public class CoreGoalRepository : ICoreGoalRepository
{
private readonly WebAPIDataContext _db;
public CoreGoalRepository(WebAPIDataContext db)
{
_db = db;
}
//Add new
public void CreateCoreGoal(CoreGoal coreGoal)
{
_db.CoreGoals.Add(coreGoal);
_db.SaveChanges();
}
//Get all
public IEnumerable<CoreGoal> GetAllCoreGoals()
{
return _db.CoreGoals
.Include(coreGoal => coreGoal.Benefits)
.Include(coreGoal => coreGoal.Steps)
.Include(coreGoal => coreGoal.Images)
.Include(coreGoal => coreGoal.SubGoals)
.ToList();
}
//Find specific
public CoreGoal Find(long key)
{
return _db.CoreGoals.FirstOrDefault(t => t.CoreGoalId == key);
}
}
public interface ICoreGoalRepository
{
void CreateCoreGoal(CoreGoal coreGoal);
IEnumerable<CoreGoal> GetAllCoreGoals();
CoreGoal Find(long key);
void DeleteCoreGoal(long id);
void UpdateCoreGoal(CoreGoal coreGoal);
}
When I do a POST request from swagger I get a template like:
{
"coreGoalId": 0,
"title": "string",
"effect": "string",
"target_Audience": "string",
"infrastructure": "string",
"benefits": [
{
"benefitId": 0,
"what": "string",
"coreGoalId": 0
}
],
"steps": [
{
"stepId": 0,
"what": "string",
"coreGoalId": 0
}
],
"images": [
{
"imagelId": 0,
"base64": "string",
"coreGoalId": 0
}
],
"subGoals": [
{
"subGoalId": 0,
"title": "string",
"priority": "string",
"audience": "string",
"social_aspects": "string",
"coreGoalId": 0,
"issues": [
{
"issueId": 0,
"title": "string",
"subGoalID": 0
}
]
}
]
}
If I POST like like this, my request fails with status 400, however if I remove
"images": [
{
"imagelId": 0,
"base64": "string",
"coreGoalId": 0
}
],
from this request, then it is successful. Why is it happening? All other models i.e. Benefit, Step are exactly identical to Image in structure.
UPDATE:
Changing base64 type from byte[] to string eliminates this problem but in that case while saving to my MySql database the big base64 string is chopped and kind of becomes useless to again form the image.

Box V2 API - Where is lock/unlock?

There is a note in the developer road map from December of 2013 saying, "Lock/Unlock – We’ve added support for locking and unlocking files into the V2 API."
I've been all through the V2 API (for c#) and cannot find it anywhere. I expected to find something in the BoxFilesManager class or as something you would pass to UpdateInformationAsync within the BoxFileRequest class.
So is there a way to lock/unlock a file?
Great question. In order to see the current lock status of a file do a
GET https://api.box.com/2.0/files/7435988481/?fields=lock
If there is no lock on the file, you'll get something like this back:
{
"type": "file",
"id": "7435988481",
"etag": "0",
"lock": null
}
If you want to lock a file, you need to do a PUT (update) on the /files/ endpoint with a body that tells us what type of lock, and when to release it. Like this:
PUT https://api.box.com/2.0/files/7435988481/?fields=lock
{"lock": {
"expires_at" : "2014-05-29T19:03:04-07:00",
"is_download_prevented": true
}
}
You'll get a response confirming your lock was created:
{
"type": "file",
"id": "7435988481",
"etag": "1",
"lock": {
"type": "lock",
"id": "14516545",
"created_by": {
"type": "user",
"id": "13130406",
"name": "Peter Rexer gmail",
"login": "prexer#gmail.com"
},
"created_at": "2014-05-29T18:03:04-07:00",
"expires_at": "2014-05-29T19:03:04-07:00",
"is_download_prevented": true
}
}
Since there isn't a lock/unlock yet, I created a Lock Manager based on the existing managers:
class BoxCloudLockManager : BoxResourceManager
{
#region Lock/Unlock Classes
[DataContract]
internal class BoxLockRequestInfo
{
[DataMember(Name = "status")]
public string Status { get; set; }
//[DataMember(Name = "expires_at")]
//public string ExpiresAt { get; set; }
[DataMember(Name = "is_download_prevented")]
public bool IsDownloadPrevented { get; set; }
}
[DataContract]
internal class BoxLockRequest
{
[DataMember(Name = "lock")]
public BoxLockRequestInfo Lock { get; set; }
}
#endregion
const string LockFileString = "{0}/?fields=lock";
public BoxCloudLockManager(IBoxConfig config, IBoxService service, IBoxConverter converter, IAuthRepository auth)
: base(config, service, converter, auth)
{
}
public async Task<BoxLockInfo> LockAsync(string documentId,bool isDownloadPrevented = true)
{
var lockRequest = new BoxLockRequest { Lock = new BoxLockRequestInfo { Status = "lock", IsDownloadPrevented = isDownloadPrevented } };
BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
.Method(RequestMethod.Put)
.Payload(_converter.Serialize(lockRequest));
IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);
return response.ResponseObject;
}
public async Task<BoxLockInfo> UnlockAsync(string documentId)
{
BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
.Method(RequestMethod.Put)
.Payload("{\"lock\":null}");
IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);
return response.ResponseObject;
}
public async Task<BoxLockInfo> GetLockInfoAsync(string documentId)
{
BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
.Method(RequestMethod.Get);
IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);
return response.ResponseObject;
}
}
I derived a class from BoxClient, adding a LockManager and instantiate it within the Constructor.
Here is the Lock Info:
[DataContract]
public class BoxLockedBy
{
[DataMember(Name = "type")]
public string Type { get; set; }
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "login")]
public string Login { get; set; }
}
[DataContract]
public class BoxLockDetails
{
[DataMember(Name = "type")]
public string Type { get; set; }
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "created_by")]
public BoxLockedBy CreatedBy { get; set; }
[DataMember(Name = "created_at")]
public string CreatedAt { get; set; }
[DataMember(Name = "expires_at")]
public string ExpiresAt { get; set; }
[DataMember(Name = "is_download_prevented")]
public bool IsDownloadPrevented { get; set; }
}
[DataContract]
public class BoxLockInfo
{
[DataMember(Name = "type")]
public string Type { get; set; }
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "etag")]
public string Etag { get; set; }
[DataMember(Name = "lock")]
public BoxLockDetails LockDetails { get; set; }
}