Tag Archives: protobuf

“protobuf” visual studio a namespace cannot directly contain members such as fields or methods

Hi everyone,

I ran into the following intellisense error while adding a new proto file:

“protobuf” visual studio a namespace cannot directly contain members such as fields or methods

This was one of many errors that were shown and appeared to be an issue with Intellisense mistaking the file for a normal class definition. The following appears to have fixed it:

1) Right click on the file and select “exclude from project”
2) Clean the solution and rebuild
3) Re-include the file in the project (you may have to toggle ‘Show All Files’ at the top of solution explorer to see the excluded item).

Cheers,
Chris

Automapper protobuf datetime to timestamp c#

Hi everyone,

I ran into the following error tonight while attempting to use automapper to map a datetime field to a protobuf timestamp property:

automapper missing type map “DateTime -> Timestamp”

To fix it, add the following to your mapping profile:

CreateMap().ConvertUsing(x => x.ToTimestamp());

UPDATE:
I later hit a similar error parsing a different entity:

Type Map configuration:
Entity-> EntityDto
EntityService.Models.Entity-> EntityService.EntityDto

Destination Member:
CreatedAt

—> System.ArgumentException: Conversion from DateTime to Timestamp requires the DateTime kind to be Utc (Parameter ‘dateTime’)

Implementing the following map appears to have resolved it:

CreateMap().ConvertUsing(x => Timestamp.FromDateTime(DateTime.SpecifyKind(x, DateTimeKind.Utc)));

If you’re planning to go the other way (timestamp -> datetime) you may also need the following:

CreateMap().ConvertUsing(x => x.ToDateTime());

I’m currently using the following base class in my library:

public class BaseMappingProfile : Profile
{
public BaseMappingProfile()
{
CreateMap().ConvertUsing(x => Timestamp.FromDateTime(DateTime.SpecifyKind(x, DateTimeKind.Utc)));
CreateMap().ConvertUsing(x => x.ToDateTime());
}
}

Google didn’t seem to turn up a lot so I thought I’d post it here in case it helps someone out!