Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant project config, add file-scoped namespaces #1611

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@
<!--#endif -->
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="FunctionsSdkPackageVersionValue" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<!--#if (NetCore)-->
<ItemGroup>
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext"/>
</ItemGroup>
<!--#endif -->
<!--#if (NetFramework)-->
<ItemGroup>
<Folder Include="Properties\" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can also be removed.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
namespace Company.Function;

public class BlobTriggerCSharp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also use primary constructors.

Suggested change
public class BlobTriggerCSharp
public class BlobTriggerCSharp(ILogger<BlobTriggerCSharp> logger)

Then don't even have the _logger field. You can access the ctor parameter anywhere in this class via logger.

{
public class BlobTriggerCSharp
{
private readonly ILogger<BlobTriggerCSharp> _logger;
private readonly ILogger<BlobTriggerCSharp> _logger;

public BlobTriggerCSharp(ILogger<BlobTriggerCSharp> logger)
{
_logger = logger;
}
public BlobTriggerCSharp(ILogger<BlobTriggerCSharp> logger)
{
_logger = logger;
}

[Function(nameof(BlobTriggerCSharp))]
public async Task Run([BlobTrigger("PathValue/{name}", Connection = "ConnectionValue")] Stream stream, string name)
{
using var blobStreamReader = new StreamReader(stream);
var content = await blobStreamReader.ReadToEndAsync();
_logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");
}
[Function(nameof(BlobTriggerCSharp))]
public async Task Run([BlobTrigger("PathValue/{name}", Connection = "ConnectionValue")] Stream stream, string name)
{
using var blobStreamReader = new StreamReader(stream);
var content = await blobStreamReader.ReadToEndAsync();
_logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,40 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
namespace Company.Function;

public class CosmosDBTriggerCSharp
{
public class CosmosDBTriggerCSharp
{
private readonly ILogger _logger;
private readonly ILogger _logger;

public CosmosDBTriggerCSharp(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<CosmosDBTriggerCSharp>();
}
public CosmosDBTriggerCSharp(ILoggerFactory loggerFactory)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend primary constructor and also switching to ILogger<CosmosDBTriggerCSharp>

{
_logger = loggerFactory.CreateLogger<CosmosDBTriggerCSharp>();
}

[Function("CosmosDBTriggerCSharp")]
public void Run([CosmosDBTrigger(
databaseName: "DatabaseValue",
containerName: "ContainerValue",
Connection = "ConnectionValue",
LeaseContainerName = "leases",
CreateLeaseContainerIfNotExists = true)] IReadOnlyList<MyDocument> input)
[Function("CosmosDBTriggerCSharp")]
public void Run([CosmosDBTrigger(
databaseName: "DatabaseValue",
containerName: "ContainerValue",
Connection = "ConnectionValue",
LeaseContainerName = "leases",
CreateLeaseContainerIfNotExists = true)] IReadOnlyList<MyDocument> input)
{
if (input != null && input.Count > 0)
{
if (input != null && input.Count > 0)
{
_logger.LogInformation("Documents modified: " + input.Count);
_logger.LogInformation("First document Id: " + input[0].id);
}
_logger.LogInformation("Documents modified: " + input.Count);
_logger.LogInformation("First document Id: " + input[0].id);
}
}
}

public class MyDocument
{
public string id { get; set; }
public class MyDocument
{
public string id { get; set; }

public string Text { get; set; }
public string Text { get; set; }

public int Number { get; set; }
public int Number { get; set; }

public bool Boolean { get; set; }
}
}
public bool Boolean { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,51 @@
using Microsoft.DurableTask.Client;
using Microsoft.Extensions.Logging;

namespace Company.Function
namespace Company.Function;

public static class DurableFunctionsOrchestrationCSharp
{
public static class DurableFunctionsOrchestrationCSharp
[Function(nameof(DurableFunctionsOrchestrationCSharp))]
public static async Task<List<string>> RunOrchestrator(
[OrchestrationTrigger] TaskOrchestrationContext context)
{
ILogger logger = context.CreateReplaySafeLogger(nameof(DurableFunctionsOrchestrationCSharp));
logger.LogInformation("Saying hello.");
var outputs = new List<string>();

// Replace name and input with values relevant for your Durable Functions Activity
outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "Tokyo"));
outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "Seattle"));
outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "London"));

// returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
return outputs;
}

[Function(nameof(SayHello))]
public static string SayHello([ActivityTrigger] string name, FunctionContext executionContext)
{
[Function(nameof(DurableFunctionsOrchestrationCSharp))]
public static async Task<List<string>> RunOrchestrator(
[OrchestrationTrigger] TaskOrchestrationContext context)
{
ILogger logger = context.CreateReplaySafeLogger(nameof(DurableFunctionsOrchestrationCSharp));
logger.LogInformation("Saying hello.");
var outputs = new List<string>();

// Replace name and input with values relevant for your Durable Functions Activity
outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "Tokyo"));
outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "Seattle"));
outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "London"));

// returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
return outputs;
}

[Function(nameof(SayHello))]
public static string SayHello([ActivityTrigger] string name, FunctionContext executionContext)
{
ILogger logger = executionContext.GetLogger("SayHello");
logger.LogInformation("Saying hello to {name}.", name);
return $"Hello {name}!";
}

[Function("DurableFunctionsOrchestrationCSharp_HttpStart")]
public static async Task<HttpResponseData> HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
[DurableClient] DurableTaskClient client,
FunctionContext executionContext)
{
ILogger logger = executionContext.GetLogger("DurableFunctionsOrchestrationCSharp_HttpStart");

// Function input comes from the request content.
string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(
nameof(DurableFunctionsOrchestrationCSharp));

logger.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);

