How can I execute a plugin from a test method? - c#

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.

Related

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

how to check service bus topic empty or not 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.)

Can a non-SF project make use of the applicationmanifest configuration values?

I've got a Service Fabric Application set up in the following way:
Solution
--SF Project
--ApplicationManifest.xml
--Stateless Project (uses app manifest values)
--Stateless Project (uses app manifest values)
--Class Library (used as a repository by the above two projects)
How can I enable the class library to make use of the ApplicationManifest.xml configuration file from the SF Project?
To allow the projects to be able to use the AppManifest for build/deployment, they simply need to be created like so:
How does a project that does not get added as a Service Fabric project make use of the applicationmanifest?
The Service Fabric projects are able to use the appmanifest by including parameters in settings.xml and servicemanifest (but non-SF projects cannot):
Option #1
If you need to access the parameters defined in your service's setting.xml, the next should work -
In your non-SF project, install
Microsoft.Extensions.Configuration and ServiceFabric.Extensions.Configuration NuGet packages
Wherever you decide to access the parameters, use the next code snippet -
var builder = new ConfigurationBuilder().AddFabricConfiguration("Config");
var configuration = builder.Build();
var section = configuration.GetSection("MyConfigSection");
var parameterValue = section["MyParameter"];
One note though - you will get access to only one SF service at a time. That's because AddFabricConfiguration() works by calling FabricRuntime.GetActivationContext(), which ties settings being loaded with the SF service you're calling a non-SF code from.
Option #2
Next option will work from any place where you could establish connection with the SF. Using code below, you could read any parameter passed into the app manifest -
var fClient = new FabricClient();
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("ns", "http://schemas.microsoft.com/2011/01/fabric");
var manifest = XDocument.Parse(fClient.ApplicationManager.GetApplicationManifestAsync("YOUR_APP_TYPE_NAME", "YOUR_APP_TYPE_VERSION").Result);
var parameterValue = manifest.XPathSelectElement("/ns:ApplicationManifest/ns:Parameters/ns:Parameter[#Name='PARAMETER_NAME']", namespaceManager).Attribute("DefaultValue").Value;

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.

How do I connect to my Service Fabric cluster from C#?

I'm trying to connect to an on-prem Service Fabric cluster from C# code to manage some services:
using System.Fabric;
...
var fabricClient = new FabricClient();
var services = await fabricClient.QueryManager.GetServiceListAsync(new Uri("fabric:/TestConsumer"));
var service = services.FirstOrDefault(e => e.ServiceName.AbsolutePath.Contains("TestManagedConsumer"));
..
(I found the above example code here.)
The problem is that I don't actually know how to connect to the cluster. The above code throws this exception:
System.Fabric.FabricElementNotFoundException: 'Application not found'
Where/how do I specify where my cluster is running? Furthermore do I need some method authentication? If I simply navigate to http://host:19080 I'm able to connect without logging in.
I'm pretty new to Service Fabric, but I've done some digging and I am not turning up much. There seems to be little to no example code out there for this type of thing. Any suggestions?
I feel pretty dumb having found what I was looking for about 5 minutes after posting this question. Doing a search for "new FabricClient" in google turned up some examples, including this page: https://github.com/Microsoft/azure-docs/blob/master/articles/service-fabric/service-fabric-connect-to-secure-cluster.md, which shows the following example:
To connect to a remote unsecured cluster, create a FabricClient instance and provide the cluster address:
FabricClient fabricClient = new FabricClient("clustername.westus.cloudapp.azure.com:19000");
I was able to connect to my cluster with this code.
There is also some good example code here: https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-deploy-remove-applications-fabricclient

Categories