接入grpc
This commit is contained in:
parent
0a94ffd5f0
commit
c86f9a4804
5
.gitignore
vendored
5
.gitignore
vendored
@ -2,3 +2,8 @@
|
||||
WinUINotes/bin/
|
||||
WinUINotes/obj/
|
||||
WinUINotes/.vshistory/
|
||||
WinUINotes.sln.DotSettings.user
|
||||
WinUINotes.Grpc/.vshistory/
|
||||
WinUINotes.Grpc/bin/
|
||||
WinUINotes.Grpc/obj/
|
||||
WinUINotes.Grpc/Protos/.vshistory/
|
||||
|
||||
21
WinUINotes.Grpc/Protos/greet.proto
Normal file
21
WinUINotes.Grpc/Protos/greet.proto
Normal file
@ -0,0 +1,21 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option csharp_namespace = "WinUINotes";
|
||||
|
||||
package greet;
|
||||
|
||||
// The greeting service definition.
|
||||
service Greeter {
|
||||
// Sends a greeting
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
// The request message containing the user's name.
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
// The response message containing the greetings.
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
35
WinUINotes.Grpc/Protos/my_diary.proto
Normal file
35
WinUINotes.Grpc/Protos/my_diary.proto
Normal file
@ -0,0 +1,35 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option csharp_namespace = "ZR.Admin.Grpc";
|
||||
|
||||
package my.diary;
|
||||
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
// The greeting service definition.
|
||||
service MyDiary {
|
||||
// Sends a greeting
|
||||
rpc Edit (EditRequest) returns (EditReply);
|
||||
rpc Query (QueryRequest) returns (QueryReply);
|
||||
}
|
||||
|
||||
// The request message containing the user's name.
|
||||
message EditRequest {
|
||||
string content = 1;
|
||||
int64 user_id = 2;
|
||||
google.protobuf.Timestamp create_time = 3; // 字段名仍然建议使用 snake_case
|
||||
}
|
||||
|
||||
// The response message containing the greetings.
|
||||
message EditReply {
|
||||
string result_message = 1;
|
||||
}
|
||||
|
||||
message QueryRequest {
|
||||
int64 user_id = 1;
|
||||
google.protobuf.Timestamp create_time = 2;
|
||||
}
|
||||
|
||||
message QueryReply {
|
||||
string content = 1;
|
||||
}
|
||||
24
WinUINotes.Grpc/WinUINotes.Grpc.csproj
Normal file
24
WinUINotes.Grpc/WinUINotes.Grpc.csproj
Normal file
@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
|
||||
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
|
||||
<RootNamespace>WinUINotes.Grpc</RootNamespace>
|
||||
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
|
||||
<UseWinUI>true</UseWinUI>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Google.Protobuf" Version="3.31.1" />
|
||||
<PackageReference Include="Grpc.Net.Client" Version="2.71.0" />
|
||||
<PackageReference Include="Grpc.Tools" Version="2.72.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.4188" />
|
||||
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250606001" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
|
||||
<Protobuf Include="Protos\my_diary.proto" GrpcServices="Client" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@ -1,10 +1,12 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.14.36203.30 d17.14
|
||||
VisualStudioVersion = 17.14.36203.30
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinUINotes", "WinUINotes\WinUINotes.csproj", "{5F69CDC8-B184-45BA-9113-311FB706CF79}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinUINotes.Grpc", "WinUINotes.Grpc\WinUINotes.Grpc.csproj", "{A42CC243-ED38-4744-B1B5-145F6641AF44}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|ARM64 = Debug|ARM64
|
||||
@ -33,6 +35,18 @@ Global
|
||||
{5F69CDC8-B184-45BA-9113-311FB706CF79}.Release|x86.ActiveCfg = Release|x86
|
||||
{5F69CDC8-B184-45BA-9113-311FB706CF79}.Release|x86.Build.0 = Release|x86
|
||||
{5F69CDC8-B184-45BA-9113-311FB706CF79}.Release|x86.Deploy.0 = Release|x86
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|ARM64.ActiveCfg = Debug|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|ARM64.Build.0 = Debug|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|ARM64.ActiveCfg = Release|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|ARM64.Build.0 = Release|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|x64.Build.0 = Release|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{A42CC243-ED38-4744-B1B5-145F6641AF44}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@ -1,18 +1,10 @@
|
||||
using Grpc.Net.Client;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Controls.Primitives;
|
||||
using Microsoft.UI.Xaml.Data;
|
||||
using Microsoft.UI.Xaml.Input;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Microsoft.UI.Xaml.Navigation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Windows.Foundation;
|
||||
using Windows.Foundation.Collections;
|
||||
using Windows.Storage;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using ZR.Admin.Grpc;
|
||||
|
||||
// To learn more about WinUI, the WinUI project structure,
|
||||
// and more about our project templates, see: http://aka.ms/winui-project-info.
|
||||
@ -24,6 +16,19 @@ namespace WinUINotes
|
||||
/// </summary>
|
||||
public sealed partial class NotePage : Page
|
||||
{
|
||||
private Greeter.GreeterClient _greeterClient;
|
||||
private MyDiary.MyDiaryClient _myDiaryClient;
|
||||
|
||||
private void InitializeGrpcClient()
|
||||
{
|
||||
//AppContext.SetSwitch(
|
||||
// "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||
// 确保你的 gRPC 服务器正在运行在指定的地址和端口
|
||||
var channel = GrpcChannel.ForAddress("https://localhost:8888"); // 或者你的服务器地址
|
||||
_greeterClient = new Greeter.GreeterClient(channel);
|
||||
_myDiaryClient = new MyDiary.MyDiaryClient(channel);
|
||||
}
|
||||
|
||||
private StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
|
||||
private StorageFile? noteFile = null;
|
||||
private string fileName = "note.txt";
|
||||
@ -33,14 +38,26 @@ namespace WinUINotes
|
||||
InitializeComponent();
|
||||
|
||||
Loaded += NotePage_Loaded;
|
||||
|
||||
InitializeGrpcClient();
|
||||
}
|
||||
|
||||
private async void NotePage_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
noteFile = (StorageFile)await storageFolder.TryGetItemAsync(fileName);
|
||||
if (noteFile is not null)
|
||||
//noteFile = (StorageFile)await storageFolder.TryGetItemAsync(fileName);
|
||||
//if (noteFile is not null)
|
||||
//{
|
||||
// NoteEditor.Text = await FileIO.ReadTextAsync(noteFile);
|
||||
//}
|
||||
|
||||
var reply = await _myDiaryClient.QueryAsync(new QueryRequest
|
||||
{
|
||||
NoteEditor.Text = await FileIO.ReadTextAsync(noteFile);
|
||||
UserId = 1L,
|
||||
CreateTime = Timestamp.FromDateTime(DateTime.UtcNow)
|
||||
});
|
||||
if (reply != null)
|
||||
{
|
||||
NoteEditor.Text = reply.Content;
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,7 +67,22 @@ namespace WinUINotes
|
||||
{
|
||||
noteFile = await storageFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
|
||||
}
|
||||
await FileIO.WriteTextAsync(noteFile, NoteEditor.Text);
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var request = new EditRequest
|
||||
{
|
||||
Content = NoteEditor.Text,
|
||||
UserId = 1L,
|
||||
CreateTime = Timestamp.FromDateTime(DateTime.UtcNow)
|
||||
};
|
||||
var reply = await _myDiaryClient.EditAsync(request);
|
||||
await FileIO.WriteTextAsync(noteFile, request.Content);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private async void DeleteButton_Click(object sender, RoutedEventArgs e)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
|
||||
@ -42,13 +42,16 @@
|
||||
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.4188" />
|
||||
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250606001" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\WinUINotes.Grpc\WinUINotes.Grpc.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Page Update="NotePage.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
|
||||
<!--
|
||||
<!--
|
||||
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
|
||||
Explorer "Package and Publish" context menu entry to be enabled for this project even if
|
||||
the Windows App SDK Nuget package has not yet been restored.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user