// Returns an HTTP 202 response with an instance management payload.
// See https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-http-api#start-orchestration
return await client.CreateCheckStatusResponseAsync(req, instanceId);
}
ILogger logger = executionContext.GetLogger("SayHello");
logger.LogInformation("Saying hello to {name}.", name);
return $"Hello {name}!";
}

[Function("DurableFunctionsOrchestrationCSharp_HttpStart")]
public static async Task<HttpResponseData> HttpStart(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
[DurableClient] DurableTaskClient client,
FunctionContext executionContext)
{
ILogger logger = executionContext.GetLogger("DurableFunctionsOrchestrationCSharp_HttpStart");

// Function input comes from the request content.
string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(
nameof(DurableFunctionsOrchestrationCSharp));

logger.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);

// Returns an HTTP 202 response with an instance management payload.
// See https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-http-api#start-orchestration
return await client.CreateCheckStatusResponseAsync(req, instanceId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
namespace Company.Function;

public class EventGridBlobTriggerCSharp
{
public class EventGridBlobTriggerCSharp
{
private readonly ILogger<EventGridBlobTriggerCSharp> _logger;
private readonly ILogger<EventGridBlobTriggerCSharp> _logger;

public EventGridBlobTriggerCSharp(ILogger<EventGridBlobTriggerCSharp> logger)
{
_logger = logger;
}
public EventGridBlobTriggerCSharp(ILogger<EventGridBlobTriggerCSharp> logger)
{
_logger = logger;
}

[Function(nameof(EventGridBlobTriggerCSharp))]
public async Task Run([BlobTrigger("PathValue/{name}", Source = BlobTriggerSource.EventGrid, Connection = "ConnectionValue")] Stream stream, string name)
{
using var blobStreamReader = new StreamReader(stream);
var content = await blobStreamReader.ReadToEndAsync();
_logger.LogInformation($"C# Blob Trigger (using Event Grid) processed blob\n Name: {name} \n Data: {content}");
}
[Function(nameof(EventGridBlobTriggerCSharp))]
public async Task Run([BlobTrigger("PathValue/{name}", Source = BlobTriggerSource.EventGrid, Connection = "ConnectionValue")] Stream stream, string name)
{
using var blobStreamReader = new StreamReader(stream);
var content = await blobStreamReader.ReadToEndAsync();
_logger.LogInformation($"C# Blob Trigger (using Event Grid) processed blob\n Name: {name} \n Data: {content}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably not encourage bad logging practices in our samples (but not sure if that is in the scope of this PR)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
namespace Company.Function;

public class EventGridTriggerCSharp
{
public class EventGridTriggerCSharp
{
private readonly ILogger<EventGridTriggerCSharp> _logger;
private readonly ILogger<EventGridTriggerCSharp> _logger;

public EventGridTriggerCSharp(ILogger<EventGridTriggerCSharp> logger)
{
_logger = logger;
}
public EventGridTriggerCSharp(ILogger<EventGridTriggerCSharp> logger)
{
_logger = logger;
}

[Function(nameof(EventGridTriggerCSharp))]
public void Run([EventGridTrigger] CloudEvent cloudEvent)
{
_logger.LogInformation("Event type: {type}, Event subject: {subject}", cloudEvent.Type, cloudEvent.Subject);
}
[Function(nameof(EventGridTriggerCSharp))]
public void Run([EventGridTrigger] CloudEvent cloudEvent)
{
_logger.LogInformation("Event type: {type}, Event subject: {subject}", cloudEvent.Type, cloudEvent.Subject);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
namespace Company.Function;
public class EventHubTriggerCSharp
{
public class EventHubTriggerCSharp
{
private readonly ILogger<EventHubTriggerCSharp> _logger;
private readonly ILogger<EventHubTriggerCSharp> _logger;

public EventHubTriggerCSharp(ILogger<EventHubTriggerCSharp> logger)
{
_logger = logger;
}
public EventHubTriggerCSharp(ILogger<EventHubTriggerCSharp> logger)
{
_logger = logger;
}

[Function(nameof(EventHubTriggerCSharp))]
public void Run([EventHubTrigger("eventHubNameValue", Connection = "ConnectionValue")] EventData[] events)
[Function(nameof(EventHubTriggerCSharp))]
public void Run([EventHubTrigger("eventHubNameValue", Connection = "ConnectionValue")] EventData[] events)
{
foreach (EventData @event in events)
{
foreach (EventData @event in events)
{
_logger.LogInformation("Event Body: {body}", @event.Body);
_logger.LogInformation("Event Content-Type: {contentType}", @event.ContentType);
}
_logger.LogInformation("Event Body: {body}", @event.Body);
_logger.LogInformation("Event Content-Type: {contentType}", @event.ContentType);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@
],
"defaultName": "HttpTriggerCSharp",
"postActions": [
{
"Description": "Adding Reference to Microsoft.Azure.Functions.Worker.Extensions.Http Nuget package",
"ActionId": "B17581D1-C5C9-4489-8F0A-004BE667B814",
"ContinueOnError": "true",
"ManualInstructions": [],
"args": {
"referenceType": "package",
"reference": "Microsoft.Azure.Functions.Worker.Extensions.Http",
"version": "3.2.0",
"projectFileExtensions": ".csproj"
}
},
{
"Description": "Adding Reference to Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore Nuget package",
"ActionId": "B17581D1-C5C9-4489-8F0A-004BE667B814",
Expand Down
Loading