Adding control plane + data plane EH sample
This commit is contained in:
Родитель
742d8d8a7e
Коммит
037168ff6d
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
|
||||
</startup>
|
||||
<appSettings>
|
||||
<add key="tenantId" value="" />
|
||||
<add key="clientId" value="" />
|
||||
<add key="clientSecret" value="" />
|
||||
<add key="eventHubNamespaceFQDN" value="" />
|
||||
</appSettings>
|
||||
</configuration>
|
|
@ -0,0 +1,117 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{1485E9E2-11C3-4601-929D-04618277EB9D}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>ControlAndDataPlane</RootNamespace>
|
||||
<AssemblyName>ControlAndDataPlane</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
<TargetFrameworkProfile />
|
||||
<PublishUrl>publish\</PublishUrl>
|
||||
<Install>true</Install>
|
||||
<InstallFrom>Disk</InstallFrom>
|
||||
<UpdateEnabled>false</UpdateEnabled>
|
||||
<UpdateMode>Foreground</UpdateMode>
|
||||
<UpdateInterval>7</UpdateInterval>
|
||||
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
|
||||
<UpdatePeriodically>false</UpdatePeriodically>
|
||||
<UpdateRequired>false</UpdateRequired>
|
||||
<MapFileExtensions>true</MapFileExtensions>
|
||||
<ApplicationRevision>0</ApplicationRevision>
|
||||
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
|
||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||
<UseApplicationTrust>false</UseApplicationTrust>
|
||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Identity.Client, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\..\..\CxCache\Microsoft.Identity.Client.4.0.0\lib\net45\Microsoft.Identity.Client.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.IdentityModel.JsonWebTokens, Version=5.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\..\..\CxCache\Microsoft.IdentityModel.JsonWebTokens.5.5.0\lib\net461\Microsoft.IdentityModel.JsonWebTokens.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.IdentityModel.Logging, Version=5.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\..\..\CxCache\Microsoft.IdentityModel.Logging.5.5.0\lib\net461\Microsoft.IdentityModel.Logging.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.IdentityModel.Tokens, Version=5.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\..\..\CxCache\Microsoft.IdentityModel.Tokens.5.5.0\lib\net461\Microsoft.IdentityModel.Tokens.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.ServiceBus">
|
||||
<HintPath>..\..\..\..\..\ServiceBus\Messaging\out\debug-amd64\AppServer\Microsoft.ServiceBus.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\..\..\CxCache\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.IdentityModel" />
|
||||
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=4.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\..\..\CxCache\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Web.Extensions" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
<None Include="nuget.config">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<BootstrapperPackage Include=".NETFramework,Version=v4.7.2">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>Microsoft .NET Framework 4.7.2 %28x86 and x64%29</ProductName>
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>.NET Framework 3.5 SP1</ProductName>
|
||||
<Install>false</Install>
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 14
|
||||
VisualStudioVersion = 14.0.25420.1
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ControlAndDataPlane", "ControlAndDataPlane.csproj", "{1485E9E2-11C3-4601-929D-04618277EB9D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{1485E9E2-11C3-4601-929D-04618277EB9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{1485E9E2-11C3-4601-929D-04618277EB9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1485E9E2-11C3-4601-929D-04618277EB9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1485E9E2-11C3-4601-929D-04618277EB9D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
|
@ -0,0 +1,105 @@
|
|||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
namespace ControlAndDataPlane
|
||||
{
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Identity.Client;
|
||||
using Microsoft.ServiceBus;
|
||||
using Microsoft.ServiceBus.Messaging;
|
||||
|
||||
class Program
|
||||
{
|
||||
static readonly string TenantId = ConfigurationManager.AppSettings["tenantId"];
|
||||
static readonly string ClientId = ConfigurationManager.AppSettings["clientId"];
|
||||
static readonly string ClientSecret = ConfigurationManager.AppSettings["clientSecret"];
|
||||
static readonly string EventHubNamespace = ConfigurationManager.AppSettings["eventHubNamespaceFQDN"];
|
||||
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
// Create token provider so that we can use it at both management and runtime clients.
|
||||
TokenProvider tokenProvider = TokenProvider.CreateAzureActiveDirectoryTokenProvider(
|
||||
async (audience, authority, state) =>
|
||||
{
|
||||
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(ClientId)
|
||||
.WithAuthority(authority)
|
||||
.WithClientSecret(ClientSecret)
|
||||
.Build();
|
||||
|
||||
var authResult = await app.AcquireTokenForClient(new string[] { $"{audience}/.default" }).ExecuteAsync();
|
||||
return authResult.AccessToken;
|
||||
},
|
||||
ServiceAudience.EventHubsAudience,
|
||||
$"https://login.microsoftonline.com/{TenantId}");
|
||||
|
||||
var eventHubName = "testeh-" + Guid.NewGuid().ToString();
|
||||
|
||||
// Create NamespaceManger and EventHubClient with Azure Active Directory token provider.
|
||||
var ehUri = new Uri($"sb://{EventHubNamespace}/");
|
||||
var namespaceManager = new NamespaceManager(ehUri, tokenProvider);
|
||||
var messagingFactory = MessagingFactory.Create(ehUri,
|
||||
new MessagingFactorySettings()
|
||||
{
|
||||
TokenProvider = tokenProvider,
|
||||
TransportType = TransportType.Amqp
|
||||
});
|
||||
var ehClient = messagingFactory.CreateEventHubClient(eventHubName);
|
||||
|
||||
// Create a new event hub.
|
||||
Console.WriteLine($"Creating event hub {eventHubName}");
|
||||
await namespaceManager.CreateEventHubAsync(eventHubName);
|
||||
|
||||
// Send and receive a message.
|
||||
await SendReceiveAsync(ehClient);
|
||||
|
||||
// Delete event hub.
|
||||
Console.WriteLine($"Deleting event hub {eventHubName}");
|
||||
await namespaceManager.DeleteEventHubAsync(eventHubName);
|
||||
|
||||
Console.WriteLine("Press enter to exit");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
static async Task SendReceiveAsync(EventHubClient ehClient)
|
||||
{
|
||||
Console.WriteLine("Fetching eventhub description to discover partitions");
|
||||
var ehDesc = await ehClient.GetRuntimeInformationAsync();
|
||||
Console.WriteLine($"Discovered partitions as {string.Join(",", ehDesc.PartitionIds)}");
|
||||
|
||||
var defaultConsumerGroup = ehClient.GetDefaultConsumerGroup();
|
||||
var receiveTasks = ehDesc.PartitionIds.Select(async partitionId =>
|
||||
{
|
||||
Console.WriteLine($"Initiating receiver on partition {partitionId}");
|
||||
var receiver = await defaultConsumerGroup.CreateReceiverAsync(partitionId, EventPosition.FromStart());
|
||||
|
||||
while(true)
|
||||
{
|
||||
var events = await receiver.ReceiveAsync(1, TimeSpan.FromSeconds(15));
|
||||
if (events == null || events.Count() == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Received from partition {partitionId} with message content '"
|
||||
+ Encoding.UTF8.GetString(events.First().GetBytes()) + "'");
|
||||
}
|
||||
|
||||
await receiver.CloseAsync();
|
||||
}).ToList<Task>();
|
||||
|
||||
await Task.Delay(5000);
|
||||
|
||||
Console.WriteLine("Sending single event");
|
||||
await ehClient.SendAsync(new EventData(Encoding.UTF8.GetBytes("Hello!")));
|
||||
Console.WriteLine("Send done");
|
||||
|
||||
Console.WriteLine("Waiting for receivers to complete");
|
||||
await Task.WhenAll(receiveTasks);
|
||||
Console.WriteLine("All receivers completed");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("ControlAndDataPlane")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("ControlAndDataPlane")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2019")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("5c908c55-400f-4041-99ff-eb2ead0ae8d3")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
@ -0,0 +1,3 @@
|
|||
# Role based access sample with WindowsAzure.ServiceBus SDK #
|
||||
|
||||
For more information on Role based access (RBAC) and how to run this sample follow [this](https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-role-based-access-control) link.
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<config>
|
||||
<add key="repositoryPath" value="packages" />
|
||||
</config>
|
||||
</configuration>
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Microsoft.Identity.Client" version="4.0.0" targetFramework="net472" />
|
||||
<package id="Microsoft.IdentityModel.JsonWebTokens" version="5.5.0" targetFramework="net472" />
|
||||
<package id="Microsoft.IdentityModel.Logging" version="5.5.0" targetFramework="net472" />
|
||||
<package id="Microsoft.IdentityModel.Tokens" version="5.5.0" targetFramework="net472" />
|
||||
<package id="Newtonsoft.Json" version="10.0.1" targetFramework="net472" />
|
||||
<package id="System.IdentityModel.Tokens.Jwt" version="4.0.4.403061554" targetFramework="net472" />
|
||||
</packages>
|
Загрузка…
Ссылка в новой задаче