Getting ProtoGen with protobuf-net - c#

I'm trying to serialize some classes with protobufs. I installed protobuf-net using NuGet, properly decorated my code with the attributes, and everything is running great.
However, eventually messages will be created in C#, but consumed in other environments, so I need to either create .proto files from C#, or - what seems more reasonable to me - create C# code from .proto files.
No matter what I do, I can't find ProtoGen.exe installed anywhere in my computer. I also found this old post, but nothing of the sort happens when I create a .proto text file. Adding ProtoBufTool manually didn't work, either.
What am I missing?

Protogen is included in the v1/280 build, or there is a separate VS tool available from the project site. There isn't a convenient way to include it in nuget in any sensible way (I checked with MS folks; twice).
For both protogen and the VS addin, the functionality has not changed for v2, so the existing v1 binaries remain fine for use with v2.
If the ProtoBufTool didn't work then... I don't know what is up with that; was there any error message / warning? The classic warning is about the input file's encoding ("protoc", google's tool, is very fussy about encoding).

Related

Do I really need whole Grpc.Core NuGet package to use Google PubSub in simple C# application

I am creating a simple C# desktop application that is pulling messages from Google Cloud PubSub. I noticed that PubSub is using Grpc.Core package that when installed is more than 500 mb! It contains many files that I don't need(for mac, android, etc.) and it doesn't seem reasonable to use such package if my application size is only few mb. There is a discussion here:
Why is Grpc.Core NuGet package so big?
In comments section it is said that it is possible to target more specific packages to suit specific needs. So my question is - is there more specific package/s that can be used to simply pull messages from Cloud PubSub to desktop application?
I feel your pain. (In fact, I feel it many times over. When I do a complete build of the google-cloud-dotnet repo that I work on, it pulls in those libraries many times over, and ends up being vast.)
It would be nice if you could add a sort of "negative dependency" to say "I don't want Grpc.Core even though Google.Cloud.PubSub.V1 depends on it indirectly, please use Grpc.Net.Client instead", but I don't believe there's any simple way of doing that in MSBuild projects.
We do make a "best-effort" attempt to support Grpc.Net.Client via the Google.Api.Gax.Grpc.GrpcNetClient package - you can depend on that package and then set the GrpcAdapter property in a ClientBuilderBase<TClient> to GrpcNetClientAdapter.Default. However:
The Pub/Sub libraries are slightly trickier to reconfigure than others, due to the manual layer of code wrapping the generated code. (I can look into how to perform that configuration if you're interested.)
We haven't done significant testing with Grpc.Net.Client, and the Pub/Sub library in particular performs a lot of streaming; while it should all just work, it's possible that there could be problems.
Doing that doesn't actually remove the Grpc.Core dependency anyway - so you'd need to manually remove the files you don't need.
There really isn't a more specific package that you can target - all I can suggest is that you delete the files you don't need. You could do that in a build target that runs post compile, for example. It's possible that there's some cunning way to tell MSBuild that when it would copy (say) the iOS libraries into a specific location, just exclude them instead - but I don't know enough MSBuild to say how you'd do that (when they're being copied due to a dependency rather than due to the project itself).

Missing googleapis/google/protobuf

I am attempting to compile a C# library for GoogleCloudSpeechToTextv1p1beta1 from a .proto file here. The protobuf compiler works, except that it is missing dependencies from the googleapis/google/protobuf directory. All of the other import files that it depends on exist, here.
My question is this: Why is the protobuf directory missing from the googleapis/google directory? Is it a library that I need to compile/assemble personally? Or is it something Google should be providing and it's just missing?
The google/protobuf directory is shipped with the Google.Protobuf.Tools package, under tools (so there's tools/google/protobuf/any.proto for example). Alternatively, the same files are within the protobuf GitHub repo (under src - the protos are mixed with the C++ code).
So you should include the tools directory as a "root" in the protoc call if you need to... but in this particular case, there's already a NuGet package of Google.Cloud.Speech.V1P1Beta1 so I'd suggest you use that instead :) (There are even docs available if you know to extrapolate the URL scheme from other APIs. It's not really advertised as such.)

Why doesn't MSHTML for .Net have querySelector and querySelectorAll, or where are they?

I want to use MSHTML [1] to access DOM in IE, but I have just found out that it lacks querySelector and querySelectorAll methods in HTML document classes, which will otherwise be very helpful to obtain elements within a document by CSS selectors.
The only workaround I can think of for now is to do everything by executing a script, but it is awful.
Is there any MSHTML version that has the two methods (querySelector and querySelectorAll)? Is there any other libraries (hopefully, official .Net libraries by Microsoft) where the two methods or their equivalents are available?
[1] %ProgramFiles(x86)%\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll
The querySelector() method and friends are alive and well in MSHtml and the IE DOM, the problem is that the PIA that most programmers have on their machine is heavily outdated. The one registered in the GAC on my machine carries a 7.0.3300.0 version number, that dates back to 2002.
There is a workaround for that, you can generate your own interop library and avoid the one in the GAC. The trick is to intentionally generate a mismatch by creating an interop assembly without a strong name so it cannot match the one in the GAC. Be sure to do this only on VS2010 or later, you want to take advantage of the Embed Interop Types feature.
Start the Visual Studio Command Prompt and navigate to your project directory. And execute this command:
Tlbimp c:\windows\system32\mshtml.tlb
It will trundle for a handful of minutes, the type library is very large, and spit out several icky looking warnings that you can ignore. Go back to VS, remove the existing MSHTML reference and use Add Reference, Browse tab. Pick the MSHTML.dll file that was generated by Tlbimp.
Do keep in mind that you now use an interop library that is compatible with the IE version that you have on your machine. Not necessarily the version your user has, so technically it is possible for your program to fail. You'd have to be very unlucky.
Yesterday I notice the same problem, I found a solution.
The Next example code works for me:
Document.querySelectorAll("#navbar_search>a").item(0).click();
If i try to do "the same", but with this code, it fails:
HTMLDocument doc;
doc = ie.Document;
doc.querySelectorAll("#navbar_search>a").item(0).click();
I'm not a COM expert but I thing the first solution is a late binding (dependences are resolved at run-time), while the second solution is a compliler-time binding and it depends on a 'good' predefined libary and that's not the case with mshtml.

Is there a pretty printer / code formatter for C# (as part of build system)?

Is there a pretty printer / code formatter for C# (as part of build system)? Read as: "lives outside of Visual Studio". It seems like there are plenty of these kinds of things for Java, C++/C, Go -- so it seems more than reasonable that C# should also have some code formatter that lives outside of the IDE?
(I'd actually like to couple the formatter with something like StyleCop, and have devs run it as part of the process before a commit).
NOTE: Not syntax highlighting, as in code in a web page. Instead, a Code beautifier, or code pretty printer, which would take code and format it to a style/standard coding format... which StyleCop does a style check to see if the code meets the format.
JetBrains also makes a free command line tool named CleanupCode that formats c# based on .editorconfig settings. I wrote a wrapper utility for it named ReGitLint that makes it easier and faster to run as a pre-commit hook or on the build server. This can help out a lot if you've got teammates using Visual Studio Code where ReSharper isn't an option.
To get up and going just run the following commands
dotnet tool install JetBrains.ReSharper.GlobalTools
dotnet tool install ReGitLint
then add the following to .git/hooks/pre-commit
#!/bin/sh
dotnet regitlint -f staged --fail-on-diff
To enforce formatting on jenkins add this to your build script
dotnet tool restore
dotnet regitlint --jenkins
For more options check out the readme
There finally is; Csharpier!
It was originally ported from the popular Prettier and it is just a code formatter for C#. It can run from the command line and is not tied to an IDE nor an extension, although support for these exists as well. You can make it run as a part of a git-hook to for example automatically format changed files on git commit.
Some benefits of choosing Csharpier over other formatting tools are:
It breaks long lines, unlike most formatters (such as dotnet-format)
It doesn't preserve the original formatting. This benefit is perhaps best explained by reading the section Building and enforcing a style guide on the Prettier web page.
For .NET SDK projects the dotnet format command can be used.
dotnet format is a code formatter that applies style preferences to a project or solution. Preferences will be read from an .editorconfig file, if present, otherwise a default set of preferences will be used.
It may also work for classic .NET Framework projects if the project does not import things that are not available on .NET SDK, like text templating msbuild targets.
The dotnet format source repository is located on GitHub dotnet/format.
The .editorconfig rules are documented, and can be exported from and will be used by Visual Studio too.
The first that comes to mind is ReSharper. However, it's an add-in to VS which isn't what you were asking for. I'm not sure if you can set it up to auto-format on save.
From their website:
ReSharper can reformat an arbitrary selected block of code, all code
in the current file, all files in a directory, or even in the entire
project or solution, according to your Code Style preferences.
ReSharper provides distinct formatting options for C#, VB.NET,
JavaScript, CSS and XML code. Reformatting affects braces layout,
blank lines, line wrapping and line breaks, spaces in various
contexts, indentation, aligning multiple constructs, and a lot more
options that you can fine-tune in the ReSharper Options dialog box.
.NET 6+ — dotnet format:
10 years after this question was asked, the .NET SDK now comes with a built-in formatting command: dotnet format — see the documentation and GitHub repo for more information.
dotnet format used to be a third-party tool which eventually got merged into the official .NET SDK, starting from .NET 6 — see anouncement.
Usage:
You can run the command in any directory that has a .csproj or .sln file present, all C# (or VB.NET) files across the solution/project will be formatted:
dotnet format
Or you can specifically run it for individual files:
dotnet format --include Program.cs
The formatting will be based on preferences outlined in an .editorconfig file, if you have one present, otherwise it'll use its own defaults.
If you have an existing and unusual style guideline about whitespaces and brace placements AStyle is probably flexible enough to match it. AStyle is not C# specific, it can do many c-like languages.
NArrange is also good if it happens to match your style guidelines. It can be a more aggressive, for example it can put parts of your classes into different regions and sort your methods.
Microsoft has a code formatter tool that use Roslyn to automatically rewrite source code according to their guidelines.
Please check that link
https://github.com/dotnet/codeformatter
Update: NArrange is not developed any more (see also https://github.com/MarcStan/narrange for a slightly newer fork).
To automate code formatting (kind of micro refactoring) during build you need a tool you can run from command line (and you can easily integrate in msbuild).
A good one if your needing isn't too advanced (authors define it as beautifier) is NArrange. It fully supports C# and VB.NET for Framework 2.0 but many widely used features of newer language versions are supported too.
NArrange is a .NET code beautifier that automatically organizes code members and elements within .NET classes.
You can now use https://www.npmjs.com/package/narrange leveraging the power of node.js
Super simple, configurable setup :)
const { createNArrange } = require("narrange");
const path = require("path");
createNArrange({
srcPath: path.join(__dirname, "src/apps/mango"),
configFilePath: path.join(__dirname, "config/NArrange.xml")
});

generating licenses.licx

I've got a bit of a problem. I'm moving my source repository from one machine to another, and in the process I'm doing some culling of what's stored as I've learned more about creating/managing a repository since I started.
The problem is that we're using dxperience tools from devexpress and it uses the .net license system (licenses.licx). Originally I had this license in the repository, and I'm hearing that this isn't necessarily the best idea. So I haven't included it in the repository. But now, when I checkout the project from the repository on my machine (same machine that I was checking out to before the move), it's looking for the license file and not generating it as (I think) it should be.
We have run into the same problem using Infragistics controls.
Our solution has been to keep a blank licnenses.licx file in our source repository (Source Gear Vault) and then change the properties of the file to Read Only false on our local workations. This way we do not end up stepping on each other with that file and it is generated with the proper keys off of our workstations.
Of course this is a bit of a manual work around that may not be suitable for you, but that is how we have been doing it.
Alternatively, you can install the EmptyLicensesLicx nuget package, and it will make sure there's an empty Licenses.licx in your project, before it gets compiled (which is all you need).
This file should be compiled into your deployment assemblies automatically by having licenses.licx included as an embedded resource. Under the hood, Visual studio uses lc.exe to include this in your assemblies.
http://www.atalasoft.com/kb/Article.aspx?id=10103
delete existing licence file
solve all errors
check properties of solution, DLL name, source name
rebuild the project

Categories