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

检查异步方法 & 补充生成代码的命名域 #9

Merged
merged 2 commits into from
Nov 14, 2024
Merged
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
17 changes: 15 additions & 2 deletions src/AutoWasmApi.Roslyn/HttpServiceInvokerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ private static (MethodBuilder?, Diagnostic?) BuildMethod((IMethodSymbol, Attribu
.Lambda("throw new global::System.NotSupportedException()");
return (b, null);
}

// 检查当前返回类型是否是Task或Task<T>
// 如果检查类型不符合要求,说明不是异步方法
// 返回错误信息
var returnTypeInfo = methodSymbol.ReturnType;
var isTask = returnTypeInfo.Name == "Task";
var isGenericTask = returnTypeInfo.OriginalDefinition.ToDisplayString() == "System.Threading.Tasks.Task<T>";

if (!isTask && !isGenericTask)
{
return (null, DiagnosticDefinitions.WAG00005(methodSymbol.Locations.FirstOrDefault()));
}

string webMethod;
if (!methodAttribute.GetNamedValue("Method", out var v))
{
Expand Down Expand Up @@ -280,7 +293,7 @@ private static (MethodBuilder?, Diagnostic?) BuildMethod((IMethodSymbol, Attribu
{
var p = bodyParameters.First().p;
statements.Add($"var _json_gen = global::System.Text.Json.JsonSerializer.Serialize({p.Name})");
statements.Add("""_request_gen.Content = new StringContent(_json_gen, global::System.Text.Encoding.Default, "application/json")""");
statements.Add("""_request_gen.Content = new global::System.Net.Http.StringContent(_json_gen, global::System.Text.Encoding.Default, "application/json")""");
}

#endregion
Expand Down Expand Up @@ -311,7 +324,7 @@ private static (MethodBuilder?, Diagnostic?) BuildMethod((IMethodSymbol, Attribu

#endregion

statements.Add("_request_gen.RequestUri = new Uri(_url_gen, UriKind.Relative)");
statements.Add("_request_gen.RequestUri = new global::System.Uri(_url_gen, UriKind.Relative)");
var returnType = methodSymbol.ReturnType.GetGenericTypes().FirstOrDefault() ?? methodSymbol.ReturnType;

if (methodSymbol.ReturnsVoid || returnType.ToDisplayString() == "System.Threading.Tasks.Task")
Expand Down
Loading