You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The easiest way to convert an application using C# script to a C# project is to start with a new project and migrate the code and configuration from your .csx and function.json files.
587
+
588
+
If you are using C# scripting for portal editing, you may wish to start by [downloading the app content to your local machine](./deployment-zip-push.md#download-your-function-app-files). Choose the "Site content" option instead of "Content and Visual Studio project". The project that the portal provides isn't needed because in the later steps of this section, you will be creating a new Visual Studio project. Similarly, do not include app settings in the download. You are defining a new development environment, and this environment should not have the same permissions as your hosted app environment.
589
+
590
+
Once you have your C# script code ready, you can begin creating the new project:
591
+
592
+
1. Follow the instructions to create a new function project [using Visual Studio](./functions-create-your-first-function-visual-studio.md), using [Visual Studio Code](./create-first-function-vs-code-csharp.md), or [using the command line](./create-first-function-cli-csharp.md). You don't need to publish the project yet.
593
+
1. If your C# script code included an `extensions.csproj` file or any `function.proj` files, copy the package references from these files, and add them to the new project's `.csproj` file alongside it's core dependencies.
594
+
595
+
The conversion activity a good opportunity to update to the latest versions of your dependencies. Doing so may require additional code changes in a later step.
596
+
597
+
1. Copy the contents of the C# scripting `host.json` file into the project `host.json` file. If you are combining this with any other migration activities, note that the [`host.json`](./functions-host-json.md) schema depends on the version you are targeting. The contents of the `extensions` section are also informed by the versions of triggers and bindings that you are using. Refer to the reference for each extension to identify the right properties to configure.
598
+
1. For any [shared files referenced by a `#load` directive](#reusing-csx-code), create new `.cs` files for their contents. You can structure this in any way you prefer. For most apps, it is simplest to create a new `.cs` file for each class that you defined. For any static methods created without a class, you'll need to define a new class or classes that they can be defined in.
599
+
1. Migrate each function to a `.cs` file. This file will combine the `run.csx` and the `function.json` for that function. For example, if you had a function named `HelloWorld`, in C# script this would be represented with `HelloWorld/run.csx` and `HelloWorld/function.json`. For the new project, you would create a `HelloWorld.cs`. Perform the following steps for each function:
600
+
601
+
1. Create a new file named `<FUNCTION_NAME>.cs`, replacing `<FUNCTION_NAME>` with the name of the folder that defined your C# script function. It is often easiest to start by creating a new function in the project model, which will cover some of the later steps. From the CLI, you can use the command `func new --name <FUNCTION_NAME>` making a similar substitution, and selecting the target template when prompted.
602
+
1. Copy the `using` statements from your `run.csx` file and add them to the new file. You do not need any `#r` directives.
603
+
1. For any `#load` statement in your `run.csx` file, add a new `using` statement for the namespace you used for the shared code.
604
+
1. In the new file, define a class for your function under the namespace you are using for the project.
605
+
1. Create a new method named `RunHandler` or something similar. This new method will serve as the new entry point for the function.
606
+
1. Copy the static method that represents your function, along with any functions it calls, from `run.csx` into your new class as a second method. From the new method you created in the previous step, call into this static method. This indirection step is helpful for navigating any differences as you continue the upgrade. You can keep the original method exactly the same and simply control its inputs from the new context. You may need to create parameters on the new method which you then pass into the static method call. After you have confirmed that the migration has worked as intended, you can remove this extra level of indirection.
607
+
1. For each binding in the `function.json` file, add the corresponding attribute to your new method. This may require you to add additional package dependencies. Consult the reference for each binding for specific requirements in the new model.
608
+
609
+
1. Verify that your project runs locally.
610
+
1. Republish the app to Azure.
611
+
612
+
### Example function conversion
613
+
614
+
This section shows an example of the migration for a single function.
615
+
616
+
The original function in C# scripting has two files:
Copy file name to clipboardExpand all lines: articles/azure-functions/migrate-dotnet-to-isolated-model.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,7 @@ To upgrade the application, you will:
65
65
66
66
## Upgrade your local project
67
67
68
-
The section outlines the various changes that you need to make to your local project to move it to the isolated worker model. Some of the steps change based on your target version of .NET. Use the tabs to select the instructions which match your desired version.
68
+
The section outlines the various changes that you need to make to your local project to move it to the isolated worker model. Some of the steps change based on your target version of .NET. Use the tabs to select the instructions which match your desired version. These steps assume a local C# project, and if your app is instead using C# script (`.csx` files), you should [convert to the project model](./functions-reference-csharp.md#convert-a-c-script-app-to-a-c-project) before continuing.
69
69
70
70
> [!TIP]
71
71
> The [.NET Upgrade Assistant] can be used to automatically make many of the changes mentioned in the following sections.
Copy file name to clipboardExpand all lines: includes/functions-dotnet-migrate-project-v4-isolated-2.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,9 @@ ms.topic: include
5
5
ms.date: 10/29/2022
6
6
ms.author: glenga
7
7
---
8
+
9
+
These steps assume a local C# project, and if your app is instead using C# script (`.csx` files), you should [convert to the project model](../articles/azure-functions/functions-reference-csharp.md#convert-a-c-script-app-to-a-c-project) before continuing.
10
+
8
11
The following changes are required in the .csproj XML project file:
9
12
10
13
1. Set the value of `PropertyGroup`.`TargetFramework` to `net7.0`.
Copy file name to clipboardExpand all lines: includes/functions-dotnet-migrate-project-v4-isolated-net-framework.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,9 @@ ms.topic: include
5
5
ms.date: 07/31/2023
6
6
ms.author: mahender
7
7
---
8
+
9
+
These steps assume a local C# project, and if your app is instead using C# script (`.csx` files), you should [convert to the project model](../articles/azure-functions/functions-reference-csharp.md#convert-a-c-script-app-to-a-c-project) before continuing.
10
+
8
11
The following changes are required in the .csproj XML project file:
9
12
10
13
1. Set the value of `PropertyGroup`.`TargetFramework` to `net48`.
Copy file name to clipboardExpand all lines: includes/functions-dotnet-migrate-project-v4-isolated.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,9 @@ ms.topic: include
5
5
ms.date: 10/29/2022
6
6
ms.author: glenga
7
7
---
8
+
9
+
These steps assume a local C# project, and if your app is instead using C# script (`.csx` files), you should [convert to the project model](../articles/azure-functions/functions-reference-csharp.md#convert-a-c-script-app-to-a-c-project) before continuing.
10
+
8
11
The following changes are required in the .csproj XML project file:
9
12
10
13
1. Set the value of `PropertyGroup`.`TargetFramework` to `net6.0`.
0 commit comments