how to check service bus topic empty or not c# - c#

I wonder is there any way to check service bus topic empty or not
I tried with nuget WindowsAzure.ServiceBus and below sample code.
In this nuget I do not get ITopicClient :(
var topicClient = TopicClient(); // we can not create object
var topicPeek = topicClient.Peek();
TopicDescription topicDescription = new TopicDescription(topicName);
var topicSize = topicDescription.SizeInBytes;
any way to do so?

With the WindowsAzure.ServiceBus package, you can create instances of Service Bus clients by using MessagingFactory.Create to get a reference to a MessagingFactory. Once you have one of those, you can call CreateTopicClient to get a TopicClient instance.
(Note that there's also a newer package called Microsoft.Azure.ServiceBus that's a bit limited in functionality, but it supports .NET Core. If you use that package, the class hierarchy is somewhat different, and you can create instances of the clients directly.)

Related

How can I execute a plugin from a test method?

I've added a test project to my solution where I want to test the integrations, and by that testing the plugins from my local machine. I've added Microsoft.Crm.Tooling.Connector and have a connection to my test instance. But I'm unsure on how and what the configuration and service is set up.
var crm = new CrmServiceClient(crmConnectionString);
crm.OrganizationServiceProxy.EnableProxyTypes();
var service = crm.OrganizationServiceProxy;
var unsecureConfig = "?";
var secureConfig = "?";
var plugin = new ExternalWorkorder_OnCreate(unsecureConfig, secureConfig);
plugin.ExecutePluginLogic(service?);
For executing the plugin, does the configuration matter? As long as i have the IServiceProvider, and how do I get that? Can I get it from CrmServiceClient? Or the OrganizationServiceProxy?
No you do not need t worry about secure and unsecure config.
Look at this article which will connect to dynamics and perform operations as expected.
You don't need the configuration if youre not counting on it in Plugin. But depending on how the plugin code is structured you will need to provide some even empty configuration.
I would recommend using FakeXrmEasy. https://dynamicsvalue.com/home
There are many examples on how to use the library.

Google Cloud PubSub V1 using GCloud Emulator

I'm fighting with Google Docs for setting up Cloud PubSub with .NET using a PubSub emulator.
https://cloud.google.com/dotnet/docs/getting-started/using-pub-sub
https://cloud.google.com/pubsub/docs/publisher
https://cloud.google.com/pubsub/docs/emulator
Coming from a Rails background, I'm tasked to implement Cloud PubSub for a .NET product, running our google cloud on .NET Core, to enable it to publish.
Google::Cloud::Pubsub.new(project: project_id, emulator_host: emulator_host)
From the documentation using .NET, I keep coming back to the following:
PublisherServiceApiClient publisherClient = PublisherServiceApiClient.Create();
PublisherClient publisher = PublisherClient.Create(...)
However, the library used from the docs Google.Cloud.PubSub.V1 -Pre
does not contain the definition.
'PublisherClient' does not contain a definition for 'Create'.
Instead, I get CreateAsync that takes in TopicName, PublisherClient.ClientCreationSettings and PublisherClient.Settings.
https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/api/Google.Cloud.PubSub.V1.PublisherClient.html
I noticed that PublisherServiceApiClient can take in a Channel, but I'm confused on how to get this going.
To conclude with an actual question, how does one currently implement Cloud PubSub with .NET for in cloud and then locally with emulator? Adding to that, am I using the wrong library or the wrong docs?
Any suggestions, pointers or piece of advice would be truly appreciated.
I managed a solution that I am happy with.
Instead of using the PublisherClient, I went with using the PublisherServiceApiClient alone.
emulatorAddr = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST");
if (emulatorAddr != null)
{
channel = new Channel(emulatorAddr, ChannelCredentials.Insecure);
pub = PublisherServiceApiClient.Create(channel);
}
else
{
pub = PublisherServiceApiClient.Create();
}
Which meant that publishing was slightly more involved then sending string to the PublisherClient, but overall not so bad.
PubsubMessage msg = new PubsubMessage
{
Data = ByteString.CopyFromUtf8(JsonConvert.SerializeObject(payload))
};
pub.PublishAsync(topic, new[]{ msg });
If the project is running in a Google Compute Engine, it will have default credentials. Otherwise, wether you're running an emulator locally or in docker you can define PUBSUB_EMULATOR_HOST.
What really helped was this https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/index.html
To make the PublisherClient connect to a local emulator, you need to pass custom ServiceEndpoint and ChannelCredentials to CreateAsync:
var serviceEndpoint = new ServiceEndpoint(theEmulatorHost, theEmulatorPort);
var publisherClient = await PublisherClient.CreateAsync(
topicName,
new PublisherClient.ClientCreationSettings(credentials: ChannelCredentials.Insecure, serviceEndpoint: serviceEndpoint));
To switch to the real PubSub, just leave away the ClientCreationSettings.
You can use the EmulatorDetection property on the ClientCreationSettings using extension method .WithEmulatorDetection(EmulatorDetection.EmulatorOrProduction). Like this:
PublisherClient publisher = await PublisherClient.CreateAsync(
topicName,
new PublisherClient.ClientCreationSettings()
.WithEmulatorDetection(EmulatorDetection.EmulatorOrProduction));
This will work if you have the following environment variable for the local emulator endpoint: PUBSUB_EMULATOR_HOST=localhost:8085
(If you use Visual Studio you might have to restart VS for the environment variable to be detected)
In windows I had problems using the set PUBSUB_EMULATOR_HOST=localhost:8085 command, so I ended up adding it manually.
Details here: https://cloud.google.com/pubsub/docs/emulator
Extra tip: you can add topics directly to API using curl: curl -X PUT http://localhost:8085/v1/projects/my-project-name/topics/my-topic

Find out hosting Uri when selfhosting JobHost

I'm trying to self host a JobHost using Microsoft.Azure.WebJobs and including the Http extension but I can't seem to figure out what Uri/port it is hosting on
This is my Main method:
static void Main(string[] args)
{
var config = new JobHostConfiguration();
var filter = new LogCategoryFilter();
filter.DefaultLevel = LogLevel.Trace;
config.LoggerFactory = new LoggerFactory()
.AddConsole(filter.Filter);
var httpExtensionConfiguration = new HttpExtensionConfiguration();
config.UseHttp(httpExtensionConfiguration);
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
var host = new JobHost(config);
host.RunAndBlock();
}
and here is the output window when running
While the HTTP Extension will add the required bindings, services and HTTP features, it does not provide a listener, so it won't setup the host for you (it relies on an external listen you'd need to setup).
With the Azure Functions runtime, the WebHost itself is the listener. The CLI uses that implementation in order to spin up the host and expose HTTP functions. You can see this approach here:
https://github.com/Azure/azure-functions-cli/blob/f0e8121c51569d8d0551fbb9bb81fbed5a9ad64c/src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs#L102-L112
You could have a simpler approach if you don't want to rely on the Script WebHost (the CLI leverages many of its features, so it makes sense there) by simply providing your application directly when building the host. You can look at the Startup class provided by the CLI to see how things are registered and configured with the latest bits:
https://github.com/Azure/azure-functions-cli/blob/ff45a85c462c6f1e83e04dcba13da8bcca7099c5/src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs#L349-L374
NOTE: The extension version you're using, as well as the code I've shared are pre-release (or not even merged yet), so they're subject to change, but that's the direction we're going.

