Comments (9)
Hi igalHcsra,
Thank you for request. I put your topic in my todo list.
What I can see right away is a possible limitation to only unary methods and POST. In any case requires some research.
from servicemodel.grpc.
Hi igalHcsra,
I have no plans to integrate ServiceModel.Grpc with Microsoft.AspNetCore.Grpc.HttpApi: HttpApi is in alpha state.
I like the idea about swagger support, there for ServiceModel.Grpc.AspNetCore.Swashbuckle is released in version 1.2.0.
Integration is in a pre-release state.
Please check this example SwashbuckleSwaggerIntegration.
Your feedback is appreciated.
from servicemodel.grpc.
Hi Max,
Works great!
Thank you very much for everything, I see you really put the effort in that one.
Can I somehow get the swagger request "Try it out" method to work with JsonElement?
JsonElement is a crucial feature in swagger for dynamic objects to be serialized, in the current swagger implementation it is not being serialized.
from servicemodel.grpc.
Hi igalHcsra,
i have doubts that i understand your question about JsonElement. Do you ask about contract like this?
[ServiceContract]
public interface ITestService
{
[OperationContract]
Task<JsonElement> Ping(JsonElement element1, JsonElement element2);
}
from servicemodel.grpc.
[DataContract]
public class TestContract
{
[DataMember]
public JsonElement TestDynamicData { get; set; }
}
from servicemodel.grpc.
Behind the scenes http request from swagger UI is converted into standard gRPC request by ServiceModel.Grpc HTTP/1.1 JSON gateway.
Before "Try it out" from swagger UI, please make sure that standard gRPC communication is working.
I assume you already have the following service implementation:
[DataContract]
public class TestContract
{
[DataMember]
public JsonElement TestDynamicData { get; set; }
}
[ServiceContract]
public interface ITestService
{
[OperationContract]
Task<TestContract> Ping(TestContract contract);
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddServiceModelGrpc(options =>
{
// here should be marshaller factory, which can correctly serialize and deserialize instance of TestContract
options.DefaultMarshallerFactory = new ...
});
}
}
Test gRPC client call, example can be found here
var clientFactory = new ClientFactory
{
// here should be marshaller factory, which can correctly serialize and deserialize instance of TestContract, the same as in Startup.cs on server
MarshallerFactory = new ...
};
var channel = new Channel("localhost", 5000, ...);
var proxy = clientFactory.CreateClient<ITestService>(channel);
var contract = new TestContract
{
TestDynamicData = ... // set JsonElement
};
await proxy.Ping(contract);
when proxy.Ping(contract)
works then you can use swagger UI "Try it out" with following JSON:
{
"contract": {
"testDynamicData": {
"property1": 1,
"property2": "some text"
}
}
}
JsonElement in the TestContract can be correctly handled by System.Text.Json.JsonSerializer.
ServiceModel.Grpc does not provide MarshallerFactory based on System.Text.Json.JsonSerializer.
The following implementation can be used:
public sealed class JsonMarshallerFactory : IMarshallerFactory
{
public Marshaller<T> CreateMarshaller<T>()
{
return new Marshaller<T>(Serialize, Deserialize<T>);
}
private static void Serialize<T>(T value, SerializationContext context)
{
using (var buffer = context.AsStream())
using (var writer = new Utf8JsonWriter(buffer))
{
JsonSerializer.Serialize(writer, value);
}
context.Complete();
}
private static T Deserialize<T>(DeserializationContext context)
{
var reader = new Utf8JsonReader(context.PayloadAsReadOnlySequence());
return JsonSerializer.Deserialize<T>(ref reader)!;
}
}
from servicemodel.grpc.
The marshaller works great it really covers the swagger functionallity to the fullest, is there a way to use the marshaller for sending also 'object' or 'dynamic' types as well?
I think I'll make the serializer to differ when it comes from the swagger client or any other so when it's a swagger call it will use the jsonMarshaller and when it comes from c# client then use an 'object' type.
from servicemodel.grpc.
is there a way to use the marshaller for sending also 'object' or 'dynamic' types as well?
As you can see it is fully under your control.
Swagger call is just an emulation of client call
Standard gRPC workflow for client call:
- client call
- marshaller.serialize() request
- http call
- marshaller.deserialize() request
- invoke you service
- marshaller.serialize() response
- http response
- marshaller.deserialize() response
Swagger UI call:
- http request with json body
- ServiceModel.Grpc HTTP/1.1 JSON gateway deserialize from json
- ServiceModel.Grpc HTTP/1.1 JSON gateway make gRPC request
- marshaller.serialize() request
- invoke gRPC (the same as for client call)
- marshaller.deserialize() request (the same as for client call)
- invoke you service (the same as for client call)
- marshaller.serialize() response (the same as for client call)
- ServiceModel.Grpc HTTP/1.1 JSON gateway marshaller.deserialize() response
- ServiceModel.Grpc HTTP/1.1 JSON gateway serialize response into json
- return json to Swagger UI
from servicemodel.grpc.
Brilliant! Thank you very much Max you are exellent!
from servicemodel.grpc.
Related Issues (20)
- How to get request body with httpcontext? HOT 4
- implement Hub in ServiceModel.Grpc HOT 4
- Question: does this somehow relate to protobuf-net.Grpc? HOT 4
- Question: Best serialisation method for performance HOT 3
- Multiple Implementations of a Service Contract on Same Server HOT 6
- CallContext model is generated on Swagger HOT 3
- Protobuf-net and Error Handling HOT 3
- [Question] What is the namespace for creation of a Channel? HOT 2
- [Question] What is the correct way to update the value of Authorization header with every new gRPC call? HOT 2
- Throwing an exception on the server side does not work properly on the Blazor WebAssembly client side HOT 2
- how to Initialize IClientBuilder? HOT 2
- Is there dependency injection container support for client? HOT 7
- Call GRPC hosted with Blazor (Asp.Net Core Server) from Console App HOT 3
- Support for MemoryPack HOT 5
- After publishing blazor server service model in the client gives argumentoutofrangeexception HOT 4
- Design Time Issue With Return Type of Task HOT 4
- WriteClientStream hangs after the client streaming call completes. HOT 1
- IEnumerable data is not empty array, it is null? HOT 9
- MessagePackSerializationException: Building dynamic formatter only allows public type HOT 2
- Update MessagePack to 2.4.35, support DateOnly and TimeOnly HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from servicemodel.grpc.