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
By supplying Copilot with detailed project context, instructions files significantly improve the relevance and accuracy of its code suggestions. For example, if your project uses Blazor and ASP.NET Core, Copilot can generate components that follow your preferred structure, use modern C# features, and adhere to your naming conventions. This leads to more consistent code and reduces the need for manual corrections or lengthy code reviews.
263
+
Instructions files also enhance Copilot’s ability to generate meaningful tests and documentation. With the right context, Copilot can suggest unit tests using xUnit, integration tests with TestServer, and even add XML comments or OpenAPI annotations to your APIs. When refactoring or adding new features, Copilot respects your dependency injection setup, configuration patterns, and error-handling strategies, making it a smarter and more reliable assistant.
264
+
265
+
Beyond technical benefits, instructions files improve collaboration across teams. New contributors can rely on Copilot to guide them through unfamiliar codebases, ensuring they follow established practices without needing extensive onboarding. This makes instructions files a powerful tool for maintaining code quality, streamlining development workflows, and fostering team alignment.
266
+
267
+
For example, if your project uses BEM-style CSS, async APIs, and code-behind Blazor components, Copilot will automatically generate markup with the correct class naming, use async/await for API calls, and structure components according to your preferences—all without needing to be explicitly told each time. This contextual intelligence transforms Copilot from a generic code assistant into a project-aware collaborator.
268
+
269
+
Let's create our first global custom instructions file!
270
+
271
+
1. Create a `copilot-instructions.md` file in the `.github` directory:
The example below can be customized in your own project, for this example we've created an instructions file specific to our C# requirements in this project.
278
+
279
+
```md
280
+
# Project Guidelines
281
+
282
+
## Project Overview
283
+
284
+
The GitHub Codespaces ❤️ .NET project demonstrates a modern web application architecture using .NET. It consists of:
285
+
286
+
1. **BackEnd API** - A weather data API built with ASP.NET Core that provides weather forecast information
287
+
2. **FrontEnd** - A Blazor Server web application that consumes the BackEnd API and displays weather data
288
+
3. **Scalar Integration** - Interactive API documentation using Microsoft's Scalar
289
+
290
+
The application showcases best practices for building and connecting microservices, API documentation, and creating responsive web UIs with .NET technologies.
291
+
292
+
## Technology Stack
293
+
294
+
### Backend
295
+
- **.NET 9.0** - Latest .NET runtime and SDK
296
+
- **ASP.NET Core** - Web framework for building APIs
297
+
- **OpenAPI/Swagger** - API documentation and specification
298
+
- **Scalar** - Interactive API documentation and testing
299
+
300
+
### Frontend
301
+
- **Blazor Server** - Server-side .NET web framework
302
+
- **Bootstrap** - CSS framework for responsive design
303
+
- **HttpClient** - For API communication
304
+
305
+
### Development Tools
306
+
- **GitHub Codespaces** - Cloud development environment
307
+
- **Visual Studio Code** - Editor with debugging capabilities
308
+
- **Docker** - Containerization platform
309
+
- **GitHub CLI** - Command-line tool for GitHub
310
+
311
+
## Code Style Guidelines
312
+
313
+
### General
314
+
- Use consistent indentation (4 spaces)
315
+
- Follow standard C# naming conventions:
316
+
- PascalCase for classes, methods, properties
317
+
- camelCase for local variables and parameters
318
+
- _camelCase for private fields
319
+
- Keep methods small and focused on a single responsibility
320
+
- Avoid overly complex logic in a single method
321
+
322
+
### C# Specific
323
+
- Use `var` when the type is obvious from the right side of the assignment
324
+
- Prefer immutable types and properties where possible
325
+
- Use modern C# features (pattern matching, nullable reference types, etc.)
326
+
- Use async/await for asynchronous operations
327
+
- Include XML documentation comments for public APIs
328
+
329
+
### Blazor Components
330
+
- Use descriptive component names
331
+
- Separate concerns between UI and logic
332
+
- Prefer code-behind patterns for complex component logic
333
+
- Keep components small and reusable
334
+
335
+
## Key Patterns
336
+
337
+
### Dependency Injection
338
+
The application uses ASP.NET Core's built-in dependency injection container:
339
+
- Register services in `Program.cs`
340
+
- Avoid service locator pattern
341
+
- Prefer constructor injection
342
+
343
+
### Configuration
344
+
- Use `appsettings.json` for application configuration
345
+
- Access configuration via `IConfiguration` or strongly-typed settings
346
+
- Use environment-specific settings with `appsettings.{Environment}.json`
347
+
348
+
### API Communication
349
+
- Use typed HttpClient pattern for service-to-service communication
350
+
- Handle transient failures gracefully
351
+
- Implement proper error handling
352
+
353
+
### Responsive Design
354
+
- Use Bootstrap grid system for responsive layouts
355
+
- Design mobile-first
356
+
- Ensure accessibility compliance
357
+
358
+
## Testing
359
+
360
+
### Unit Testing
361
+
- Write unit tests for business logic and service classes
362
+
- Use xUnit as the testing framework
363
+
- Follow AAA pattern (Arrange, Act, Assert)
364
+
- Mock dependencies using Moq or NSubstitute
365
+
366
+
### Integration Testing
367
+
- Test API endpoints with HttpClient
368
+
- Use TestServer for in-memory integration tests
369
+
- Validate response status codes and content
370
+
371
+
### UI Testing
372
+
- Test Blazor components with bUnit
373
+
- Validate component rendering and interactivity
374
+
- Test user workflows end-to-end
375
+
376
+
### GitHub Actions
377
+
- Ensure CI pipeline validates all PRs
378
+
- Include both build and test steps
379
+
- Maintain code coverage metrics
380
+
381
+
## Deployment
382
+
383
+
### Containerization
384
+
- Use Docker for containerized deployments
385
+
- Follow multi-stage build patterns for optimized images
386
+
- Keep container images small and secure
387
+
388
+
### Cloud Hosting
389
+
- Deploy to Azure App Service or Azure Container Apps
390
+
- Use infrastructure as code for environment setup
391
+
- Implement proper logging and monitoring
392
+
```
393
+
394
+
2. You can also create specific instruction files that will be automatically applied to only specific files or directories. They must be within a `.github/instructions` directory and end in `.instructions.md`.
395
+
396
+
In the `.github` directory, create an `instructions` subdirectory. Within the subdirectory, create a `frontendstyling.instructions.md` file. We are going to use the existing requirements for the front end design of our application:
397
+
398
+
```md
399
+
---
400
+
applyTo: "SampleApp/FrontEnd/*.cs"
401
+
---
402
+
403
+
## Coding Conventions
404
+
405
+
- Use C# 12 features where appropriate
406
+
- Name components with PascalCase (e.g., `WeatherDisplay.razor`)
407
+
- Keep components small and focused on a single responsibility
408
+
- Use code-behind files (.razor.cs) for complex component logic
409
+
- Follow BEM naming convention for CSS classes
410
+
- Use CSS variables for colors, spacing, and typography
411
+
412
+
## Accessibility
413
+
414
+
- Ensure proper contrast ratios for text
415
+
- Test with screen readers
416
+
```
417
+
418
+
3. Attach your newly created instructions file to GitHub Copilot Chat in Agent Mode and reference the change in output from the previous examples.
419
+
420
+
In the above exercises we achieved the following:
421
+
- ✅ Created and applied globally scoped instruction files
422
+
- ✅ Understand how Copilot interprets and follows these rules
423
+
- ✅ Use instruction files to enforce team standards and reduce overhead
424
+
- ✅ Confidently prompt Copilot for consistent, high-quality code
425
+
426
+
427
+
**Part 2: Model Context Protocol (MCP)**
428
+
429
+
Model Context Protocol (MCP) is a universal standard that allows AI tools to integrate and interact with external services. An MCP host can connect to one or more MCP servers, which in turn provide tools that an AI tools can utilize.
430
+
431
+
GitHub Copilot Agent mode supports MCP, so you can connect to thousands of services. Microsoft provides many MCP servers, including ones for GitHub, Azure AI Foundry, and Playwright. They aren’t just for fetching data, but can perform complex operations with natural language and Copilot.
432
+
433
+
You can learn more about MCP and how to configure it using the dedicated [GitHub Skills course Integration MCP with GitHub Copilot](https://github.com/skills/integrate-mcp-with-copilot).
259
434
260
435
### Useful Links and Further Learning
261
436
- [Use agent mode in VS Code](https://code.visualstudio.com/docs/copilot/chat/chat-agent-mode)
0 commit comments