List all AWS RDS instances using AWS .NET SDK

I'm trying to list all my RDS instances on AWS, using the .NET SDK for AWS.
I was expecting the SDK to offer something similar to the SDK's EC2 describe-instances, and sure enough, that is part of the CLI, but not so straight-forward in the SDK.
Does anyone know how to do this ?
Solution
The AWS .NET SDK (v3) contains a similar construct for RDS as for EC2. I missed that somehow. See my answer with source-code below.
Thanks in advance
I think you are looking for DescribeDBInstances. The DescribeDBInstancesResult has a list of DBInstances. That's where you'll find the information on each RDS instance.
Edit: The function and object names are the same but here's the link for V3.
So it turns out, that the procedure to get all RDS instances closely mimic the EC2 way of doing it.
You will need to install the AWSSDK.RDS nuget package
In Package Management Console in VS.NET
Install-Package AWSSDK.RDS
Once you have done that, you will need to add the necessary assemblies:
using Amazon.RDS;
using Amazon.RDS.Model;
And then you can do something like this:
public static void ListAllRDSInstances(RegionEndpoint region)
{
var c = new AmazonRDSClient(region);
var request = new DescribeDBInstancesRequest();
var response = c.DescribeDBInstances(request);
response.DBInstances
.ForEach(instance => {
//do stuff for each instance in region
});
}

Push NuGet package programmatically using NuGet.Core

I'm currently packaging some files and pushing them on to a NuGet feed on one of our servers using the command line tool.
Rather than using the command line tool I've set up a project using Nuget.Core and successfully managed to create a package. I'm now trying to push that package from my machine on to the NuGet feed via NuGet.Core.
Using the command line tool that looks like this (and I got this working too):
nuget.exe push package.nupkg -ApiKey MYAPIKEY -Source http://nugetpackagefeedaddress
What I want to do is replicate the push function using NuGet.Core. The closest I've managed to get so far is getting two repositories from the PackageRepositoryFactory, one for the local machine path and one for the package feed and then retrieve the package from the local one and try and add it to the feed like this:
var remoteRepo = PackageRepositoryFactory.Default.CreateRepository("myNugetPackagefeedUrl");
var localRepo = PackageRepositoryFactory.Default.CreateRepository(#"locationOfLocalPackage");
var package = localRepo.FindPackagesById("packageId").First();
remoteRepo.AddPackage(package);
This code results in a NotSupportedException stating the 'Specified method is not supported'
Is it possible to push packages using NuGet.Core? and am I anywhere close to it with the above code?
Note: I'm aware I could wrap the call to nuget.exe and call that from .NET but I'd either want to package and push from NuGet.Core or do both by wrapping the calls to nuget.exe rather than half and half
So it turns out I was looking in the wrong place entirely. The method I wanted was PushPackage on PackageServer
The code now looks like this
var localRepo = PackageRepositoryFactory.Default.CreateRepository(#"locationOfLocalPackage");
var package = localRepo.FindPackagesById("packageId").First();
var packageFile = new FileInfo(#"packagePath");
var size = packageFile .Length;
var ps = new PackageServer("http://nugetpackagefeedaddress", "userAgent");
ps.PushPackage("MYAPIKEY", package, size, 1800, false);
I'm not sure what the best values for the userAgent parameter when newing up the PackageServer would be. Similarly if anyone has any advice on what the timeout or disableBuffering parameters want to be, let me know (for example is the timeout in ms, seconds etc.)
The PushPackage method signature looks like this:
void PackageServer.PushPackage(string apiKey, IPackage package, long packageSize, int timeout, bool disableBuffering)
In addition to rh072005's answer:
Timeout is in milliseconds, be careful.
Uri is tricky. For NuGet.Server implementation PushPackage uri should be "http://nugetserveraddress" while for IPackageRepository objects Uri becomes "http://nugetserveraddress/nuget"
For large packages you will get (404) Not Found if IIS server is not configured to accept large requests.

Categories